這是從MSDN文章「CLR內向外:CLR託管的API」,2006年8月
int main(int argc, _TCHAR* argv[])
{
// Bind to the runtime.
ICLRRuntimeHost *pClrHost = NULL;
HRESULT hrCorBind = CorBindToRuntimeEx(
NULL, // Load the latest CLR version available
L"wks", // Workstation GC ("wks" or "svr" overrides)
0, // No flags needed
CLSID_CLRRuntimeHost,
IID_ICLRRuntimeHost,
(PVOID*)&pClrHost);
// Construct our host control object.
DHHostControl *pHostControl = new DHHostControl(pClrHost);
// Notify the CLR that this host implements hosting managers.
pClrHost->SetHostControl(pHostControl);
// Now, start the CLR.
HRESULT hrStart = pClrHost->Start();
// Load an assembly and execute a method in it.
HRESULT hrExecute = pClrHost->ExecuteInDefaultAppDomain(
pwzAssemblyPath, pwzAssemblyName,
pwzMethodName, pwzMethodArgs,
&retVal);
}
還有另一個MSDN「高級技術避免和檢測死鎖在.NET應用程序」,四月,2006年的一篇關於'通過宿主API探索'的部分,這也有助於解釋如何使用API從C/C++代碼託管。
希望這會有所幫助, 最好的問候, 湯姆。
可能重複:http://stackoverflow.com/questions/2216551/loading-managed-dll-into-appdomain-from-native-c-代碼/ 2217034 – 2010-02-13 02:23:41
爲什麼不在編譯器中創建一個迷你加載器來加載.NET運行時宿主環境並加載自定義的.NET例程來爲您進行反射並獲取簽名存根......? – t0mm13b 2010-02-13 02:23:46
@Moron:當我輸入以前的評論時,你說出了我的想法? – t0mm13b 2010-02-13 02:24:24