CString conversion

  • Thread starter Thread starter Alex Feinman [MVP]
  • Start date Start date
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)
 
Could you please adjust timezone settings on your machine? You are posting
in the future
 
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 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
 
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.
 
Back
Top