how translate UNICODE to ANSI

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to send a string from my DLL( in eVC 4.0)
to a C# application.

I have declared the string as "char *someString".
for example,

********code in my DLL*******

char *someString;
someString = (GetString)(...); // GetString is a pointer to a function in a
// third party DLL. This
function returns a char *.

SendMessage(,, (LPARAM)someString);

***********code in my DLL ends****************

######## code in my C# application########
unsafe
{
string str = new string((char *)m.LParam.ToPointer());
}
MessageBox.Show(str);

######## code in my C# application ends here ########

in my C# application, MessageBox is
showing some symbols rather than the actual string. I presume that
the string which is sent by the DLL might be in UNICODE.

I want to display the actual string, How can I do this.

Kindly let me know.

Cheers,

Naveen.
 
Well, the easiest thing to do would be using Unicode in your C++ module. As
for conversions, there are several ways to approach this:

StringBuilder sb = new StringBuilder();
int pos = 0;
byte ch = Marshal.ReadByte( m.LParam, pos++ );
while( ch != 0 )
{
sb.Append(ch);
ch = Marshal.ReadByte( m.LParam, pos++ );
}

Of course you will need to guarantee that the trailing zero is there - you
cannot catch unmanaged exception from the managed code.
Ideally, you would want to pass string length as wParam.

Even better way to handle this would be to use message as the signal for the
application to make a call into the DLL passing a byte buffer, which DLL
will fill with the string. Then you would use Encoding.ASCII.GetString to
convert the buffer contents to string
 
Hi,

I have tried the second approach.. passing a byte[] in to my C dll(eVC).
please go thorough my code..

----------- My managed code ----------------

char[] buf = new char[bufferSize+1];
byte[] buffer = Encoding.ASCII.GetBytes(buf); // or can I just send a
byte[]. I mean
// byte[]
buffer = new byte[100].
// byte[]
result = GetString(buffer).

byte[] result = GetString(buffer); // method in my DLL.
string s = Encoding.ASCII.GetString(result,0,result.Length);

------------- My unmanaged code in C dll --------------------

char* GetString(char* buffer) // NotSupportedException is occurring here.
{
strcpy(buffer, string);
return buffer;
}

When I run this code.. I am getting "NotSupportedException" at the function
GetString. I'm not sure that how to receive byte[] in
C dll in EVC. How to resolve this problem.

My doubt is, what is a good practice for sending byte[] to C dll and how do
we can we manipulate this buffer in unmanaged code.

Where can I get detailed information about the translation between any two
string representations in .NET Compact Framework/.NET Framework.

Kindly let me know.

Cheers,
Naveen.
 
Back
Top