问题描述
鉴于此C API声明,如何将其导入C#?
Given this C API declaration how would it be imported to C#?
int _stdcall z4ctyget(CITY_REC *, void *);
我已经可以做到这一点了
I've been able to get this far:
[DllImport(@"zip4_w32.dll",
CallingConvention = CallingConvention.StdCall,
EntryPoint = "z4ctygetSTD",
ExactSpelling = false)]
private extern static int z4ctygetSTD(ref CITY_REC args, void * ptr);
在C#中,自然不会编译"void *".
Naturally in C# the "void *" doesn't compile.
某些Google搜索表示应将其翻译为对象".这似乎应该工作.但是其他人指出"Void *在C/C ++术语中称为函数指针,在C#术语中是委托".这在这里没有太大意义,因为它将委派给什么?通过Googling找到的其他API的一些类似调用使用了相应API中的其他功能.但是在此API中,没有其他调用会有意义.
Some Googling indicates that it should be translated as "object." Which seems like it should work. But others indicate that "Void * is called a function pointer in C/C++ terms which in C# terms is a delegate". That doesn't make a whole lot of sense here as what would it delegate to? Some similar calls for other APIs found through Googling use other functions in the respective API. But in this API no other call would make sense.
该呼叫的文档显示了一个示例:
The documentation for the call shows an example:
z4ctyget(&city, "00000");
似乎表明甚至可以传递静态值.
Which seems to show that even a static value could be passed.
它将用对象代替void *进行编译.我不知道这是否对,而且我还没有机会进行测试(许可问题).
It will compile with object in place of the void *. I don't know whether this is right and I haven't had an opportunity to test it (licensing issue).
推荐答案
对于void *参数,您可以仅使用IntPtr
For the void* parameter you can just use an IntPtr
[DllImport(@"zip4_w32.dll",
CallingConvention = CallingConvention.StdCall,
EntryPoint = "z4ctygetSTD",
ExactSpelling = false)]
private extern static int z4ctygetSTD(ref CITY_REC args, IntPtr ptr);
这篇关于如何将void * C API导入C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!