473,387 Members | 1,553 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,387 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 4689
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 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.