从非托管C ++ EXE消耗托管C#DLL-无法启动CLR主机

问题描述:

你好,
这是我要运行的代码:

Hello,
Here is the code I''m trying to run:

HRESULT hr = S_OK;
ICLRMetaHost    *m_pMetaHost = NULL;
ICLRRuntimeInfo *m_pRuntimeInfo = NULL;
ICorRuntimeHost  *m_pHost = NULL;
       
//CorBindToRuntimeEx is deprecated, try this code instead
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*) &m_pMetaHost);
if (hr != S_OK) 
	return hr;
hr = m_pMetaHost->GetRuntime (DOTNETVERSION, IID_ICLRRuntimeInfo, (LPVOID*) &m_pRuntimeInfo);
if (hr != S_OK)
	return hr;
hr = m_pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*) &m_pHost );
       if (FAILED(hr)) return hr;
        
//Start the CLR
hr = m_pHost->Start();


尝试执行m_pHost-> Start()时,程序崩溃.

在输出窗口中,我遇到了第一次机会异常和访问冲突:
在sidebar.exe中的0x7c812afb处出现第一次机会异常:0x04242420:0x4242420.
``sidebar.exe'':已加载``C:\ WINDOWS \ assembly \ NativeImages_v4.0.30319_32 \ mscorlib \ 246f1a5abb686b9dcdf22d3505b08cea \ mscorlib.ni.dll''
''sidebar.exe'':已加载''C:\ WINDOWS \ Microsoft.NET \ Framework \ v4.0.30319 \ Culture.dll''
''sidebar.exe'':已加载''C:\ WINDOWS \ Microsoft.NET \ Framework \ v4.0.30319 \ nlssorting.dll''
sidebar.exe中0x79168cef处的首次机会异常:0xC0000005:访问冲突读取位置0xc6c5fb37.
线程``Win32线程''(0x930)已退出,代码为-2146233082(0x80131506).
线程``Win32线程''(0x67c)已退出,代码为-2146233082(0x80131506).
线程``Win32线程''(0xdfc)已退出,代码为-2146233082(0x80131506).
程序"[0x508] sidebar.exe:本机"已退出,代码为-2146233082(0x80131506).

任何想法将不胜感激.


问候,
Mike


The program crashed upon trying to execute m_pHost->Start().

In the output window, I''m getting first-chance exceptions and an access violation:
First-chance exception at 0x7c812afb in sidebar.exe: 0x04242420: 0x4242420.
''sidebar.exe'': Loaded ''C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\mscorlib\246f1a5abb686b9dcdf22d3505b08cea\mscorlib.ni.dll''
''sidebar.exe'': Loaded ''C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Culture.dll''
''sidebar.exe'': Loaded ''C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll''
First-chance exception at 0x79168cef in sidebar.exe: 0xC0000005: Access violation reading location 0xc6c5fb37.
The thread ''Win32 Thread'' (0x930) has exited with code -2146233082 (0x80131506).
The thread ''Win32 Thread'' (0x67c) has exited with code -2146233082 (0x80131506).
The thread ''Win32 Thread'' (0xdfc) has exited with code -2146233082 (0x80131506).
The program ''[0x508] sidebar.exe: Native'' has exited with code -2146233082 (0x80131506).

Any ideas would be appreciated.


Regards,
Mike

好,我想我发现了问题:
OK, I think I found the problem:
ICorRuntimeHost  *m_pHost = NULL;


应该更改为


should be changed to

ICLRRuntimeHost  *m_pHost = NULL;


此方法有一种完全不同的替代方法.这直接将.NET方法暴露给非托管方法.许多人会告诉你这是不可能的,但是事实并非如此.对于C#,C#/CLI和VB.NET,这是不可能的,但在IL中是可能的并且已标准化.实际上,您无需了解IL,有一种解决方案可将非托管导出自动添加到已编译的程序集.我从下面引用的一些CodeProject文章中知道这一点.

请查看我过去的解决方案以获取更多信息: ^ ].

—SA
There is a radically different alternative to this approach. This is directly exposing a .NET methods to the unmanaged. Many would tell you it is impossible, but this is not true. This is impossible for C#, C#/CLI and VB.NET, but possible and standardized in IL. You don''t need to know IL in fact, there is a solution automatically adding unmanaged export to your compiled assembly. I know it from some CodeProject articles referenced below.

Please see my past solution for more information: How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^].

—SA