473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StringBuilder vs unsigned char*

Hy
I have a litle problem :

I have to invoke a function in an unmanaged dll (writen in C) from C#
------------------------------------------------

int string_modify(unsigned char* myparameter)
--------------------------------------------------

how to declare this function in C# ?

I tried
--------------------------------
[DllImport("engip11bridge.dll")]

public static extern int string_modify(

StringBuilder testString

);

-----------------------------------------------

but the StringBuilder class is null terminated, while myparameter may
contain some nulls and in this case I lose part of the buffer .
Any suggestion ??

Andrea
Nov 15 '05 #1
5 7128
Andrea,

Your declaration needs a little tweaking. It should be:

[DllImport("engip11bridge.dll")]
public static extern int string_modify(
[MarshalAs(UnmanagedType.LPStr)] StringBuilder testString);

This will make sure that the string is converted properly to an ASCII
representation.

I don't understand what you mean by you may lose part of the buffer. I
believe that all of the characters in the buffer will be passed (assuming
they have been set) when it is marshaled through, not just up to the first
null character.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hy
I have a litle problem :

I have to invoke a function in an unmanaged dll (writen in C) from C#
------------------------------------------------

int string_modify(unsigned char* myparameter)
--------------------------------------------------

how to declare this function in C# ?

I tried
--------------------------------
[DllImport("engip11bridge.dll")]

public static extern int string_modify(

StringBuilder testString

);

-----------------------------------------------

but the StringBuilder class is null terminated, while myparameter may
contain some nulls and in this case I lose part of the buffer .
Any suggestion ??

Andrea

Nov 15 '05 #2
Nicholas,
When the .NET marshals a String, it stops at the first null char.

Andrea indicated that the string being passed "contain some nulls". I
believe Andrea will need to use an IntPtr to pass the buffer, however I do
not have a complete example for Andrea to follow.

Hope this helps
Jay

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ou**************@tk2msftngp13.phx.gbl...
Andrea,

Your declaration needs a little tweaking. It should be:

[DllImport("engip11bridge.dll")]
public static extern int string_modify(
[MarshalAs(UnmanagedType.LPStr)] StringBuilder testString);

This will make sure that the string is converted properly to an ASCII
representation.

I don't understand what you mean by you may lose part of the buffer. I believe that all of the characters in the buffer will be passed (assuming
they have been set) when it is marshaled through, not just up to the first
null character.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hy
I have a litle problem :

I have to invoke a function in an unmanaged dll (writen in C) from C#
------------------------------------------------

int string_modify(unsigned char* myparameter)
--------------------------------------------------

how to declare this function in C# ?

I tried
--------------------------------
[DllImport("engip11bridge.dll")]

public static extern int string_modify(

StringBuilder testString

);

-----------------------------------------------

but the StringBuilder class is null terminated, while myparameter may
contain some nulls and in this case I lose part of the buffer .
Any suggestion ??

Andrea


Nov 15 '05 #3
I solved my problem using a byte[] instead of a StringBuilder

Andrea

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> ha scritto nel
messaggio news:OK****************@TK2MSFTNGP09.phx.gbl...
Nicholas,
When the .NET marshals a String, it stops at the first null char.

Andrea indicated that the string being passed "contain some nulls". I
believe Andrea will need to use an IntPtr to pass the buffer, however I do
not have a complete example for Andrea to follow.

Hope this helps
Jay

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ou**************@tk2msftngp13.phx.gbl...
Andrea,

Your declaration needs a little tweaking. It should be:

[DllImport("engip11bridge.dll")]
public static extern int string_modify(
[MarshalAs(UnmanagedType.LPStr)] StringBuilder testString);

This will make sure that the string is converted properly to an ASCII representation.

I don't understand what you mean by you may lose part of the buffer.

I
believe that all of the characters in the buffer will be passed (assuming they have been set) when it is marshaled through, not just up to the first null character.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hy
I have a litle problem :

I have to invoke a function in an unmanaged dll (writen in C) from C#
------------------------------------------------

int string_modify(unsigned char* myparameter)
--------------------------------------------------

how to declare this function in C# ?

I tried
--------------------------------
[DllImport("engip11bridge.dll")]

public static extern int string_modify(

StringBuilder testString

);

-----------------------------------------------

but the StringBuilder class is null terminated, while myparameter may
contain some nulls and in this case I lose part of the buffer .
Any suggestion ??

Andrea



Nov 15 '05 #4
Adrea,
Doh!

Of course a byte array would also work! Or even a Char array if you know the
function you are calling uses Unicode "strings".

The "sample" I was looking at when I wrote my message used an IntPtr to read
a string from that contains nulls (from the GetPrivateProfileSectionNames
API). The book did not include a sample of writing strings with nulls.

The book I am referencing is Adam Nathan's ".NET and COM - The Complete
Interoperability" from SAMS press.

Hope this helps
Jay

"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
I solved my problem using a byte[] instead of a StringBuilder

Andrea

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> ha scritto nel
messaggio news:OK****************@TK2MSFTNGP09.phx.gbl...
Nicholas,
When the .NET marshals a String, it stops at the first null char.

Andrea indicated that the string being passed "contain some nulls". I
believe Andrea will need to use an IntPtr to pass the buffer, however I do
not have a complete example for Andrea to follow.

Hope this helps
Jay

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:Ou**************@tk2msftngp13.phx.gbl...
Andrea,

Your declaration needs a little tweaking. It should be:

[DllImport("engip11bridge.dll")]
public static extern int string_modify(
[MarshalAs(UnmanagedType.LPStr)] StringBuilder testString);

This will make sure that the string is converted properly to an ASCII representation.

I don't understand what you mean by you may lose part of the buffer. I
believe that all of the characters in the buffer will be passed (assuming they have been set) when it is marshaled through, not just up to the first null character.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> Hy
> I have a litle problem :
>
> I have to invoke a function in an unmanaged dll (writen in C) from

C# > ------------------------------------------------
>
> int string_modify(unsigned char* myparameter)
> --------------------------------------------------
>
> how to declare this function in C# ?
>
> I tried
> --------------------------------
> [DllImport("engip11bridge.dll")]
>
> public static extern int string_modify(
>
> StringBuilder testString
>
> );
>
> -----------------------------------------------
>
> but the StringBuilder class is null terminated, while myparameter may > contain some nulls and in this case I lose part of the buffer .
> Any suggestion ??
>
> Andrea
>
>



Nov 15 '05 #5


Andrea,
Could you post an example of the DllImport statement and possibly how
you converted byte[] to a string?

Thank you,
Mick

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6

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

Similar topics

3
31485
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
11
18240
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
2
7373
by: Peter | last post by:
Hi, A newbie question .. I want to use an array of length 4 while each array element is a string of 40 chars. I typed .. StringBuilder title = new StringBuilder(40);
15
2775
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
3
41895
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
8
2250
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,...
5
7765
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
7
2409
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...
29
9925
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7202
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
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
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...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.