convert a C# string to a unmanaged C char

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I have to access a third party api to work with their
hardware. Everything was going great UNTIL I ran into the
problem of C# char being 16 bits and the C char being 8
bits. I cannot seem to cast the string properly to use it
in the unmanaged code.

I have tried to use the MarshalAs function in my DllImport
statement but that didn't work.

What i need is to take a string of numbers and convert it
to a Zero Terminated C string or char. Can anyone please
help me.
 
Jim said:
I have to access a third party api to work with their
hardware. Everything was going great UNTIL I ran into the
problem of C# char being 16 bits and the C char being 8
bits. I cannot seem to cast the string properly to use it
in the unmanaged code.

You shouldn't have to do anything, it should just marshal accross as an 8 bit null terminated char* that will work in C. eg

[DllImport("user32", EntryPoint= "MessageBoxA")]
private static extern int MessageBox(IntPtr hwnd, String lpText, String lpCaption, int wType);
 
What do you get if you specify CharSet=CharSet.Auto in the DllImport
declaration?

HabibH.
 
-----Original Message-----


You shouldn't have to do anything, it should just marshal
accross as an 8 bit null terminated char* that will work
in C. eg
[DllImport("user32", EntryPoint= "MessageBoxA")]
private static extern int MessageBox(IntPtr hwnd, String
lpText, String lpCaption, int wType);
--
Michael Culley





.


I have read that a string should marshal as a char* but
for some reason mine is not doing it.
this is my dll declaration
[DllImport("PikaApi.dll")]
public static extern int PK_VP_PlayDMTFString(uint hPort,
string PK_PCHAR);

then I call it from my code
string x = textBox1.text;
if(PK_VP_PlayDMTFString(hBoardPort, x) == PK_SUCCESS)

but it doesn't work.

any ideas?
 
I have read that the string will marshal over to a char*
but for some reason it didn't owrk for me.

here is my DllImport code

[DllImport("PikaAPI.dll")]
public static extern uint PK_VP_PlayDTMFString(uint
BoardPort, string PK_PCHAR);

then I call it using

string x = textBox1.text;
PK_VP_PlayDTMFString(hPort, x);

but not luck. any ideas?


-----Original Message-----


You shouldn't have to do anything, it should just marshal
accross as an 8 bit null terminated char* that will work
in C. eg
[DllImport("user32", EntryPoint= "MessageBoxA")]
private static extern int MessageBox(IntPtr hwnd, String
lpText, String lpCaption, int wType);
 
I just tried this code and it worked ok. Try this on your system and see what you get. Maybe there is a global option that causes it
to marshal as unicode or maybe it thinks your function is unicode because it doesn't end in a capital A. Try using
CharSet=CharSet.Ansi

[DllImport("user32", EntryPoint= "MessageBoxA")]
private static extern int MessageBoxAPI(IntPtr hwnd, string lpText, string lpCaption, int wType);

string x = "This is a test!";
string y = "Test caption!";
MessageBoxAPI(this.Handle, x, y, 0);

--
Michael Culley


jim said:
I have read that the string will marshal over to a char*
but for some reason it didn't owrk for me.

here is my DllImport code

[DllImport("PikaAPI.dll")]
public static extern uint PK_VP_PlayDTMFString(uint
BoardPort, string PK_PCHAR);

then I call it using

string x = textBox1.text;
PK_VP_PlayDTMFString(hPort, x);

but not luck. any ideas?


-----Original Message-----


You shouldn't have to do anything, it should just marshal
accross as an 8 bit null terminated char* that will work
in C. eg
[DllImport("user32", EntryPoint= "MessageBoxA")]
private static extern int MessageBox(IntPtr hwnd, String
lpText, String lpCaption, int wType);
--
Michael Culley





.
 
Back
Top