• Welcome to Theos PowerBasic Museum 2017.

RE:Loading the Common Language Runtime into a Process

Started by Carlo Pagani, September 10, 2008, 07:52:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Greg Lyon

#15
I fiddled around with a couple more of the .NET Framework classes. Here are some code snippets, they require José's CCLRHost code.

System.Collections.ArrayList

   ...
   oDisp = pCLRHost.CreateInstance(pDomain, "mscorlib", "System.Collections.ArrayList")
   IF ISOBJECT(oDisp) THEN

      vStr = UCODE$("First string")
      OBJECT CALL oDisp.Add(vStr) TO vCount

      vStr = UCODE$("Second string")
      OBJECT CALL oDisp.Add(vStr) TO vCount

      vStr = UCODE$("Third string")
      OBJECT CALL oDisp.Add(vStr) TO vCount

      FOR x = 0 to VARIANT#(vCount)
          vIndex = x
          OBJECT CALL oDisp.Item(vIndex) TO vItem
          strOutput = VARIANT$(vItem)
          STDOUT ACODE$(strOutput)
      NEXT x

      oDisp = NOTHING
   ELSE
       STDOUT "Error calling CreateInstance()"
   END IF
   ...


System.Random

    ...
    oDisp = pCLRHost.CreateInstance(pDomain, "mscorlib", "System.Random")
    IF ISOBJECT(oDisp) THEN
        OBJECT CALL oDisp.Next() TO vRnd
        strOutput = FORMAT$(VARIANT#(vRnd))
        STDOUT strOutput
        oDisp = NOTHING
    ELSE
        STDOUT "Error calling CreateInstance()"
    END IF
    ...


    ...
    oDisp = pCLRHost.CreateInstance(pDomain, "mscorlib", "System.Random")
    IF ISOBJECT(oDisp) THEN
        OBJECT CALL oDisp.NextDouble() TO vRnd
        strOutput = FORMAT$(VARIANT#(vRnd))
        STDOUT strOutput
        oDisp = NOTHING
    ELSE
        STDOUT "Error calling CreateInstance()"
    END IF
    ...


Greg Lyon

#16
I couldn't get this overload of System.Random.Next to work, I never figured out why. OBJRESULT$ says "Invalid number of parameters.".


    ...
    oDisp = pCLRHost.CreateInstance(pDomain, "mscorlib", "System.Random")
    IF ISOBJECT(oDisp) THEN
        vMin = 1   ' inclusive
        vMax = 50  ' exclusive
        OBJECT CALL oDisp.Next(vMin, vMax) TO vRnd
        IF VARIANT#(vRnd) = 0 then
            STDOUT OBJRESULT$(OBJRESULT)
        ELSE
            strOutput = FORMAT$(VARIANT#(vRnd))
            STDOUT strOutput
        END IF
        oDisp = NOTHING
    ELSE
        STDOUT "Error calling CreateInstance()"
    END IF
    ...

Edwin Knoppert

The translation to the COM interface makes it impossible to use the overloads this way.
I suspect functions will get an ordinal number.
Like i mentioned before, the easiest is making use of a custom assembly to bridge the gap.
I know Jose doesn't like my solution :)
But then.. just wait until he provides the assembly file loading part.
Which is fairly simple with respect to his current code.

José Roca

Quote
I couldn't get this overload of System.Random.Next to work,

COM doesn't support overloading.

Quote
Like i mentioned before, the easiest is making use of a custom assembly to bridge the gap.
I know Jose doesn't like my solution

My interest in .NET is very low. Having to create wrapper assembles is not something I'm going to do unless I absolutely need it.

Greg Lyon

QuoteMy interest in .NET is very low. Having to create wrapper assembles is not something I'm going to do unless I absolutely need it.

I don't blame you.  I find it interesting, but it's not something that I really need either.


José Roca

#20
 
Well, it seems that we don't need to write a wrapper after all.

This overloaded method has three definitions:

Random.Next ()
Random.Next (Int32)
Random.Next (Int32, Int32)

For the first, use:

OBJECT CALL oDisp.Next() TO vRnd

For the second, use:

vMax = 50
OBJECT CALL oDisp.Next_3(vMax) TO vRnd

For the third, use:

vMin = 1
vMax = 50
OBJECT CALL oDisp.Next_2(vMin) TO vRnd

Notice that, at least with this class, the order is no the same than in the documentation.

What is clear, however, is that it adds an underscore and an ordinal to the name of the method.

Greg Lyon

José,

Aha, thanks.  If you don't mind me asking, how did you figure that out?


José Roca

 
If you look at mscorlib20.inc, you will see that it is the technique that they have used for the overloaded methods of this library, e.g.

AppDomain.CreateInstance (String, String)
AppDomain.CreateInstance (String, String, Object[])
AppDomain.CreateInstance (String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)


   ' =====================================================================================
   METHOD CreateInstance ( _                  ' VTable offset = 148
     BYVAL STRING _                           ' [0] [in] AssemblyName /* VT_BSTR */
   , BYVAL STRING _                           ' [0] [in] typeName /* VT_BSTR */
   ) AS SystemObjectHandle                    ' [out][retval] **pRetVal /* **_ObjectHandle <dispinterface> */
   ' =====================================================================================

   ' =====================================================================================
   METHOD CreateInstance_2 ( _                ' VTable offset = 156
     BYVAL STRING _                           ' [0] [in] AssemblyName /* VT_BSTR */
   , BYVAL STRING _                           ' [0] [in] typeName /* VT_BSTR */
   , BYVAL DWORD _                            ' [0] [in] activationAttributes /* VT_SAFEARRAY */
   ) AS SystemObjectHandle                    ' [out][retval] **pRetVal /* **_ObjectHandle <dispinterface> */
   ' =====================================================================================

   ' =====================================================================================
   METHOD CreateInstance_3 ( _                ' VTable offset = 164
     BYVAL STRING _                           ' [0] [in] AssemblyName /* VT_BSTR */
   , BYVAL STRING _                           ' [0] [in] typeName /* VT_BSTR */
   , BYVAL INTEGER _                          ' [0] [in] ignoreCase /* VT_BOOL <Integer> */
   , BYVAL LONG _                             ' [0] [in] bindingAttr /* BindingFlags <enum> */
   , BYVAL SystemBinder _                     ' [1] [in] *Binder /* *_Binder <dispinterface> */
   , BYVAL DWORD _                            ' [0] [in] args /* VT_SAFEARRAY */
   , BYVAL SystemCultureInfo _                ' [1] [in] *culture /* *_CultureInfo <dispinterface> */
   , BYVAL DWORD _                            ' [0] [in] activationAttributes /* VT_SAFEARRAY */
   , BYVAL SystemEvidence _                   ' [1] [in] *securityAttributes /* *_Evidence <dispinterface> */
   ) AS SystemObjectHandle                    ' [out][retval] **pRetVal /* **_ObjectHandle <dispinterface> */
   ' =====================================================================================


Greg Lyon

José,

I see. 

Why didn't I notice that?  I must have spent 20 minutes looking at mscorlib20.inc;D

Thanks.








Greg Lyon

#24
System.Text.StringBuilder

    ...
    oDisp = pCLRHost.CreateInstance(pDomain, "mscorlib", "System.Text.StringBuilder")
    IF ISOBJECT(oDisp) THEN
        vStr1 = "Hello"
        vStr2 = " World"
        OBJECT CALL oDisp.Append_3(vStr1) TO vSB
        LET oSB = vSB
        OBJECT CALL oSB.Append_3(vStr2) TO vSB
        LET oSB = vSB
        OBJECT CALL oSB.ToString() TO vSB
        strOutput = VARIANT$(vSB)
        STDOUT strOutput
        oDisp = NOTHING
        oSB = NOTHING
    ELSE
        STDOUT "Error calling CreateInstance()"
    END IF
    ...