Calling a DLL with LPSTR parameters

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!
 
P

Peter Foot [MVP]

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
 
S

Sergey Bogdanov

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
 

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