C#中的指针声明

问题描述 投票:1回答:1

这可能是一个愚蠢的初学者问题,但我不明白。我有一个声明一个函数的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中返回的参数的数据?

谢谢!

c# pointers dll
1个回答
2
投票

那么,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);  
...
© www.soinside.com 2019 - 2024. All rights reserved.