Vba 指向字符串的长指针

Vba 指向字符串的长指针,vba,winapi,ms-word,Vba,Winapi,Ms Word,我正在根据问题致电CredUnPackAuthenticationBuffer: Public Declare PtrSafe Function CredUnPackAuthenticationBuffer Lib "credui" Alias "CredUnPackAuthenticationBufferW" ( _ ByVal dwFlags As LongPtr, _ ByVal pAuthBuffer As LongPtr, _ ByVal cbA

我正在根据问题致电CredUnPackAuthenticationBuffer:

Public Declare PtrSafe Function CredUnPackAuthenticationBuffer Lib "credui" Alias "CredUnPackAuthenticationBufferW" ( _
ByVal dwFlags As LongPtr, _
ByVal pAuthBuffer As LongPtr, _
ByVal cbAuthBuffer As LongPtr, _
ByRef pszUserName As LongPtr, _
ByRef pcchMaxUserName As LongPtr, _
ByRef pszDomainName As LongPtr, _
ByRef pcchMaxDomainName As LongPtr, _
ByRef pszPassword As LongPtr, _
ByRef pcchMaxPassword As LongPtr) _
As LongPtr

Dim res As LongPtr
        Dim usernameBuf As LongPtr
        Dim domainBuf As LongPtr
        Dim passwordBuf As LongPtr
        Dim max As Long
        max = 100
        Dim flags As Long
        flags = CRED_PACK_GENERIC_CREDENTIALS
        
        res = CredUnPackAuthenticationBuffer(flags, ppvOutAuthBuffer, pulOutAuthBufferSize, usernameBuf, MAX_USER_NAME, domainBuf, MAX_DOMAIN, passwordBuf, MAX_PASSWORD)
        Dim error As Long
        error = Err.LastDllError
此调用成功,错误为0,并且usernameBuf、domainBuf和passwordBuf具有值

我正试图从这些指针中获取文本。从MSDN中,我知道这些值是指向以null结尾的字符串的指针。我尝试从调用以下代码以获取字符串数据,但字节数始终为0

Private Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
 (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal Length As Long)

Private Declare PtrSafe Function lstrlenW Lib "kernel32.dll" (ByVal lpString As LongPtr) As Long


Public Function StringFromPointerW(ByVal pointerToString As LongPtr) As String

    Const BYTES_PER_CHAR As Integer = 2

    Dim tmpBuffer()    As Byte
    Dim byteCount      As Long
 
    ' determine size of source string in bytes
    byteCount = lstrlenW(pointerToString) * BYTES_PER_CHAR
    
    If byteCount > 0 Then
        ' Resize the buffer as required
        ReDim tmpBuffer(0 To byteCount - 1) As Byte
        
        ' Copy the bytes from pointerToString to tmpBuffer
        Call CopyMemory(VarPtr(tmpBuffer(0)), pointerToString, byteCount)
    End If
 
    ' Straigth assigment Byte() to String possible - Both are Unicode!
    StringFromPointerW = tmpBuffer

End Function
我做错了什么?我尝试将缓冲区作为字符串和strprtr传递,这会使Word崩溃。

pszUserName、pszDomainName和pszPassword的函数。这意味着ByVal作为LongPtr,您必须传递已经分配了相应字符数的字符串的StrPtrs值。
相应的三个缓冲区长度参数ByRef为Long,而不是LongPtr。dwFlags、cbAuthBuffer和函数返回值也很长。 您也不应该直接传递MAX_uu常量,因为这些参数应该是可写的

公共声明PtrSafe函数CredUnPackAuthenticationBuffer Lib credui别名CredUnPackAuthenticationBufferW_ 拜瓦尔:只要_ ByVal pAuthBuffer作为LongPtr_ ByVal cbAuthBuffer只要_ ByVal pszUserName作为LongPtr_ ByRef PCCHMAXSusername,只要_ ByVal pszDomainName As LongPtr_ ByRef pcchMaxDomainName,只要_ ByVal pszPassword作为LongPtr_ ByRef PCCHMAXSPassword尽可能长_ 只要 暗淡如长 Dim usernameBuf作为字符串 Dim domainBuf作为字符串 Dim passwordBuf作为字符串 暗淡的旗子一样长 flags=信用卡包装通用凭证 Dim usernameBufLen长度:usernameBufLen=MAX\U USER\U NAME Dim domainBufLen作为Long:domainBufLen=MAX_DOMAIN Dim passwordBufLen的长度:passwordBufLen=MAX\u PASSWORD usernameBuf=字符串$usernameBufLen,vbNullChar domainBuf=字符串$domainBufLen,vbNullChar passwordBuf=字符串$passwordBufLen,vbNullChar res=CredUnPackAuthenticationBuffer_ 旗帜_ ppvoututhBuffer_ pulOutAuthBufferSize_ StrPtrusernameBuf_ usernameBufLen_ 斯特尔多曼布夫_ domainBufLen_ StrptPasswordBuf_ 密码Buflen_ 您不需要StringFromPointerW。缓冲区已包含可用数据。

pszUserName、pszDomainName和pszPassword的函数。这意味着ByVal作为LongPtr,您必须传递已经分配了相应字符数的字符串的StrPtrs值。
相应的三个缓冲区长度参数ByRef为Long,而不是LongPtr。dwFlags、cbAuthBuffer和函数返回值也很长。 您也不应该直接传递MAX_uu常量,因为这些参数应该是可写的

公共声明PtrSafe函数CredUnPackAuthenticationBuffer Lib credui别名CredUnPackAuthenticationBufferW_ 拜瓦尔:只要_ ByVal pAuthBuffer作为LongPtr_ ByVal cbAuthBuffer只要_ ByVal pszUserName作为LongPtr_ ByRef PCCHMAXSusername,只要_ ByVal pszDomainName As LongPtr_ ByRef pcchMaxDomainName,只要_ ByVal pszPassword作为LongPtr_ ByRef PCCHMAXSPassword尽可能长_ 只要 暗淡如长 Dim usernameBuf作为字符串 Dim domainBuf作为字符串 Dim passwordBuf作为字符串 暗淡的旗子一样长 flags=信用卡包装通用凭证 Dim usernameBufLen长度:usernameBufLen=MAX\U USER\U NAME Dim domainBufLen作为Long:domainBufLen=MAX_DOMAIN Dim passwordBufLen的长度:passwordBufLen=MAX\u PASSWORD usernameBuf=字符串$usernameBufLen,vbNullChar domainBuf=字符串$domainBufLen,vbNullChar passwordBuf=字符串$passwordBufLen,vbNullChar res=CredUnPackAuthenticationBuffer_ 旗帜_ ppvoututhBuffer_ pulOutAuthBufferSize_ StrPtrusernameBuf_ usernameBufLen_ 斯特尔多曼布夫_ domainBufLen_ StrptPasswordBuf_ 密码Buflen_
您不需要StringFromPointerW。缓冲区已包含可用数据。

函数要求pszUserName、pszDomainName和pcchMaxPassword使用LPWSTR。这意味着ByVal作为LongPtr,您必须传递已经分配了相应字符数的字符串的StrPtrs值。啊,好的,这是我没有尝试的一种组合。是否有正确的方法来分配字符串中的字符?是的,s=string$100,vbNullChar。相应的三个缓冲区长度参数是ByRef-As-Long,而不是LongPtr。dwFlags、cbAuthBuffer和函数返回值也很长。明白了,谢谢!函数要求LPWSTR提供pszUserName、pszDomainName和PCCHMAXSPassword。这意味着ByVal作为LongPtr,您必须传递已经分配了相应字符数的字符串的StrPtrs值。啊,好的,这是我没有尝试的一种组合。是否有正确的方法来分配字符串中的字符?是的,s=string$100,vbNullChar。相应的三个缓冲区长度参数是ByRef-As-Long,而不是LongPtr。dwFlags、cbAuthBuffer和函数返回值也很长。明白了,谢谢!