求教调用C++的dll

qzyuanmu 2014-07-09 09:43:19
c#调用 c的 char* CMd5_data(char * str)要怎么写方法 ?

[DllImport(@"C:\Windows\System32\Keys-windows.dll")]
public static extern Char CMd5_data(ref Char str);
...全文
180 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigbaldy 2014-07-09
  • 打赏
  • 举报
回复

[DllImport(@"C:\Windows\System32\Keys-windows.dll")]
public static extern IntPtr CMd5_data(string str);
xian_wwq 2014-07-09
  • 打赏
  • 举报
回复
C#下引用非托管API,关于字符串的参数转换如下: [in]char * ->string [out]char* ->StringBuilder 使用DllImport,除了EntryPoint,大多数情况下 CharSet和CallingConvention参数也需要

 [DllImport("rython_share", EntryPoint = "DBAR_CompValue", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
largeskymengsk 2014-07-09
  • 打赏
  • 举报
回复
[DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.LPStr)] public static extern string CMd5_data([MarshalAs(UnmanagedType.LPStr)] string sb); 加上这个,很老的平台,对字符串处理不一样的。
largeskymengsk 2014-07-09
  • 打赏
  • 举报
回复
如果传入的数据会被修改,就用下面这两个,
 [DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr CMd5_data([MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);

        [DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern string CMd5_data([MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);
如果传入参数不需要修改,就用下面两个。
        [DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr CMd5_data([MarshalAs(UnmanagedType.LPStr)] string sb);

        [DllImport("xxx.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern string CMd5_data([MarshalAs(UnmanagedType.LPStr)] string sb);
返回IntPtr和String是有区别的,依赖于你返回指向所指向内存的申请方式。
qzyuanmu 2014-07-09
  • 打赏
  • 举报
回复
引用 6 楼 u012804018 的回复:
[quote=引用 5 楼 qzyuanmu 的回复:] IntPtr 怎么换成 string啊,求解
http://msdn.microsoft.com/zh-cn/library/system.intptr.aspx[/quote] 调用C++真心不懂,返回是一个c++的指针,再怎么读取啊?
鏡椛氹鈅 2014-07-09
  • 打赏
  • 举报
回复
引用 5 楼 qzyuanmu 的回复:
IntPtr 怎么换成 string啊,求解
http://msdn.microsoft.com/zh-cn/library/system.intptr.aspx
qzyuanmu 2014-07-09
  • 打赏
  • 举报
回复
IntPtr 怎么换成 string啊,求解

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧