调用C#dll

调用方法

  1. bool StartTheDotNetRuntimeInDefaultDomain(LPCWSTR runtimeVersion, LPCWSTR dllPath, LPCWSTR startClass, LPCWSTR startMethod, LPCWSTR startArgument) {
  2. ICLRMetaHost* pMetaHost = nullptr;
  3. ICLRRuntimeInfo* pRuntimeInfo = nullptr;
  4. ICLRRuntimeHost* pRuntimeHost = nullptr;
  5. HRESULT result;
  6. DWORD current_appdomain_id = 0;
  7. DWORD ret_code = 0;
  8. result = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
  9. if (SUCCEEDED(result))
  10. {
  11. result = pMetaHost->GetRuntime(runtimeVersion, IID_PPV_ARGS(&pRuntimeInfo));
  12. if (SUCCEEDED(result))
  13. {
  14. BOOL loadable = false;
  15. result = pRuntimeInfo->IsLoadable(&loadable);
  16. if (SUCCEEDED(result) && loadable)
  17. {
  18. result = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost));
  19. if (SUCCEEDED(result))
  20. {
  21. result = pRuntimeHost->Start();
  22. bool bres = FAILED(result);
  23. if (SUCCEEDED(result))
  24. {
  25. result = pRuntimeHost->GetCurrentAppDomainId(&current_appdomain_id);
  26. result = pRuntimeHost->ExecuteInDefaultAppDomain(dllPath, startClass, startMethod, startArgument, &ret_code);
  27. }
  28. }
  29. }
  30. }
  31. }
  32. if (pRuntimeHost)
  33. {
  34. pRuntimeHost->Release();
  35. }
  36. if (pRuntimeInfo)
  37. {
  38. pRuntimeInfo->Release();
  39. }
  40. if (pMetaHost)
  41. {
  42. pMetaHost->Release();
  43. }
  44. return SUCCEEDED(result);
  45. }
  46. ///注:runtimeVersion = "v4.0.30319", .net 4.0 版本号,固定的
  47. ///startClass = L"CPtkDomain.CPtkEntrance"; 完整名称


异常及处理