[已解决问题]
解决于 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"><</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">></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> .......... <BR> If GetTrayText(tmpStr, i) Then <BR> Console.WriteLine(i & " : " & tmpStr) <BR> End If <BR> .......... <BR><BR>结果出现AccessViolationException如下: <BR><BR>Unhandled Exception: System.AccessViolationException: Attempted to read or write <BR> protected memory. This is often an indication that other memory is corrupt. <BR> at UseDll.Module1.GetTrayText(Char[]& TrayText, Int32 index) <BR> at UseDll.Module1.Main() in F:\aaaSoft\GetTrayInfo\UseDll\Module1.vb <BR>:line 17