HighTechTalks DotNet Forums  

Unicode to ASCII in C#

Dotnet Framework (Compact Framework) microsoft.public.dotnet.framework.compactframework


Discuss Unicode to ASCII in C# in the Dotnet Framework (Compact Framework) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
srks
 
Posts: n/a

Default Unicode to ASCII in C# - 04-23-2010 , 05:53 AM






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.

Reply With Quote
  #2  
Old   
Paul G. Tobey [eMVP]
 
Posts: n/a

Default RE: Unicode to ASCII in C# - 04-23-2010 , 11:51 AM






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.

"srks" wrote:

Quote:
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.

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.