Unicode to ASCII in C#

  • Thread starter Thread starter srks
  • Start date Start date
S

srks

Hi i am calling a function in Native c++ .dll from .NetCF 3.5 app

the function takes char * as one of the args.

code snippets:
in C++
int SendResponse(int Bus, char *response_msg, int msg_size)

in C#
[DllImport("NIDeviceWrapper.dll")]
public static extern int SendResponse(int Bus,
StringBuilder response_msg,
int msg_size);

// call to the native code
ResponseSb = new StringBuilder(ResponseString);

// Send the response message through the library interface.
status = cNiDeviceWrapper.SendResponse(cDevice.ActiveBus,
ResponseSb, ResponseSb.Length);

i am getting a pointer to Unicode chars as an argument in the c++ code
rather than a pointer to bytes.
how do i solve this so that i get a pointer to bytes as in C++ code.
 
What I have typically done for this is to use something like this:

string s = "this is a test";
byte[] b = UnicodeEncoding.Unicode.GetBytes(s);

and declare the parameter as a byte array, not a string.

You can also use something like this for marshalling:

[DllImport("mydll.dll")]
public static extern int MyTestC([MarshalAs(UnmanagedType.LPStr)]
string cstring);

Note that in neither case is StringBuilder normally used.

Paul T.
 
Back
Top