调用com接口报错,尝试读写受保护的内存!

wuhongyao3 2015-11-19 10:50:22
现需要调用一个dll组件,给的接口原型
HRESULT GetOperInfo([in]LONG i_ind, [in]LONG i_port, [out] BSTR* s_val, [out,retval] long* Result)

在调用过程中一直提示尝试读写受保护的内存,怀疑参数类型BSTR传入问题。于是尝试使用
string
StringBuilder
byte[]
char[]
都失败

[DllImport("xxx.dll",EntryPoint = "GetOperInfo")]
public static extern void GetOperInfo(long i_ind,long i_port,ref string s_val,ref int Result);
public static extern void GetOperInfo(long i_ind,long i_port,ref StringBuilder s_val,ref int Result);
public static extern void GetOperInfo(long i_ind,long i_port,ref byte[] s_val,ref int Result);
public static extern void GetOperInfo(long i_ind,long i_port,ref char[] s_val,ref int Result);

网上搜了确实用string就可以。但就是调用失败,请求思路或者解决方案。谢谢;



...全文
178 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhongyao3 2015-11-19
  • 打赏
  • 举报
回复
static extern int GetOperInfo(int i_ind,int i_port,StringBuilder s_val,ref int Result); 这样正常了。返回类型int有无都可以正常调用成功。 谢谢。结贴了。
wuhongyao3 2015-11-19
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
static extern int GetOperInfo(int i_ind,int i_port,ref StringBuilder s_val,ref int Result);
不好意思,刚刚在测试。 StringBuilder 不用 ref ,用的话直接挂掉。 原来一直纠结是BSTR有问题。原来 long 和返回值的问题啊。 方向错了。 我再试试其他的,待会过来结贴。
Poopaye 2015-11-19
  • 打赏
  • 举报
回复
3楼我说错了,s_val应该还是ref StringBuilder s_val
Poopaye 2015-11-19
  • 打赏
  • 举报
回复
static extern int GetOperInfo(int i_ind,int i_port,ref StringBuilder s_val,ref int Result);
wuhongyao3 2015-11-19
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
public static extern void GetOperInfo(long i_ind,long i_port,ref StringBuilder s_val,ref int Result); 这要成功了才奇怪了
请问 HRESULT 这个在c#里边应该使用什么类型?
Poopaye 2015-11-19
  • 打赏
  • 举报
回复
public static extern void GetOperInfo(long i_ind,long i_port,ref StringBuilder s_val,ref int Result); 这要成功了才奇怪了

110,566

社区成员

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

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

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