Ber*_*ard 1 excel vba 32bit-64bit
当我运行 32 位版本的 Excel 时,我使用的代码运行良好。当我切换到 64 位版本后,宏就崩溃了。我更新了 dll 调用以在LongPtr
各处使用而不是Long
.
对于特定的 VBA7,有什么方法可以确定哪些参数和返回类型需要更改,哪些不需要更改Declare Function
?
这是我更新的一些“声明函数”的示例(实际上还有更多)。
#If VBA7 Then
Private Declare PtrSafe Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As LongPtr) As LongPtr
Private Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As LongPtr, ByVal nWidth As LongPtr, ByVal nHeight As LongPtr) As LongPtr
Private Declare PtrSafe Function DeleteDC Lib "gdi32.dll" (ByVal hdc As LongPtr) As LongPtr
Private Const LOGPIXELSY As Long = 90
#Else
Private Declare CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Const LOGPIXELSY As Long = 90
#End If
Run Code Online (Sandbox Code Playgroud)
此代码改编自此问题的答案: vb 宏字符串宽度
相关片段复制如下:
Private Function GetLabelSize(text As String, font As StdFont) As SIZE
Dim tempDC As Long
Dim tempBMP As Long
Dim f As Long
Dim lf As LOGFONT
Dim textSize As SIZE
tempDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0)
tempBMP = CreateCompatibleBitmap(tempDC, 1, 1)
Run Code Online (Sandbox Code Playgroud)
我收到一个运行时错误,只显示“编译错误:类型不匹配”。对 的函数调用会CreateDC
突出显示,调试器会在该函数上中断GetLabelSize
。我不知道现在哪个变量导致了错误。我还假设一旦修复了第一个错误,我也会遇到其他错误。
我是否需要将最后一个参数 ( ) 的值ByVal 0
作为显式类型变量传递?如果是这样怎么办?
我更新了 dll 调用以在
LongPtr
各处使用而不是Long
.
你不应该那样做。
通过添加PtrSafe
到函数声明中,您向编译器保证您已将LongPtr
其放置在所有需要的地方,而不是其他地方。
LongPtr
是一个指针大小的整数。它必须用于与指针具有相同大小的事物。
要了解哪些 Windows API 类型应被描述为LongPtr
,您必须查看原始函数签名,请参阅https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types,跟踪使用的数据类型从所有类型typedef
一直到基本类型,并用于LongPtr
那些指向事物的指针。
对于您所展示的功能,那就是
#If VBA7 Then
Private Declare PtrSafe Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As LongPtr) As LongPtr
Private Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As LongPtr, ByVal nWidth As Long, ByVal nHeight As Long) As LongPtr
Private Declare PtrSafe Function DeleteDC Lib "gdi32.dll" (ByVal hdc As LongPtr) As Long
#Else
Private Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
#End If
Run Code Online (Sandbox Code Playgroud)
当您声明变量来保存结果时,您还LongPtr
需要使用:#If VBA7
#If VBA7 Then
Dim tempDC As LongPtr
Dim tempBMP As LongPtr
#Else
Dim tempDC As Long
Dim tempBMP As Long
#End If
Run Code Online (Sandbox Code Playgroud)
如果您不需要支持Office 2007 及更早版本,您可以放弃#If VBA7
s 并仅使用LongPtr
分支。
归档时间: |
|
查看次数: |
5725 次 |
最近记录: |