C++函数中设置的错误码(SetLastError),C#要及时获取怎么办呢?
【1】C++代码
设置了1010的错误码
EXPORTDLL_API void Err_Get()
{
SetLastError(1010); //注册表项无效的错误码
wprintf(L"Err_Get \n");
}
【2】C#代码:
需要设置配置参数SetLastError=true
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode, SetLastError=true)]
public static extern void Err_Get();
测试,使用Win32Exception(using System.ComponentModel;):
CExportDll.Err_Get();
Win32Exception win32Exp = new Win32Exception();
Console.WriteLine(win32Exp.Message);