p/invoke problem with string containing utf-8 characters

  • Thread starter Thread starter Peter Bladh
  • Start date Start date
P

Peter Bladh

Hi

I have a problem recieving strings containing non-ascii characters. The
following code sets up the p/invoke:
DllImport("Nccdll.dll", SetLastError = false)]
static extern unsafe int NccWrapperReceive(StringBuilder msg, int size);
The actual function (C) in the dll looks like this:
int NccWrapperReceive(LPWSTR msg, int size)



This usually works, but when the msg parameter contains a character above
0x7E, that character and any following characters "disappear". How do I get
the entire string?

Thanx!
/peter
 
Hi again,

after some more research it seems like the 'mbstowcs' function in the C-dll
doesn't work properly. When it tries to copy the "weird" character it writes
outside the string (where the data should be copied to) at position -1.
Any ideas?

/peter
 
I wouldn't use the stringbuilder type as parameter.
I would use a byte array, let the function fill that byte array and
provide the conversion yourself with the use of the functions in the
System.Text namespace.
That's how I solved all the invoke problems regarding strings.

Kind regards

Miguel
Peter said:
Hi again,

after some more research it seems like the 'mbstowcs' function in the C-dll
doesn't work properly. When it tries to copy the "weird" character it writes
outside the string (where the data should be copied to) at position -1.
Any ideas?

/peter


Peter Bladh said:
Hi

I have a problem recieving strings containing non-ascii characters. The
following code sets up the p/invoke:
DllImport("Nccdll.dll", SetLastError = false)]
static extern unsafe int NccWrapperReceive(StringBuilder msg, int size);
The actual function (C) in the dll looks like this:
int NccWrapperReceive(LPWSTR msg, int size)



This usually works, but when the msg parameter contains a character above
0x7E, that character and any following characters "disappear". How do I
get the entire string?

Thanx!
/peter
 
Back
Top