473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6737
"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 identify the most correct and efficient way to do it....
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 external function in a dll that accepts parameter...
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 eventual aim is to get it into a pre-allocated,...
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() = appel.GetIDAsVariant("comm/datamodem") The...
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 code). I get the following compilation error:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.