Reading string from buffer pointer returned by DLL function - em, pointers needed

Sorry tooo much cheese on that.

I found some VB code which I’m trying to convert to lotusscript to get the username of a someone logged into a remote computer.

I have declared the following functions:

Declare Function NetWkstaUserEnum Lib “netapi32.dll” (Byval strServerName As Unicode String, Byval dwLevel As Long, _

lpBuffer As Long, Byval dwPrefMaxLen As Long, lpdEntriesRead As Long, lpdTotalEntries As Long, _

lpdResumehandle As Long) As Long

Declare Function NetApiBufferFree Lib “netapi32.dll” (Byval pBuffer As Long) As Long

Declare Function NetApiBufferSize Lib “netapi32.dll” (lpBuffer As Any, lpLength As Long) As Long

Declare Sub CopyMem Lib “kernel32.dll” Alias “RtlMoveMemory” (pTo As Any, uFrom As Any, Byval lSize As Long)

Declare Function lstrlenW Lib “kernel32.dll” (Byval lpString As Long) As Long

The first three I know I’ll need to use. I’m not sure what the last two are for.

So far I have the following:

Sub Initialize

Dim lngReturn As Long, lngBuffer As Long, lngEntriesRead As Long, lngTotalEntries As Long, lngResumeHandle As Long

Dim intCount As Integer

Dim strHostName As String * 255

Dim results As String



strHostName="56.9.0.23" & Chr(0) ' add null character to string.

lngReturn = NetWkstaUserEnum(strHostName, 0, lngBuffer, -1, lngEntriesRead, lngTotalEntries, lngResumeHandle)



If lngReturn=NERR_Success Then 

	Msgbox "API Call Successfull: " & lngEntriesRead & " entries read of " & lngTotalEntries & " total entries."

	

	'>> Need to get at lngBuffer here.

	

	If lngBuffer Then

		Msgbox "Clearing buffer."

		Call NetApiBufferFree(Byval lngBuffer)

	End If

Else

	Msgbox "API Call failed."

End If

End Sub

My call works and my message boxes tell me I’m getting results. Problem is I can’t figure out how to read from lngBuffer.

Microsofts description of the function describe it as "Pointer to the buffer that receives the data. The format of this data depends on the value of the level parameter. "

Obviously I’m not too familar with VB or calling C functions so any tips would be wonderful.

There is an example in the help where they declare the function as I have (returns value to a pointer) and just place a fixed length string in the call but it doesn’t seem right to me and I can’t make it work (type mismatch errors).

Subject: Reading string from buffer pointer returned by DLL function - em, pointers needed…

Seem to be getting somewhere, left my parameters as pointers (long) and used the following function:

Function PtrToString(lpwString As Long) As String

'Convert a LPWSTR pointer to a VB string

Dim Buffer() As Byte

Dim nLen As Long

Dim i As Integer

Dim tmpResult As String



If lpwString Then

	nLen = lstrlenW(lpwString) * 2

	Msgbox "nLen: " & nLen

	If nLen Then

		Redim Buffer(0 To (nLen - 1)) As Byte

		CopyMem Buffer(0), Byval lpwString, nLen

		Msgbox "Buffer size " & Ubound(Buffer)

		For i=0 To Ubound(Buffer)

			If Buffer(i)<>0 Then 'each character followed by NULL character. Seems if we add this to string it clears whole string.

				tmpResult = tmpResult & Uchr$(Buffer(i))

			End If

		Next

		PtrToString=tmpResult

	End If

End If

End Function

Calling code in the middle of original code is:

	For intCount=0 To Ubound(typWkStaInfo)

		lngReturn = NetApiBufferSize(Byval lngBuffer, lngLength)

		CopyMem typWkStaInfo(0), Byval lngBuffer, lngLength

		

		Msgbox PtrToString(typWkStaInfo(intCount).lngUserName)

	Next intCount

Seems to work - not sure that I’m comfortable with this 'CopyMem’thing though…

Subject: A couple of things…

The reason you are seeing zeroes in your buffer is because you are dealing with unicode characters. Instead of bytes you should use integers.

Secondly, you should be able to use a string directly with the API call, and not having to rebuild this buffer manually.

A bit like this:

Declare Sub MovMem_UnicodeString Lib “kernel32.dll” Alias “RtlMoveMemory” (Byval targetString As Unicode String, Byval sourcePtr as Long, Byval size As Long)

Dim ptr as Long

Dim length as Long

Dim result as String*256 ’ This needs to be large enough to accomodate your result!

’ Set ptr and length to the pointer of the string and the size of the buffer in memory.

’ You have that part nailed already.

’ Copy the result to the string

call MovMem_UnicodeString(result, ptr, length)

’ Result now contains your string

Also keep in mind that RtlMoveMemory is a windows-only call, and thus limits portability. Since you’re already using other Win32 API calls, I assume that this is not going to be a problem.

cheers,

Bram

Subject: Thanks, clears a few things up!