C + + manually loading CLR run managed programs (CLR Hosting)

Source: Internet
Author: User
Tags hosting

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:

    1. 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.
    2. Call iclrmetahost::enumerateinstalledruntimes, iclrmetahost::getruntime, or ICLRMetaHostPolicy::GetRequestedRuntime method to obtain a valid ICLRRuntimeInfo Pointer.
    3. 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:

    1. You start the runtime by calling the ICLRRuntimeHost::Start Method.
    2. Executes managed Code.
    3. 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).

  1. #include <SDKDDKVer.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <metahost.h>
  6. #include <mscoree.h>
  7. #pragma comment (lib, "mscoree.lib")
  8. int _tmain (int argc, _tchar* Argv[])
  9. {
  10. ICLRMetaHost *pmetahost = nullptr;
  11. ICLRMetaHostPolicy *pmetahostpolicy = nullptr;
  12. ICLRRuntimeHost *pruntimehost = nullptr;
  13. ICLRRuntimeInfo *pruntimeinfo = nullptr;
  14. HRESULT hr = clrcreateinstance (clsid_clrmetahost, iid_iclrmetahost, (lpvoid*) & pmetahost);
  15. hr = Pmetahost->getruntime (L"v4.0.30319", Iid_ppv_args (&pruntimeinfo));
  16. if(FAILED (hr))
  17. {
  18. Goto cleanup;
  19. }
  20. hr = Pruntimeinfo->getinterface (clsid_clrruntimehost, Iid_ppv_args (&pruntimehost));
  21. hr = Pruntimehost->start ();
  22. DWORD dwret = 0;
  23. hr = Pruntimehost->executeindefaultappdomain (L"SampleManagedApp.exe",
  24. L"samplemanagedapp.program",
  25. L"Test",
  26. L"Hello world!",
  27. &dwret);
  28. hr = Pruntimehost->stop ();
  29. Cleanup
  30. if(pruntimeinfo! = Nullptr)
  31. {
  32. Pruntimeinfo->release ();
  33. Pruntimeinfo = nullptr;
  34. }
  35. if(pruntimehost! = Nullptr)
  36. {
  37. Pruntimehost->release ();
  38. Pruntimehost = nullptr;
  39. }
  40. if(pmetahost! = Nullptr)
  41. {
  42. Pmetahost->release ();
  43. Pmetahost = nullptr;
  44. }
  45. }

The corresponding managed code is as follows,

  1. using System;
  2. namespace Samplemanagedapp
  3. {
  4. Class Program
  5. {
  6. static void Main (string[] Args)
  7. {
  8. }
  9. public static int Test (string s)
  10. {
  11. Console.WriteLine (s);
  12. return 0;
  13. }
  14. }
  15. }

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.