How to include Chr(0) in String variable

A

Ajak

Hello,

I'm converting a VB6 app to VB2005 which uses a third party API dll.

The API function accepts structure as its parameter (byRef).
Below is the struct (shortened for clarity):-

<StructLayout(LayoutKind.Sequential)> _
Public Structure MCPBusType
Public OutBuffer As String <-- the API function will
fill result here: Initialised to String of 4096 characters
Public OutBufferSize As Integer <-- the length/size of outBuffer
(=4096 of course)
Public ResponseLen As Integer
End Structure

And I declare the Api function as follows:-

<DllImport("MCPAPI.DLL", EntryPoint:="McpEnum")> _
Public Shared Function McpEnum(ByRef MCPBus As MCPBusType) As Integer
End Function

My problem is the API function returns result in OutBuffer and it's not
necessarily at the start of it. What I meant is the result in OutBuffer
could be Chr(0) & Chr(0) & "RESULT" & Chr(0) & .... but the expected result
is that "RESULT". If I take a look in OutBuffer, I couldn't get the "RESULT"
but instead it will return zero-length string ("") since there is a null
termination char at the begining of OutBuffer. I couldn't use Mid(OutBuffer,
2, 5) cos that will throw ArgOutOfRangeException.

In VB6, taking the OutBuffer gives me everything including null chars, up to
the length of the string (4096).

So, please, is there a way for me to read the whole 4096 chars in OutBuffer,
including the null chars?

Or is there any way I can change the struct and change OutBuffer to array of
bytes, and pass them to the API function and it will take it as if it was
string variable?

Please help....
 
H

Herfried K. Wagner [MVP]

Ajak said:
I'm converting a VB6 app to VB2005 which uses a third party API dll.

The API function accepts structure as its parameter (byRef).
Below is the struct (shortened for clarity):-

<StructLayout(LayoutKind.Sequential)> _
Public Structure MCPBusType
Public OutBuffer As String <-- the API function will
fill result here: Initialised to String of 4096 characters
Public OutBufferSize As Integer <-- the length/size of
outBuffer (=4096 of course)
Public ResponseLen As Integer
End Structure

Post the C version of the structure. Maybe the string should be stored
inside the structure.
 
J

James Hahn

You could try to access the string as an array of char using .ToCharArray,
and then process it into another string char by char (eg, discarding invalid
characters up the the result length).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top