“在少数地方添加“PtrSafe””

huangapple go评论62阅读模式
英文:

Adding "PtrSafe" in few places

问题

以下是翻译好的内容:

"Complie Error" 之前显示代码,并要求将其更新为 64 位,因此我在几个地方添加了 "PtrSafe",但现在它在上面的代码中出现了 "byref argument type mismatch" 错误,标记为布尔类型。

之前代码正常工作,但最近我已将Excel更改为Office365,但它不起作用。我不知道如何修复这个问题。如果有人能帮忙修复上面的代码,不胜感激。

英文:

The code was showing "Complie Error" earlier and asking to update it to 64 bit so I changed and added "PtrSafe" in few places but now it is giving

> "byref argument type mismatch"

error in above code. by highlighting to boolean.

The code was earlier working fine but recently I have changed excel to Office365 in which it is not working. I don't know how to fix this. If someone can help fixing the above code? Thanks in advance.

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

    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

答案1

得分: 1

只添加PtrSafe并不能让您的函数在64位中工作。PtrSafe只是告诉VBA,“嘿,你可以在64位中安全使用这个函数(我确保它可以工作)” - 确保它能够工作需要由您来完成。您可以在此处查看兼容性文档:https://learn.microsoft.com/en-us/office/client-developer/shared/compatibility-between-the-32-bit-and-64-bit-versions-of-office

最重要的一点(也可能是您的问题所在)是,指针和句柄的大小取决于您是在32位还是

英文:

Just adding PtrSafe does not make your function work in 64-bit. The PtrSafe just tells VBA, "hey you are safe to use this in 64-bit as well (I made sure it works)" - the making sure it works has to be done by you. You can see documentation on compatibility here: https://learn.microsoft.com/en-us/office/client-developer/shared/compatibility-between-the-32-bit-and-64-bit-versions-of-office

The most important thing (and where your problem probably lies) is, that pointers and handles have a different size depending on if you are using 32bit or 64bit. Pointers/handles. In 32bit they should be of type Long in 64bit of type LongLong - The Type LongPtr assigns the type depending on if you are on 32bit or 64bit. So on 32bit LongPtr is Long on 64bit LongLong - So what you usually do is go through your Declare Functions, check the APIs documentation, and make sure all your pointers/handles are of type LongPtr - For a more in depth and more detailed explanation see the resource linked above.

huangapple
  • 本文由 发表于 2023年5月10日 15:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定