I'm calling CredUnPackAuthenticationBuffer
as per this question:
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
This call succeeds, error is 0 and usernameBuf, domainBuf and passwordBuf have values.
I'm trying to get the text from these pointers. From MSDN, I know these values are pointers to a null-terminated string. I've tried calling the following code (from here) to get the string data, but byteCount
is always 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
What am I doing wrong? I've tried passing the buffers as String, and StrPtr(), which crashes Word.