Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In the code below how do i get the correct length of the data stored in the location pointed

to by myBuffer. Using size and Len i keep getting 4. I seem to be getting 4 for everything i have tried.

 

 

Dim itText(255) As Byte

 

Dim MyGCHandle As GCHandle = GCHandle.Alloc(itText, GCHandleType.Pinned)

Dim myBuffer As IntPtr = MyGCHandle.AddrOfPinnedObject()

ReadProcessData(lvDPointer, myBuffer, 255)

 

Dim sBuf As String, sLen As Integer

 

sLen = myBuffer.Size -- always returns 4

sLen = len(mybuffer) -- always returns 4

sLen = marshal.sizeof(mybuffer) -- always returns 4

 

sBuf = New String(CChar(" "), sLen + 2)

CopyMemory(sBuf, myBuffer, sLen)

MyGCHandle.Free()

  • *Experts*
Posted

If you're using this for an API, most APIs return you the size of what was read. That would be what you'd use in the CopyMemory call.

 

If you were to find a managed way to figure out the size of what myBuffer pointed to, I'm sure it would say 256 - the size of the array. It would never know the size of the data read in by the function.

 

Your methods to determine the size is always 4 because they're all returning the amount of memory used by myBuffer. A pointer in current operating systems is 4 bytes (32 bit systems).

 

While you could get the size of itText, you pretty much already know it.

 

If the API is returning you text, you could convert the byte array to a string then use substring on Chr(0). Or, simply loop through each byte in itText until you find the value 0 - that should be the null terminator in C-style strings.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...