Reprinted from: http://www.linuxidc.com/Linux/2012-10/72293.htm Mechanism introduction
There are times when the main program is implemented in C/S + +, but we want to extend the unmanaged program through managed code, and thus gain a range of benefits from managed Code. such as the development of high efficiency, automatic garbage collection and so On.
The fundamental difference between running managed and unmanaged code is that the managed code is that the process first loads the CLR and then runs the managed program through the clr, while the unmanaged code runs as the operating system allocates memory directly from its PE header loader. therefore, If you need to extend an unmanaged program through managed code, you first load the CLR to get the unmanaged program to run managed Code.
You can use the following procedure to load the CLR into a process:
- Call the CLRCreateInstance function to get the ICLRMetaHost or ICLRMetaHostPolicy interface. The CLRCreateInstance function supersedes all the corbindto* functions listed in The. NET Framework 1.1 and 2.0 hosting the global static functions Section.
- Call iclrmetahost::enumerateinstalledruntimes, iclrmetahost::getruntime, or ICLRMetaHostPolicy::GetRequestedRuntime method to obtain a valid ICLRRuntimeInfo Pointer.
- Call the ICLRRuntimeInfo::GetInterface method. Specify CLSID_CLRRuntimeHost for the Rclsid parameter and iid_iclrruntimehost for the riid parameter.
The prototypes of all these interfaces are located in the Metahost.h file, which is located in the Include directory of the Windows Software Development Kit (SDK). The host can use the ICLRRuntimeInfo and ICLRRuntimeHost interfaces to control which version of the runtime to load and the behavior of basic features such as garbage collection and assembly Loading. Use the ICLRRuntimeHost interface to do the following:
- You start the runtime by calling the ICLRRuntimeHost::Start Method.
- Executes managed Code.
- Gets a pointer to the ICLRControl interface, which provides access to the manager implemented by the common language runtime, and registers the host control object that implements the IHostControl Interface. The common language runtime invokes the IHostControl interface to determine the manager of the host Implementation.
Refer here http://msdn.microsoft.com/en-us/library/01918c6x.aspx
Instance Code
The following is a C + + load of the CLR instance code that runs the managed program, and after starting the clr, run the program named Test in Managed program SampleManagedApp.exe by calling Executeindefaultappdomain. It is important to note that ExecuteInDefaultAppDomain can only execute methods that have a managed code signature of static int pwzmethodname (String pwzargument).
- #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));
- 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->stop ();
- Cleanup
- if(pruntimeinfo! = Nullptr)
- {
- Pruntimeinfo->release ();
- Pruntimeinfo = nullptr;
- }
- if(pruntimehost! = Nullptr)
- {
- Pruntimehost->release ();
- Pruntimehost = nullptr;
- }
- if(pmetahost! = Nullptr)
- {
- Pmetahost->release ();
- Pmetahost = nullptr;
- }
- }
The corresponding managed code is as follows,
- using System;
- namespace Samplemanagedapp
- {
- Class Program
- {
- static void Main (string[] Args)
- {
- }
- public static int Test (string s)
- {
- Console.WriteLine (s);
- return 0;
- }
- }
- }
eventually, the SampleManagedApp.exe is run under the same path as the unmanaged program and the unmanaged program will get output from the final console: Hello world!
C + + manually loading CLR run managed programs (CLR Hosting)