How to import const char* API to C#?

后端 未结 3 1889
一向
一向 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:46

    Always use C++ const char* or char* and not std::string.

    Also keep in mind that char in C++ is a sbyte in C# and unsigned char is a byte in C#.

    It is advisable to use unsafe code when dealing with DllImport.

    [DllImport("zip4_w32.dll",
       CallingConvention = CallingConvention.StdCall,
       EntryPoint = "z4LLkGetKeySTD",
       ExactSpelling = false)]
     private extern static sbyte* or byte* z4LLkGetKeySTD();
    
     void foo()
     {
       string res = new string(z4LLkGetKeySTD());
     }
    

提交回复
热议问题