vbstroehle Posted March 20, 2004 Posted March 20, 2004 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() Quote
*Experts* Nerseus Posted March 20, 2004 *Experts* Posted March 20, 2004 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 Quote "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
vbstroehle Posted March 20, 2004 Author Posted March 20, 2004 Appreciate you help Nerseus. Recommending the search for Chr(0) did the trick. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.