'"GetCurrentProcessId()" VBA 7 migration
I am currently migrating a large Excel tool from VBA 6 and Excel 32bit to VBA 7 with Excel 64bit.
The kernel32 function "GetCurrentProcessID" is causing excel to crash. I can recreate the error in an empty excel file with the single line of VBA code
Declare PtrSafe Function GetCurrentProcessId Lib "kernel32" () As Long
If I then call the function within Excel, the number gets populated on the sheet, but Excel crashes after a few seconds. I tried to declare the function with
Declare PtrSafe Function GetCurrentProcessId Lib "kernel32" () As LongPtr
and other variations, but nothing seems to work. I am running Microsoft Office 365 with 64 bit.
Did anyone come across this problem before?
Edit: Sorry, I should have been clearer. The presence of the declaration does not cause Excel to crash. Instead, calling it from the spreadsheet causes the crash. I did notice though that calling it from within VBA does not cause a crash, and a wrapper function can be built such that it can be used again from within the spreadsheet. Very strange, but workable solution.
Solution 1:[1]
As noted in the comments, your first declaration works fine without crashing Excel.
In my testing I noticed that GetCurrentProcessId in Excel VBA returns null. So I think that the reason your program is crashing is that you are failing to handle the null. It's not this command but whatever you are doing with the response that is causing the crash. Don't pass null in to native code. It will crash.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HackSlash |
