473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Stringbuilder help...

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
Nov 16 '05 #1
3 4687
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

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Sijin Joseph" <si***************@hotmail.com> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Sijin Joseph" <si***************@hotmail.com> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
> 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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
6
by: Jim Heavey | last post by:
Hello, I a m building an email message in a StringBuilder Object and then using the StringBuilder ToString() method to get it to regular text. It appears to me that when I place a "\n" into the...
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
7
by: rhkodiak | last post by:
I was in an interview question what is the most efficient way to sort a string for example "010101", the result would then be "000111", and write the code on how to do this. I wrote the code for...
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.