gpt4 book ai didi

windows - 如何使 WinHttpCrackUrl 在 64 位中工作

转载 作者:可可西里 更新时间:2023-11-01 09:20:49 28 4
gpt4 key购买 nike

我有使用 WinHttp 的 Visual Basic for Applications 代码,并且可以与在 32 位 Windows XP 上运行的 32 位 Office 2010 完美配合。相同的代码无法在 64 位 Windows 8 上的 64 位 Office 2013 上正常运行,即使它可以正常编译。

问题是 WinHttpCrackUrl() 在 Windows 8 上返回错误 87“参数不正确”。

我已经仔细检查并三重检查了代码中所有指针是否在适当的地方声明为 LongPtr。我做错了什么?

以下代码在 32 位 Excel/Windows 上运行良好,但在 64 位 Excel/Windows 上运行失败:

Private Type URL_COMPONENTS
dwStructSize As Long
lpszScheme As LongPtr
dwSchemeLength As Long
nScheme As Long
lpszHostName As LongPtr
dwHostNameLength As Long
nPort As Long
lpszUserName As LongPtr
dwUserNameLength As Long
lpszPassword As LongPtr
dwPasswordLength As Long
lpszUrlPath As LongPtr
dwUrlPathLength As Long
lpszExtraInfo As LongPtr
dwExtraInfoLength As Long
End Type

Private Declare PtrSafe Function WinHttpCrackUrl Lib "WinHTTP" ( _
ByVal pwszUrl As LongPtr, _
ByVal dwUrlLength As Long, _
ByVal dwFlags As Long, _
ByRef lpUrlComponents As URL_COMPONENTS) As Long

Sub Test()
Dim result as Long
Dim URLComp As URL_COMPONENTS
Dim mURL as String
mURL = "http://www.stackoverflow.com" & vbNullChar

With URLComp
.dwStructSize = Len(URLComp)
.dwHostNameLength = -1
.dwSchemeLength = -1
.dwUrlPathLength = -1
End With

result = WinHttpCrackUrl(StrPtr(mURL), 0, 0, URLComp)

' Prints 1 on 32-bit Excel/Windows (indicating success)
' Prints 0 on 64-bit Excel/Windows (indicating failure)
Debug.Print result

' Prints 87 on 64-bit Excel/Windows ("The parameter is incorrect.")
Debug.Print err.LastDllError
End Sub

最佳答案

struct 在 C++ 代码中是对齐的,但 VBA 结构是打包的。在 32 位中,对于您的结构,这无关紧要,因为所有成员都对齐 4。但在 64 位中,指针需要 8 字节对齐,并且结构有一些额外的填充。像这样输入:

Private Type URL_COMPONENTS
dwStructSize As Long
padding1 As Long
lpszScheme As LongPtr
dwSchemeLength As Long
nScheme As Long
lpszHostName As LongPtr
dwHostNameLength As Long
nPort As Long
lpszUserName As LongPtr
dwUserNameLength As Long
padding2 As Long
lpszPassword As LongPtr
dwPasswordLength As Long
padding3 As Long
lpszUrlPath As LongPtr
dwUrlPathLength As Long
padding4 As Long
lpszExtraInfo As LongPtr
dwExtraInfoLength As Long
padding5 As Long
End Type

我猜你会想要一些条件编译来切换更好的 32 位和 64 位版本,但我必须承认不知道如何用 VBA 做到这一点。

关于windows - 如何使 WinHttpCrackUrl 在 64 位中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156699/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com