C# ICLRRuntimeHost::ExecuteInDefaultAppDomain无法执行应用程序
我已使用此代码运行预编译的C#应用程序:C# ICLRRuntimeHost::ExecuteInDefaultAppDomain无法执行应用程序,c#,c++,clr,C#,C++,Clr,我已使用此代码运行预编译的C#应用程序: ICLRMetaHost *p_meta_host = nullptr; ICLRRuntimeInfo *p_runtime_info = nullptr; ICLRRuntimeHost *p_clr_runtime_host = nullptr; CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&p_meta_host)); p_meta_host->GetRuntime(L"v4.
ICLRMetaHost *p_meta_host = nullptr;
ICLRRuntimeInfo *p_runtime_info = nullptr;
ICLRRuntimeHost *p_clr_runtime_host = nullptr;
CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&p_meta_host));
p_meta_host->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&p_runtime_info));
p_runtime_info->GetInterface(CLSID_CLRRuntimeHost,
IID_PPV_ARGS(&p_clr_runtime_host));
p_clr_runtime_host->Start();
auto hr = p_clr_runtime_host->ExecuteInDefaultAppDomain(
L"C:\\somefolder\\test.exe",
L"InjectExample.Program",
L"EntryPoint",
L"hey!",
&p_return_value);
执行后,hr
不S\u正常
和GetLastError()
返回ERROR\u文件未找到
关键是文件肯定存在于
'C:\\somefolder\\test.exe'
知道为什么会这样吗?
--UPD。在调试日志中发现:
'NetLoader.exe' (Win32): Loaded 'C:\somefolder\test.exe'.
'NetLoader.exe' (Win32): Loaded 'C:\somefolder\test.exe'.
'NetLoader.exe' (Win32): Unloaded 'C:\somefolder\test.exe'
问题已解决。显然,
hr
等于COR_E_______________________________________
这意味着我试图使用错误的方法。
有关返回代码的更多信息,请参见:
您必须改进错误检查。使用GetLastError()是不正确的,该函数只能在Win32 api调用失败后使用。HRESULT返回值提供错误代码,大多数托管异常将转换为0x1813xxx。使用IErrorInfo了解有关引发的特定异常的更多信息,请注意无法获取神圣堆栈跟踪。明智的做法是使用调试器,以便您可以查看这些异常并检查堆栈跟踪,将调试器类型更改为“混合”。这两种方法都不能很好地替代应用程序本身正确报告未处理的异常。订阅AppDomain.CurrentDomain.UnhandledException事件从来都不是可选的。