这可能是一个愚蠢的初学者问题,但我不明白。我有一个声明一个函数的DLL
int get_state(const unsigned char n,unsigned int *state)
什么是相关的C#import语句?是
public static extern int get_card(byte n,ref uint state);
[DllImport(@"my.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
正确?
使用此函数时,如何调用get_card()
以获取state
中返回的参数的数据?
谢谢!
那么,DllImportAttribute
必须放在它描述的方法之前:
public static class MyClass {
...
// Since you don't use String, StringBuilder etc.
// CharSet = CharSet.Ansi is redundant and can be omitted
[DllImport(@"my.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int get_card(byte n, ref uint state);
...
}
声明了get_card
方法后,您可以像往常一样使用它,就像任何其他方法一样(并且.Net将对参数进行编组):
...
byte n = 123;
uint state = 456;
int result = MyClass.get_card(n, ref state);
...