[已解决问题] 在VB.NET中调用C++编写的Dll中的函数遇到AccessViolationException,
提问时间: 2008-01-26 01:40
浏览:954 次
我在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
提问者:cloudgamer - 初学一级
最佳答案
你试试将Char数组换成 String 看看 因为.NET内置处理字符是UniCode的,而C可能是ANSI。需要转换。但你传入那个实际上是一个指针, 但这个内群区域还是.NET内部受保护区域。
2008/2/3 0:34:54 回答者:G yc {Son of VB}
评论
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题



> 问题排行榜

相关内容

相关链接