简体   繁体   中英

C DLL. How to import in C#

I've to import a C DLL in C# and have that function:

set_pin (IN char* str_user, IN char* str_old_pin, IN char* str_new_pin)

How to write that in c#? and what's the meaning of the "IN"?

IN means that the parameter is being used to supply data to the method, and is not being used to return data from the method.

Because the data is only being passed TO the method, you can just declare the parameters as string - but you may need to know in which format the method is expecting the strings - are they UTF8, UTF16, ANSI etc?

Once you know the required string format, you can specify it in the P/Invoke declaration for the method (or choose CharSet.Auto , which generally means "ANSI"):

[DllImport("your.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall))]
public static extern bool set_pin(string str_user, string str_old_pin, string str_new_pin);

Just try it with "CharSet.Auto" and don't worry about using something different unless it doesn't work!

Note that you didn't specify the return type of the method, so I've just made it bool as an example.

You may also need to specify the calling convention if it isn't the standard StdCall .

See the calling convention enum for details.

Also see this Microsoft article for documentation on writing P/Invoke specifications .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM