CString conversion

A

Alex Feinman [MVP]

Nothing. You cannot directly marshal CString to managed code.
You need to write a C++ wrapper that would receive CString and copy it ito
a managed buffer (StringBuilder, declared downstream as LPTSTR)
 
A

Alex Feinman [MVP]

Could you please adjust timezone settings on your machine? You are posting
in the future
 
J

jayderk

here is what I created to convert a C++ char[] to a C# string.
Hope this helps



byte[] barcodeBuff = new byte[40];

string correctString = convertDllData(barcodeBuff);

public static string convertDllData(byte[] mybuffer){
StringBuilder temp = new StringBuilder();
int counter = 0;

for(int i = 0; i < mybuffer.Length; i++){
if(mybuffer != 0x00)
{
temp.Append( Convert.ToChar(mybuffer));
counter++;
}
else{
break;
}

}

return temp.ToString();
}
 
E

Emre Dincer

i am trying to P/Invoke a function which returns type CString when i try to
assign String type in .net to CString i get a native exception.

Does anyone know to which type CString corresponds to?
regards
emre dincer
 
J

Jon Skeet

jayderk said:
here is what I created to convert a C++ char[] to a C# string.
Hope this helps

byte[] barcodeBuff = new byte[40];

string correctString = convertDllData(barcodeBuff);

public static string convertDllData(byte[] mybuffer){
StringBuilder temp = new StringBuilder();
int counter = 0;

for(int i = 0; i < mybuffer.Length; i++){
if(mybuffer != 0x00)
{
temp.Append( Convert.ToChar(mybuffer));
counter++;
}
else{
break;
}

}

return temp.ToString();
}


I believe that's exactly equivalent to:

Encoding.GetEncoding (28591).GetString(mybuffer);

However, it may well not be what is actually wanted - it depends on
what encoding is being used by C++. I suspect it's more likely that the
encoding you want is the default one for the system, in which case you
should use

Encoding.Default.GetString(mybuffer);

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top