473,473 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting C# string to char*

i'm calling a C++ DLL from C#.
here's C++ interface:
char* __cdecl SendCmd(void* handle, char* cmd, char* data, char* buffer);

here's my call in C#:
[DllImport("somedll.dll", CharSet = CharSet.Auto, EntryPoint = "SendCmd")]
unsafe public static extern byte* SendCmd
(void* hReader,
[MarshalAs(UnmanagedType.LPArray)] byte[] cmd,
[MarshalAs(UnmanagedType.LPArray)] byte[] data,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer);

and the call is:
ReaderDLL.SendCommandGetData
(rdrHandle,
Encoding.ASCII.GetBytes("antenna off"),
Encoding.ASCII.GetBytes(""),
respBuffer);

the question is: when i pass the byte array to the DLL, (DLL expects
null-terminated char array) does it see a null-terminated char array?

in other words, GetBytes changes the string to byte array. how do i make
sure the byte array is null-terminated as expected by the dll?

thanks
Mar 28 '07 #1
3 11925
>in other words, GetBytes changes the string to byte array. how do i make
>sure the byte array is null-terminated as expected by the dll?
You add an explicit null character at the end of the string.

Encoding.ASCII.GetBytes("antenna off\0"),

But an easier solution would be to simpliy change the parameter type
from byte[] to string (for input params) or StringBuilder (for output
buffers).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 28 '07 #2

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:OW**************@TK2MSFTNGP02.phx.gbl...
in other words, GetBytes changes the string to byte array. how do i make
sure the byte array is null-terminated as expected by the dll?

You add an explicit null character at the end of the string.

Encoding.ASCII.GetBytes("antenna off\0"),
Better yet, avoid strings entirely, since the DLL probably recognizes
commands based on a particular sequence of bytes rather than any stringy
parsing:

ReaderDLL.SendCommandGetData
(rdrHandle,
new byte[] { 0x61, 0x6e, 0x74, 0x65, 0x6e, 0x6e, 0x61, 0x20, 0x6f, 0x66,
0x66, 0 }, // "antenna off"
new byte[] { 0 },
respBuffer);

The unicode->ansi conversion is just an extra chance for things to get
messed up.
>
But an easier solution would be to simpliy change the parameter type
from byte[] to string (for input params) or StringBuilder (for output
buffers).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 30 '07 #3
Ben,
>Better yet, avoid strings entirely,
Well, from a readability and maintainability perspective I'd have to
disagree. I don't want to hunt down an ASCII table just to change the
command to "antenna on" or something.

>The unicode->ansi conversion is just an extra chance for things to get
messed up.
As long as the commands only contain characters in the ASCII set it
shouldn't cause any problems.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.