How to import const char* API to C#?

后端 未结 3 1891
一向
一向 2020-12-31 21:10

Given this C API declaration how would it be imported to C#?

const char* _stdcall z4LLkGetKeySTD(void);

I\'ve been able to get this far:

3条回答
  •  暖寄归人
    2020-12-31 21:36

    Try this

       [DllImport("zip4_w32.dll",
           CallingConvention = CallingConvention.StdCall,
           EntryPoint = "z4LLkGetKeySTD",
           ExactSpelling = false)]
       private extern static IntPtr z4LLkGetKeySTD();
    

    You can then convert the result to a String by using Marshal.PtrToStringAnsi(). You will still need to free the memory for the IntPtr using the appropriate Marshal.Free* method.

提交回复
热议问题