C#调用Dll动态库问题

chen4297 2014-07-23 04:31:49
extern "C" bool __declspec(dllexport) __stdcall EvideoOpenRoom(BSTR RoomIP, BSTR ServerIP, int iFlag)

C# 如何调用

[DllImportAttribute("SendWineEV.dll")]
public static extern bool EvideoOpenRoom(ref string RoomIP, ref string ServerIP, int DataLen);
我是这样写的,但是测不出来
...全文
201 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen4297 2014-07-24
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
去掉ref试试,或者用stringbuilder试试
试过了都不行; [DllImport("SendWineEV.dll", CharSet=CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool )] public static extern bool EvideoOpenRoom( [MarshalAs(UnmanagedType.BStr)] string RoomIP, [MarshalAs(UnmanagedType.BStr)] string ServerIP, int DataLen); 后来这样 [MarshalAs(UnmanagedType.BStr)] string 试了一下 可以了,可能主要是传入的字符类型不符。
xian_wwq 2014-07-23
  • 打赏
  • 举报
回复
1. CharSet要根据dll内部的约定来定,并不是全是Unicode 2. 只是给个例子说明下dllimport的相关参数,没有说是标准答案。。。
CGabriel 2014-07-23
  • 打赏
  • 举报
回复
引用 2 楼 xian_wwq 的回复:
如果改了stringbuilder还有问题, DllImport相关的属性最好都显式说明下,例如:

[DllImport("mydll", EntryPoint = "MyFunction", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
C# 里面的 string 是 unicode 的,你这里写明了要 CharSet = CharSet.Ansi, 调用肯定失败。 最好的 办法是把 Charset 改成 unicode 的;如果实在不行可以用 Marshal.StringToHGlobalAnsi 把 unicode string 转 ansi
xian_wwq 2014-07-23
  • 打赏
  • 举报
回复
如果改了stringbuilder还有问题, DllImport相关的属性最好都显式说明下,例如:

[DllImport("mydll", EntryPoint = "MyFunction", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
bdmh 2014-07-23
  • 打赏
  • 举报
回复
去掉ref试试,或者用stringbuilder试试

110,536

社区成员

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

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

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