How to import void * C API into C#?

随声附和 提交于 2019-12-04 03:06:23

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);

You can also use void* if you mark your class as unsafe.

It really depends on what the API is looking for in that parameter.

You can add IntPtr or Object* to get past compiler, but you will still need to pass it the correct data when you call it.

As far as I can tell the C declaration of z4ctyget is:

int z4ctyget(CITY_REC *cityrec, char *zipcode);

The second parameter is a 5 character ANSI string representing the zip code at which you want to start your search or "00000" to start at the beginning of the file. So your declaration should be:

[DllImport(@"zip4_w32.dll", CharSet = CharSet.Ansi)]
private extern static int z4ctygetSTD(ref CITY_REC args, string zipcode);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!