Calling a DLL with LPSTR parameters

  • Thread starter Thread starter Arnau Font
  • Start date Start date
A

Arnau Font

Hi,

I'm trying to invoke a function in a DLL, using DllImport. One of the
parameters is a ANSI string, but the CharSet enumeration has only the Auto
and Unicode members. How can I send a string in ANSI to the DLL?


[DllImport("Protocol.dll", CharSet = CharSet.Auto)]
static extern int Configure(string password, uint pwdSize); //The password
should be passed as ANSI

Thanks!
 
use:-
[DllImport("Protocol.dll")]
static extern int Configure(byte[] password, uint pwdSize); //The password
should be passed as ANSI

And use System.Text.Encoding.ASCII.GetBytes() to get the byte array for your
string, and pass this into the function.

Peter
 
Why you do not want to modify native part that it would receive UNICODE
string?

If this is impossible, you can try to pass your string as byte array:

byte [] b = System.Text.Encoding.ASCII.GetBytes("Password");
Configure(b, b.Length);

....

[DllImport("Protocol.dll")]
static extern int Configure(byte [] password, int pwdSize);

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Back
Top