Compile Error:
The code in this project must is updated for use on64-bit systems.
Review and update Declare statements and then mark them with the Ptrsafe attribute.
1. Question:
32-bit VBA program that is running on the 64-bit system, this compilation error occurs.
Environment: Office2010 Excel 64-bit, 64-bit Win7 OS
?
2. Causes
In the original program, the function is declared as follows. This function compiles and runs in the 32-bit system without problems. However, a compilation error will occur in the 64-bit system.
Private Declare Function widechartomultibyte Lib "Kernel32" _
(ByVal CodePage as Long, ByVal dwFlags as Long, lpwidecharstr as Integer, ByVal Cchwidechar as Long, _
Lpmultibytestr as Byte, ByVal Cchmultibyte as Long, ByVal Lpdefaultchar as String, ByVal Lpuseddefaultchar as Long) as Lon G
?
3. Modifications
In fact, the message, it has been explained very clearly.
As long as you add the "Ptrsafe" attribute at the declaration of the function (without modifying other places), the program is normal.
Post-Modification Program:
Private Declare ptrsafeFunction widechartomultibyte Lib "Kernel32" _
(ByVal CodePage as Long, ByVal dwFlags as Long, lpwidecharstr as Integer, ByVal Cchwidechar as Long, _
Lpmultibytestr as Byte, ByVal Cchmultibyte as Long, ByVal Lpdefaultchar as String, ByVal Lpuseddefaultchar as Long) as Lon G
?
4. Expansion
①ptrsafe<keyword>
https://msdn.microsoft.com/en-us/library/office/gg278832.aspx?f=255&MSPPError=-2147217396
Declare statements with the Ptrsafe keyword is the recommended syntax. Declare statements that include ptrsafe work correctly in the VBA7 development environment on both 32-bit and 64-bit PLATF Orms-All data types in the Declare statement (parameters and return values), need to store 64-bit Quantitie S is updated to use Longlong for 64-bit integrals or LongPtr for pointers and handles. To ensure backwards compatibility with VBA version 6 and earlier use the following construct:
?
②longptr Data Type
https://msdn.microsoft.com/en-us/library/office/gg251378.aspx?f=255&MSPPError=-2147217396
LONGPTR (Long integer on 32-bit systems, Longlong integer on 64-bit systems) variables is stored as signed 32-bit (4-byte ) numbers ranging in value from-2,147,483,648 to 2,147,483,647 on 32-bit systems; and signed 64-bit (8-byte) numbers ranging in value from-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit Systems.
VBA Excel widechartomultibyte Compile error on 64-bit System