p/invoke problem with string containing utf-8 characters

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
 
P

Peter Bladh

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
 
M

Miguel

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
 

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