469,085 Members | 1,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

convert a C# string to a unmanaged C char

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.
Nov 15 '05 #1
7 6562
"Jim" <ji**@kc.rr.com> wrote in message news:07****************************@phx.gbl...
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);

--
Michael Culley
"Jim" <ji**@kc.rr.com> wrote in message news:07****************************@phx.gbl... 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.

Nov 15 '05 #2
What do you get if you specify CharSet=CharSet.Auto in the DllImport
declaration?

HabibH.

"Jim" <ji**@kc.rr.com> wrote in message
news:07****************************@phx.gbl...
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.

Nov 15 '05 #3
-----Original Message-----
"Jim" <ji**@kc.rr.com> wrote in message news:07****************************@phx.gbl...
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);
--
Michael Culley
"Jim" <ji**@kc.rr.com> wrote in message news:07****************************@phx.gbl...
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.

.

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?

Nov 15 '05 #4
-----Original Message-----
What do you get if you specify CharSet=CharSet.Auto in the DllImportdeclaration?

HabibH.

"Jim" <ji**@kc.rr.com> wrote in message
news:07****************************@phx.gbl...
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.

.


I have not tried that.....

Nov 15 '05 #5
jim
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-----
"Jim" <ji**@kc.rr.com> wrote in message news:07****************************@phx.gbl...
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);
--
Michael Culley
"Jim" <ji**@kc.rr.com> wrote in message

news:07****************************@phx.gbl...
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.

.

Nov 15 '05 #6
jim
I have not tried that, I will try it first thing tomorrow.

-----Original Message-----
What do you get if you specify CharSet=CharSet.Auto in the DllImportdeclaration?

HabibH.

"Jim" <ji**@kc.rr.com> wrote in message
news:07****************************@phx.gbl...
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.

.

Nov 15 '05 #7
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" <ji**@kc.rr.com> wrote in message news:0f****************************@phx.gbl...
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-----
"Jim" <ji**@kc.rr.com> wrote in message

news:07****************************@phx.gbl...
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);

--
Michael Culley
"Jim" <ji**@kc.rr.com> wrote in message

news:07****************************@phx.gbl...
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.

.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Kueishiong Tu | last post: by
3 posts views Thread by Maileen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.