如何导入为const char * API为C#? [英] How to import const char* API to C#?

查看:100
本文介绍了如何导入为const char * API为C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此C API声明如何将它导入到C#



 为const char * _stdcall z4LLkGetKeySTD(无效)?; 



我已经能够走到这一步:

 函数[DllImport(zip4_w32.dll,
CallingConvention = CallingConvention.StdCall,
入口点=z4LLkGetKeySTD,
ExactSpelling = FALSE) ]
私有外部静态为const char * z4LLkGetKeySTD();


解决方案

试试这个

 函数[DllImport(zip4_w32.dll,
CallingConvention = CallingConvention.StdCall,
入口点=z4LLkGetKeySTD,
ExactSpelling = FALSE)]
私有外部静态的IntPtr z4LLkGetKeySTD();

您可以那么结果通过Marshal.PtrToStringAnsi转换为String()。您仍需要使用适当的Marshal.Free *的方法来免费为IntPtr的内存。


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:

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

解决方案

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.

这篇关于如何导入为const char * API为C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆