Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 4-9 extracted from chapter The CLR (Common Languages Runtime)


Listing 4-8<     > Listing 4-10



Example_4_9.cpp
// You need to link with the static lib mscoree.lib 
// to compile this C++ file.
#include <mscoree.h>

// The import must be done on a single line. 
#import <mscorlib.tlb> raw_interfaces_only ...
  ... high_property_prefixes("_get","_put","_putref") ... 
    ... rename("ReportEvent","ReportEventManaged") rename_namespace("CRL")

// We use the namespace ComRuntimeLibrary.
using namespace CRL;
ICLRRuntimeHost * pClrHost = NULL;

void main (void){
   // Get a COM pointer of type ICorRuntimeHost on the CLR.
   HRESULT hr CorBindToRuntimeEx(
               NULL// We ask for the most recent version of the CLR.
               NULL// We ask for the workstation version of the CLR.
               0, 
               CLSID_CLRRuntimeHost,
               IID_ICLRRuntimeHost, 
               (LPVOID *) &pClrHost);
   if (FAILED(hr)){
      printf("Can't get a pointer of type ICLRRuntimeHost!");
      return;
   }
   printf("We got a pointer of type ICCLRRuntimeHost.\n");
   printf("Launch the CLR.\n");
   pClrHost->Start();

   DWORD retVal=0;
   hr pClrHost->ExecuteInDefaultAppDomain(
    L"C:\\test\\MyManagedLib.dll",// Path + Asm.
    L"MyProgramNamespace.MyClass",// Full name of the type.
    L"MyMethod",                  // Name of the method to run. It must
                                  // have the signature int XXX(string).
    L"Hello from host!",          // The argument string.
    &retVal);                     // A reference to the returned value.
  
   pClrHost->Stop();
   printf("CLR stopped.\n");
   pClrHost->Release();
   printf("Bye!\n");
}	
Copyright Patrick Smacchia 2006 2007