首页 新闻 搜索 专区 学院

在VB.NET中调用C++编写的Dll中的函数遇到AccessViolationException,

0
[已解决问题] 解决于 2008-02-03 12:34
我在C++中编写了一个Dll,其中函数的原型是: <BR><BR> <DL class=code> <DT>C/C++ code <DD><PRE><DIV><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><SPAN style="COLOR: #0000ff">extern</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">C</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> _declspec(dllexport) BOOL GetTrayText(__out </SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">); </SPAN></DIV> </PRE></DD></DL><BR><BR>在VB.NET中声明这个外部函数: <BR><BR> <DL class=code> <DT>C# code <DD><PRE><DIV><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">DllImport(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">GetTrayInfo</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> Public Function GetTrayText(ByRef TrayText As Char(), ByVal index As Integer) As Boolean End Function</SPAN></DIV> </PRE></DD></DL><BR><BR><BR>程序中的调用: <BR><BR>&nbsp; &nbsp; &nbsp; .......... <BR>&nbsp; &nbsp; If &nbsp; GetTrayText(tmpStr, &nbsp; i) &nbsp; Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(i &nbsp; &amp; &nbsp; " &nbsp; : &nbsp; " &nbsp; &amp; &nbsp; tmpStr) <BR>&nbsp; &nbsp; End &nbsp; If <BR>&nbsp; &nbsp; &nbsp; .......... <BR><BR>结果出现AccessViolationException如下: <BR><BR>Unhandled &nbsp; Exception: &nbsp; System.AccessViolationException: &nbsp; Attempted &nbsp; to &nbsp; read &nbsp; or &nbsp; write <BR>&nbsp; protected &nbsp; memory. &nbsp; This &nbsp; is &nbsp; often &nbsp; an &nbsp; indication &nbsp; that &nbsp; other &nbsp; memory &nbsp; is &nbsp; corrupt. <BR>&nbsp; &nbsp; &nbsp; at &nbsp; UseDll.Module1.GetTrayText(Char[]&amp; &nbsp; TrayText, &nbsp; Int32 &nbsp; index) <BR>&nbsp; &nbsp; &nbsp; at &nbsp; UseDll.Module1.Main() &nbsp; in &nbsp; F:\aaaSoft\GetTrayInfo\UseDll\Module1.vb <BR>:line &nbsp; 17
cloudgamer的主页 cloudgamer | 菜鸟二级 | 园豆:280
提问于:2008-01-26 01:40
< >
分享
最佳答案
0
你试试将Char数组换成 String 看看 因为.NET内置处理字符是UniCode的,而C可能是ANSI。需要转换。但你传入那个实际上是一个指针, 但这个内群区域还是.NET内部受保护区域。
G yc {Son of VB.NET} | 老鸟四级 |园豆:2006 | 2008-02-03 00:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册