StringBuilder vs unsigned char*

  • Thread starter Thread starter Andrea
  • Start date Start date
A

Andrea

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
 
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,
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 said:
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]
- (e-mail address removed)




Andrea said:
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
 
I solved my problem using a byte[] instead of a StringBuilder

Andrea

Jay B. Harlow said:
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

message news:[email protected]...
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]
- (e-mail address removed)




Andrea said:
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
 
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 said:
I solved my problem using a byte[] instead of a StringBuilder

Andrea

Jay B. Harlow said:
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

message news:[email protected]...
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]
- (e-mail address removed)




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
 
Andrea,
Could you post an example of the DllImport statement and possibly how
you converted byte[] to a string?

Thank you,
Mick
 
Back
Top