Sleep Lib "kernel32" gives 64-bit systems error
Asked Answered
O

2

21

I'm trying to close access (Application.Quit) after running all functions.

VBA close access after all functions finished has been a reference for me.

but when I Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long), It's giving me the following error:

The code in this project must be updated for use on 64 bit systems.

Is there any replacement of this code to do run all functions before completely closing access?

Oversupply answered 13/1, 2017 at 15:52 Comment(0)
S
30

The dwMilliseconds parameter is a DWORD, so it will technically be 32bit on a 32bit machine and 64bit on a 64bit machine. Because of this, it requires PtrSafe notation (although technically dwMilliseconds will marshal correctly because it's ByVal... and who wants to wait that long anyway) Change the declaration to this:

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sarsenet answered 13/1, 2017 at 16:2 Comment(3)
Thank you so much, as always! @cominternOversupply
What you say here in the first sentence is not correct. A DWORD is always a 4-byte type, which means it'll be 32 bits regardless of the target architecture. Now, it may be that VBA's warning is a false positive, but you can guarantee that DWORD will never be 64 bits in size, and I suspect the real solution is just that you need PtrSafe. (Note that the same is not true for DWORD_PTR, which is an unsigned type the same size as a native pointer, so your statement would apply to that type, but that's not what the Sleep API uses.)Stlaurent
Got it. It really help me.Hartwig
B
6

change your api declaration to this :

#If VBA7 And Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

For 64bit APIs read this: http://www.jkp-ads.com/articles/apideclarations.asp

Bimbo answered 13/1, 2017 at 15:59 Comment(3)
Do we need the extra And Win64 that the accepted answer didn't have? What is best for greatest compatibility?Shawna
Do we need the extra And Win64? - In this case, no. But I would put it there always, in case you want to use a 64bit type.Sarcastic
On Win10 Pro/64, I get an error message on the second line: Compile error: Fixed-length strings, arrays, user-defined types and Declare statements are not allowed as Public members of object modules. I changed it to Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)Enshrine

© 2022 - 2024 — McMap. All rights reserved.