c lib to c# strings

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

I have made a DLL in a C
One of this function returns a string type:

GetMyTextString(char *txt);
SetMyTextString(char *txt);
But the compiler give me this answer:
error CS0214: Pointers may only be used in an unsafe context

What is the best way of doing this?
I need to send and return a string from a C DLL
 
Or can I use:
GetMyTextString(char txt[1024]);
SetMyTextString(char txt[1024]);

Since I have the control of the lib the string will never exceed 1024 bytes

[DllImport("SerialLIB.dll")]
public static extern long SetMyTextString(char[] buf);

[DllImport("SerialLIB.dll")]
public static extern long GetMyTextString(char[] buf);

???
 
You should pass in an instance System.Text.StringBuilder as it acts as
mutable string.


GTi said:
Or can I use:
GetMyTextString(char txt[1024]);
SetMyTextString(char txt[1024]);

Since I have the control of the lib the string will never exceed 1024
bytes

[DllImport("SerialLIB.dll")]
public static extern long SetMyTextString(char[] buf);

[DllImport("SerialLIB.dll")]
public static extern long GetMyTextString(char[] buf);

???


GTi said:
I have made a DLL in a C
One of this function returns a string type:

GetMyTextString(char *txt);
SetMyTextString(char *txt);
But the compiler give me this answer:
error CS0214: Pointers may only be used in an unsafe context

What is the best way of doing this?
I need to send and return a string from a C DLL
 
Stelrad Doulton said:
You should pass in an instance System.Text.StringBuilder as it acts as
mutable string.

Um... I'm guessing that his C dll is unmanaged code, using pointers into
a buffer, so converting it to use a StringBuilder is a rather massive
change.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
GTi said:
Or can I use:
GetMyTextString(char txt[1024]);
SetMyTextString(char txt[1024]);

Since I have the control of the lib the string will never exceed 1024
bytes

[DllImport("SerialLIB.dll")]
public static extern long SetMyTextString(char[] buf);

[DllImport("SerialLIB.dll")]
public static extern long GetMyTextString(char[] buf);

???


GTi said:
I have made a DLL in a C
One of this function returns a string type:

GetMyTextString(char *txt);
SetMyTextString(char *txt);
But the compiler give me this answer:
error CS0214: Pointers may only be used in an unsafe context

What is the best way of doing this?
I need to send and return a string from a C DLL
 
James Curran said:
Stelrad Doulton said:
You should pass in an instance System.Text.StringBuilder as it acts as
mutable string.

Um... I'm guessing that his C dll is unmanaged code, using pointers
into
a buffer, so converting it to use a StringBuilder is a rather massive
change.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
GTi said:
Or can I use:
GetMyTextString(char txt[1024]);
SetMyTextString(char txt[1024]);

Since I have the control of the lib the string will never exceed 1024
bytes

[DllImport("SerialLIB.dll")]
public static extern long SetMyTextString(char[] buf);

[DllImport("SerialLIB.dll")]
public static extern long GetMyTextString(char[] buf);

???


"GTi" <[email protected]> skrev i melding
I have made a DLL in a C
One of this function returns a string type:

GetMyTextString(char *txt);
SetMyTextString(char *txt);
But the compiler give me this answer:
error CS0214: Pointers may only be used in an unsafe context

What is the best way of doing this?
I need to send and return a string from a C DLL

I solved my problem to only return a int.
Actual I want to reading a string from a serial port, decoding it and get a
int value.
Instead of decoding it in C# I decode it in my C DLL and return the int
value only.
 
GTi said:
I solved my problem to only return a int.
Actual I want to reading a string from a serial port, decoding it and get a
int value.

Ya'know.... That's what I thought that C dll was doing, reading from a
serial port. I would suggest that it might be easier just to move
everything into C#, but I couldn't find a simple mean of reading a serial
port from C#.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
James Curran said:
GTi said:
I solved my problem to only return a int.
Actual I want to reading a string from a serial port, decoding it and get a
int value.

Ya'know.... That's what I thought that C dll was doing, reading from a
serial port. I would suggest that it might be easier just to move
everything into C#, but I couldn't find a simple mean of reading a serial
port from C#.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

serial is NOT included with .NET 1.0/1.1
I have no answer why it is not included (forgetfulness)..
But it will be included in .NET 2.0

I can't wait until then, so this is a ramp function...
 
Back
Top