Author Topic: CCLRHost Class: Hosting the Common Language Runtime  (Read 1306 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3049
  • Gender: Male
    • José Roca Software
CCLRHost Class: Hosting the Common Language Runtime
« on: August 30, 2011, 11:20:21 PM »
 
CCLRHost is a class designed to host the .NET Common Language Runtime (CLR) in a PowerBASIC application and create and unwrap an instance of a COM visible .NET class whose methods and properties can be called using the PowerBASIC COM Automation statements.

The folliwing example hosts the .NET 4 runtime in a PowerBASIC application, creates an instance of the System.Collections.Stack collection and calls its Push and Pop methods.

Code: [Select]
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "windows.inc"
#INCLUDE ONCE "CCLRHost.inc"

FUNCTION PBMAIN () AS LONG

   LOCAL pCLRHost AS ICLRHost
   LOCAL oStack AS DISPATCH
   LOCAL vRes AS VARIANT
   LOCAL vPrm AS VARIANT
   LOCAL bstrOutput AS WSTRING

   ' // Create an instance of the CCLRHost class
   pCLRHost = NewCLR4Host("v4.0.30319")   ' --> change version number if needed
   IF ISNOTHING(pCLRHost) THEN EXIT FUNCTION

   ' // Create an instance of the Stack collection
   oStack = pCLRHost.CreateInstance("mscorlib", "System.Collections.Stack")
   IF ISOBJECT(oStack) THEN
      ' Push and Pop some strings
      vPrm = "rocks!"
      OBJECT CALL oStack.Push(vPrm)
      vPrm = "PB"
      OBJECT CALL oStack.Push(vPrm)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput = VARIANT$$(vRes)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput += " " & VARIANT$$(vRes)
      MSGBOX bstrOutput
      oStack = NOTHING
   END IF

   pCLRHost = NOTHING

END FUNCTION

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3049
  • Gender: Male
    • José Roca Software
Re: CCLRHost Class: Hosting the Common Language Runtime
« Reply #1 on: August 30, 2011, 11:22:08 PM »
 
Same as above, but using the .NET 2 runtime.

Code: [Select]
#INCLUDE "CCLRHost.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL pCLRHost AS ICLRHost
   LOCAL oStack AS DISPATCH
   LOCAL vRes AS VARIANT
   LOCAL vPrm AS VARIANT
   LOCAL bstrOutput AS WSTRING

   ' // Create an instance of the CCLRHost class
   pCLRHost = NewCLRHost("v2.0.50727", "wks")
   IF ISNOTHING(pCLRHost) THEN EXIT FUNCTION

   ' // Create an instance of the Stack collection
   oStack = pCLRHost.CreateInstance("mscorlib", "System.Collections.Stack")
   IF ISOBJECT(oStack) THEN
      ' Push and Pop some strings
      vPrm = "rocks!"
      OBJECT CALL oStack.Push(vPrm)
      vPrm = "PB"
      OBJECT CALL oStack.Push(vPrm)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput = VARIANT$$(vRes)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput += " " & VARIANT$$(vRes)
      MSGBOX bstrOutput
      oStack = NOTHING
   END IF

   pCLRHost = NOTHING

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 3049
  • Gender: Male
    • José Roca Software
Re: CCLRHost Class: Hosting the Common Language Runtime
« Reply #2 on: August 30, 2011, 11:23:03 PM »
 
The following version allows to use your own domain, instead of the default domain.

Code: [Select]
#INCLUDE "CCLRHost.inc"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL pCLRHost AS ICLRHost
   LOCAL pDomain AS SystemAppDomain

   LOCAL oStack AS DISPATCH
   LOCAL vRes AS VARIANT
   LOCAL vPrm AS VARIANT
   LOCAL bstrOutput AS WSTRING

   ' // Create and instance of the CCLRHost class and initialize it
   pCLRHost = NewCLRHost("v2.0.50727", "wks")
   IF ISNOTHING(pCLRHost) THEN EXIT FUNCTION

   ' // Create a custom domain
   pDomain = pCLRHost.CreateDomain("MyDomain")

   ' // Create an instance of the Stack collection
   oStack = pCLRHost.CreateInstance2(pDomain, "mscorlib", "System.Collections.Stack")
   IF ISOBJECT(oStack) THEN
      ' // Push and Pop some strings
      vPrm = "rocks!"
      OBJECT CALL oStack.Push(vPrm)
      vPrm = "PB"
      OBJECT CALL oStack.Push(vPrm)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput = VARIANT$$(vRes)
      OBJECT CALL oStack.Pop TO vRes
      bstrOutput += " " & VARIANT$$(vRes)
      MSGBOX bstrOutput
      oStack = NOTHING
   END IF

   pCLRHost.UnloadDomain(pDomain)

   pDomain = NOTHING
   pCLRHost = NOTHING

END FUNCTION
' ========================================================================================