关于C#调用C++DLL的问题,请高手帮忙解决一下

gvb2011 2011-06-08 09:11:51
/**
* 调用ws函数
* wsdlURL ws的url地址
* inxml 入参,传入的ws xml消息
* outxml 出参,传出的ws xml消息
**/
extern "C" _declspec(dllexport) int _stdcall invokeWS(char* wsdlURL, char* inxml, char* outxml);
DLL文件函数型
outxml为指针变量
我在C#中用以下调用方法
[DllImport("C:\\Windows\\System32\\csi_ws.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int invokeWS( string wsdlURL, string inxml, ref string outxml);
运行时提示
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
请问应该怎才能正确调用呢?
谢谢!
...全文
45 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gvb2011 2011-06-08
  • 打赏
  • 举报
回复
问题解决了
从以下的说明中学到的希望可以帮到相同问题的朋友
c++ dll中的原型
int test(char* xm,char* fa,UINT &VerNum,double Mile,char *SurvMile);
c#调用时
[DllImport(@"Test2.DLL")]
public static extern int test(string xm,string fa,ref UInt32 VerNum,double Mile, StringBuilder SurvMile);


注意:
1.调用的时候,有部分char* ,如果想获得返回值,不能用string 作参数来进行调用,这样得不到返回到结果,可以用StringBuilder来声明变
StringBuilder strMyTemp = new StringBuilder(256);//256是长度
2.结构体的引用传递
首先在c#中定义和c++相同的结构体,如果是引用传递,在结构体前面加上[In, Out]
[DllImport(@"test.dll")]
public static extern int test([In, Out] SLineData[] lndt,ref UInt32 length);
3.其他的类型如整形等等用ref加上数据变量则可获得返回值

gvb2011 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bdmh 的回复:]
你先确认,那两个指针,是字符串,还是指向的数组
[/Quote]
outxml为指针变量是字符串
gvb2011 2011-06-08
  • 打赏
  • 举报
回复
我想得到outxml返回的值
那我执行这个函数的语句应该怎么写呢
int ret=invokeWS(这里应该样写啊?)
bdmh 2011-06-08
  • 打赏
  • 举报
回复
你先确认,那两个指针,是字符串,还是指向的数组
xuexiaodong2009 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gvb2011 的回复:]

请问怎样用调用,不明白
[/Quote]他是说,你的数据类型对应不正确,他把类型替你改正了
gvb2011 2011-06-08
  • 打赏
  • 举报
回复
请问怎样用调用,不明白
flyerwing 2011-06-08
  • 打赏
  • 举报
回复
试下这个


public static extern int invokeWS(System.IntPtr wsdlURL, System.IntPtr inxml, System.IntPtr outxml) ;

111,101

社区成员

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

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

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