调用C#dll
调用方法
bool StartTheDotNetRuntimeInDefaultDomain(LPCWSTR runtimeVersion, LPCWSTR dllPath, LPCWSTR startClass, LPCWSTR startMethod, LPCWSTR startArgument) {
ICLRMetaHost* pMetaHost = nullptr;
ICLRRuntimeInfo* pRuntimeInfo = nullptr;
ICLRRuntimeHost* pRuntimeHost = nullptr;
HRESULT result;
DWORD current_appdomain_id = 0;
DWORD ret_code = 0;
result = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
if (SUCCEEDED(result))
{
result = pMetaHost->GetRuntime(runtimeVersion, IID_PPV_ARGS(&pRuntimeInfo));
if (SUCCEEDED(result))
{
BOOL loadable = false;
result = pRuntimeInfo->IsLoadable(&loadable);
if (SUCCEEDED(result) && loadable)
{
result = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost));
if (SUCCEEDED(result))
{
result = pRuntimeHost->Start();
bool bres = FAILED(result);
if (SUCCEEDED(result))
{
result = pRuntimeHost->GetCurrentAppDomainId(¤t_appdomain_id);
result = pRuntimeHost->ExecuteInDefaultAppDomain(dllPath, startClass, startMethod, startArgument, &ret_code);
}
}
}
}
}
if (pRuntimeHost)
{
pRuntimeHost->Release();
}
if (pRuntimeInfo)
{
pRuntimeInfo->Release();
}
if (pMetaHost)
{
pMetaHost->Release();
}
return SUCCEEDED(result);
}
///注:runtimeVersion = "v4.0.30319", .net 4.0 版本号,固定的
///startClass = L"CPtkDomain.CPtkEntrance"; 完整名称
异常及处理