Stringbuilder help...

G

Guest

Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter.
The Stringbuilder appears to be working, the problem is that the component
is sending me a string with NULL characters in the middle, and my
StringBuilder is shrinking the message. Example

StringBuilder buffer = new StringBuilder(255);

Win32 sends : VB123421414\0\0\0\0HRA0021331
StringBuilder reads : VB123421414

Is there any way to read the full buffer? The capacity is 255, how can I
read position 240 for example? is raising an error if I put buffer[240]

Thanks!!!!
SAlva
 
N

Nicholas Paldino [.NET/C# MVP]

Sijin and Salvador,

A ref byte won't work because the marshaller doesn't know how much
information to marshal back.

The string builder doesn't work because when marshaling back, it takes
the string terminator ('\0') and stops marshaling at that point.

To get around this, declare the paramter as type IntPtr. Allocate
unmanaged memory using the static methods on the Marshal class. Pass that
pointer, and upon return, pass the pointer to PtrToAnsiString.

This is the important part. Once you do that, you have to advance the
pointer instance past the end of the first '\0' character, and then read the
memory again. You do this until you have read all the content.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sijin Joseph said:
Have you tried passing a ref byte[] to the DLL instead of a stringbuilder,
since the return value has NULL chars in it the StringBuilder will not
interpret the value correctly.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter.
The Stringbuilder appears to be working, the problem is that the
component is sending me a string with NULL characters in the middle, and
my StringBuilder is shrinking the message. Example

StringBuilder buffer = new StringBuilder(255);

Win32 sends : VB123421414\0\0\0\0HRA0021331
StringBuilder reads : VB123421414

Is there any way to read the full buffer? The capacity is 255, how can I
read position 240 for example? is raising an error if I put buffer[240]

Thanks!!!!
SAlva
 
N

Nicholas Paldino [.NET/C# MVP]

Salvador,

Most likely though, you are producing a memory leak (because the message
array is allocated in unmanaged code, but it is never unallocated).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Salvador said:
Hi thanks both for the reply.

Actually, I have tried the IntPtr, the problem is my Win32 DLL crashes
when
I execute it.

I have used byte[] and now is working as follows:

[DllImport("wselink.dll", CharSet = CharSet.Ansi , CallingConvention =
CallingConvention.StdCall)]
private static extern int SolveReceive(int function, [Out] byte[] message,
int size);

Call:
//
// Builds the response
//
byte[] Response = new byte[responseFixedSize];

//
// Retrieve the response
//
int RetrieveResult = SolveReceive(SOLVE_REQ, Response, Response.Length);

if ( RetrieveResult > 0)
{
return System.Text.Encoding.Default.GetString(Response);
}

Thanks for your help!!
Salva

Nicholas Paldino said:
Sijin and Salvador,

A ref byte won't work because the marshaller doesn't know how much
information to marshal back.

The string builder doesn't work because when marshaling back, it
takes
the string terminator ('\0') and stops marshaling at that point.

To get around this, declare the paramter as type IntPtr. Allocate
unmanaged memory using the static methods on the Marshal class. Pass
that
pointer, and upon return, pass the pointer to PtrToAnsiString.

This is the important part. Once you do that, you have to advance
the
pointer instance past the end of the first '\0' character, and then read
the
memory again. You do this until you have read all the content.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sijin Joseph said:
Have you tried passing a ref byte[] to the DLL instead of a
stringbuilder,
since the return value has NULL chars in it the StringBuilder will not
interpret the value correctly.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph



Salvador wrote:
Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter.
The Stringbuilder appears to be working, the problem is that the
component is sending me a string with NULL characters in the middle,
and
my StringBuilder is shrinking the message. Example

StringBuilder buffer = new StringBuilder(255);

Win32 sends : VB123421414\0\0\0\0HRA0021331
StringBuilder reads : VB123421414

Is there any way to read the full buffer? The capacity is 255, how can
I
read position 240 for example? is raising an error if I put
buffer[240]

Thanks!!!!
SAlva
 

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