1、VC代码:(vs2013运行正常)
// ConsoleApplication_CallCS.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" /* int _tmain(int argc, _TCHAR* argv[]) { return 0; } //*/ /* #include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include <windows.h> #include <metahost.h> #include <mscoree.h> #pragma comment(lib, "mscoree.lib") int _tmain(int argc, _TCHAR* argv[]) { ICLRMetaHost *pMetaHost = nullptr; ICLRMetaHostPolicy *pMetaHostPolicy = nullptr; ICLRRuntimeHost *pRuntimeHost = nullptr; ICLRRuntimeInfo *pRuntimeInfo = nullptr; HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); //hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); hr = pMetaHost->GetRuntime(L"vv2.0.50727", IID_PPV_ARGS(&pRuntimeInfo)); if (FAILED(hr)) { goto cleanup; } hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost)); hr = pRuntimeHost->Start(); DWORD dwRet = 0; //hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"SampleManagedApp.exe", // L"SampleManagedApp.Program", // L"Test", // L"Hello World!", // &dwRet); hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll", L"ClassLibrary4VC.ClassLibraryTest01", L"ClassLibraryFunc01", L"Hello World!", &dwRet); hr = pRuntimeHost->Stop(); cleanup: if (pRuntimeInfo != nullptr) { pRuntimeInfo->Release(); pRuntimeInfo = nullptr; } if (pRuntimeHost != nullptr) { pRuntimeHost->Release(); pRuntimeHost = nullptr; } if (pMetaHost != nullptr) { pMetaHost->Release(); pMetaHost = nullptr; } system("pause"); } //*/ //CorBindToRuntimeEx // https://msdn.microsoft.com/zh-cn/library/99sz37yh.aspx // http://zhidao.baidu.com/link?url=L2NYvtj82SvXjqn6-Abatd6dyK3VdSta_DZAeqGqoRUCDcsvlXrxK-vnMUXq8RJQLSSSlHY1M6RTWLHTzJJSvcuipSwuhGwkFFCWcZBZpGG // http://blog.csdn.net/hadstj/article/details/34823905 #include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include <windows.h> #include <metahost.h> #include <mscoree.h> #pragma comment(lib, "mscoree.lib") int _tmain(int argc, _TCHAR* argv[]) { ICLRMetaHost *pMetaHost = nullptr; ICLRMetaHostPolicy *pMetaHostPolicy = nullptr; ICLRRuntimeHost *pRuntimeHost = nullptr; ICLRRuntimeInfo *pRuntimeInfo = nullptr; //HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); ////hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); //hr = pMetaHost->GetRuntime(L"vv2.0.50727", IID_PPV_ARGS(&pRuntimeInfo)); //if (FAILED(hr)) //{ // goto cleanup; //} //hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost)); // invoke the method that loads the CLR HRESULT hrCorBind = CorBindToRuntimeEx( NULL, // CLR version - NULL load the latest available // L"v4.0.30319", L"wks", // GCType ("wks" = workstation or "svr" = Server) 0, // STARTUP_LOADER_SAFEMODE CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pRuntimeHost); if (FAILED(hrCorBind)) { printf("CorBindToRuntimeEx failed : %08X ", hrCorBind); goto cleanup; } // *** HRESULT hr = pRuntimeHost->Start(); if (FAILED(hr)) { printf("pRuntimeHost->Start() failed : %08X ", hr); goto cleanup; } DWORD dwRet = 0; //hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"SampleManagedApp.exe", // L"SampleManagedApp.Program", // L"Test", // L"Hello World!", // &dwRet); hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll", L"ClassLibrary4VC.ClassLibraryTest01", L"ClassLibraryFunc01", L"Hello World!", &dwRet); if (FAILED(hr)) { printf("pRuntimeHost->ExecuteInDefaultAppDomain failed : %08X ", hr); goto cleanup; } WORD words[6] = { 0 }; words[0] = 11; words[1] = 22; words[2] = 33; words[3] = 07; words[4] = 55; hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll", L"ClassLibrary4VC.ClassLibraryTest01", L"ClassLibraryFunc02", (WCHAR*)&words[0], &dwRet); hr = pRuntimeHost->Stop(); if (FAILED(hr)) { printf("pRuntimeHost->Stop() failed : %08X ", hr); goto cleanup; } cleanup: if (pRuntimeInfo != nullptr) { pRuntimeInfo->Release(); pRuntimeInfo = nullptr; } if (pRuntimeHost != nullptr) { pRuntimeHost->Release(); pRuntimeHost = nullptr; } if (pMetaHost != nullptr) { pMetaHost->Release(); pMetaHost = nullptr; } system("pause"); }
2、