我用C++写的DLL库,然后其中有一行代码是这样的:if( size == 0)  
{
   throw exception("the size if zero, please reselect!");
}如果后台的DLL算法如果出现这个错误,希望由前台C#抛出该提示,try
{
  Dll.functionName(ref a ,ref b ...)
}
catch
{
  ......
}问题如下:
(1)我该如何在dll中编写throw块;
(2)我该如何在C#中编写try catch块;
才能保证在dll所throw出的错误能够显示在C#的前台上?请高手赐教,谢谢!

解决方案 »

  1.   

    try
    {
      Dll.functionName(ref a ,ref b ...);
    }
    catch(Exception ex)
    {
      MessageBox.Show(ex.Message);
    }
      

  2.   

    SetLastError and Marshal.GetLastWin32Error()
     [DllImport("user32.dll", SetLastError=true)]
         public static extern int MessageBoxA(int hWnd, String text, 
                                  String caption, uint type);int i=MessageBoxA(....);
    if (i<=0)
    Console.WriteLine("CloseHandle call failed with an error code of: {0}", 
                Marshal.GetLastWin32Error());
      

  3.   

    throw a .Net exception in your c++ code