我在C++中编写了一个Dll,其中函数的原型是:
- C/C++ code
extern "C" _declspec(dllexport) BOOL GetTrayText(__out char*,int);
在VB.NET中声明这个外部函数:
- C# code
<DllImport("GetTrayInfo")> Public Function GetTrayText(ByRef TrayText As Char(), ByVal index As Integer) As Boolean
End Function
程序中的调用:
..........
If GetTrayText(tmpStr, i) Then
Console.WriteLine(i & " : " & tmpStr)
End If
..........
结果出现AccessViolationException如下:
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at UseDll.Module1.GetTrayText(Char[]& TrayText, Int32 index)
at UseDll.Module1.Main() in F:\aaaSoft\GetTrayInfo\UseDll\Module1.vb
:line 17