I have written a C++ DLL with the following function exported
extern "C" BOOL WINAPI SetUserPassword(const char* u, const char* p)
When calling this from C# I am using the following code
[DllImport("mydll.dll")]
private static extern int SetUserPassword(String user, String password);
Now when SetUserPassword is called I only get the first letter of each parameter. I did some googling and found that String was perhaps not the best to use so tried using IntPtr and Marshal.StringToBSTR() but this didn’t work either.
How should this be structured correctly? I am able to modify the C++ DLL if required.
The only thing that makes sense is that your C# code is actually:
When you do this you pass a wide character based string to your C++ code and since it expects single byte characters it interprets the 0 byte in the second half of the first two byte character as a string terminator.
You should write:
Or in fact you could write it exactly as in your question because
Ansiis the default.And in fact it turns out that you are on CE which does not support
Ansicharacter set and so the only reasonable solution is to make your C++ code accept wide character strings.