472,328 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 6681
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to...
3
by: keith | last post by:
In managed C++, there is variable String *s. The variable got value from a C# assembly. Then I need to convert it into char *c in order to call an...
1
by: Bonj | last post by:
HI In keeping with my policy of always using _TCHAR*s when possible, I am trying to convert a System::String* to a _TCHAR*. When I use this (my...
1
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() =...
0
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

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.