繁体   English   中英

我在这里遇到的“类型不匹配错误”是什么?

[英]What is the "Type Mismatch Error" I am getting here?

我在这个特定的 VBA 代码中遇到类型不匹配错误,我正在使用 64 位 VBA 但它是为 32 位开发的。 任何解决方案,我是 VBA 的新手,我无法弄清楚。 它在 VarPtr 中显示错误(Public Fuction Hook() As boolean)。 我在下面添加代码

Option Explicit


Private Const PAGE_EXECUTE_READWRITE = &H40

Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Long, Source As Long, ByVal Length As Long)

Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As Long, _
        ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long

Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Long

Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _
        ByVal lpProcName As String) As Long

Private Declare PtrSafe Function DialogBoxParam Lib "User32" Alias "DialogBoxParamA" (ByVal hInstance As Long, _
        ByVal pTemplateName As Long, ByVal hWndParent As Long, _
        ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer

Dim HookBytes(0 To 5) As Byte
Dim OriginBytes(0 To 5) As Byte
Dim pFunc As Long
Dim Flag As Boolean

Private Function GetPtr(ByVal Value As Long) As Long
    GetPtr = Value
End Function

Public Sub RecoverBytes()
    If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 6
End Sub

Public Function Hook() As Boolean
    Dim TmpBytes(0 To 5) As Byte
    Dim p As Long
    Dim OriginProtect As Long

    Hook = False

    pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")


    If VirtualProtect(ByVal pFunc, 6, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then

        MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, 6
        If TmpBytes(0) <> &H68 Then

            MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 6

            p = GetPtr(AddressOf MyDialogBoxParam)

            HookBytes(0) = &H68
            MoveMemory ByVal VarPtr(HookBytes(1)), ByVal VarPtr(p), 4
            HookBytes(5) = &HC3

            MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 6
            Flag = True
            Hook = True
        End If
    End If
End Function

Varptr 将在 64 位 Excel 上返回“LongPtr”,在 32 位 Excel 上返回“Long”。 您的 api 声明都使用“Long”,这在 VarPtr 将返回 Long 的 32 位世界中很好,但在 VarPtr 返回 LongPtr 的 64 位 excel 中则不行。 您需要更新您的 api 声明以使用 LongPtr 而不是 Long 用于 64 位。 API 调用之一的示例如下:

#If VBA7 Then
    Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias _
            "RtlMoveMemory" (ByRef Destination As LongPtr, ByRef Source As LongPtr, _
            ByVal Length As LongPtr)
#Else
    Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias _
           "RtlMoveMemory" (Destination As Long, Source As Long, _
            ByVal Length As Long)
#End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM