none
Host CLR from unmanaged code how to get to instance of Activator / _Activator RRS feed

  • Question

  • Hi,

    We try from within AutoIt to host the CLR which works partially

      https://www.autoitscript.com/forum/topic/187334-using-net-libary-with-autoit-possible/

    However how do I get in a C like language access to the Activator instance or how can i instantiate that one?

    oMSCorAssembly.CreateInstance("System.Activator") seems not to work

    Similar for value types like System.Int32 or System.String there is no clear example around.

    In general would be nice to C a hosting example from PLAIN C like language

    thx,

    Elwin

    Thursday, April 27, 2017 6:41 PM

Answers

All replies

  • Hi junkew,

    Thank you for posting here.

    For your question, what is your C like language?

    If you use C++ unmanaged code to call managed code to create a instance of System.Activator, you could check the link of blog below.

    https://blogs.msdn.microsoft.com/calvin_hsia/2013/08/30/call-managed-code-from-your-c-code/

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 28, 2017 8:02 AM
    Moderator
  • I have working examples in C++ but looking more for plain C.

    AutoIt is the language I am using which uses mainly either COM or DLL Call to Win32 Api

    Instantiating objects like system.arraylist all works fine from AutoIt but just getting a "System.Activator" so far no luck. Reading thru activator.cs reference source gives me the belief I cannot instantiate from non managed language or com 

            

      // This class only contains statics, so hide the worthless constructor
            private Activator() {}

    As I am hosting the CLR I somehow have to get access to _Activator/System.Activator to be able to 

    a. instantiate oActivator to be of type System.Activator or get _Activator

    b. Then do oActivator.CreateInstance as according to documentation this can instantiate more types then system.assembly.createinstance

    I even tried to instantiate with CoCreateInstance but no luck

    Friday, April 28, 2017 1:53 PM
  • Hi junkew,

    Thank you for feedback.

    For your question, please check the following link. It shows how to use .net library with autolt.

    https://www.autoitscript.com/forum/topic/187334-using-net-libary-with-autoit-possible/?page=9

    If you want to expose the Activator class to unmanaged code. Please refer to the following links.

    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices._activator(v=vs.110).aspx

    https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices._activator?view=netframework-4.7

    And you could download the source file from Calling C# .NET methods from unmanaged C/C++ code for reference.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 8:25 AM
    Moderator
  • Hi,

    I am co author of that AutoIt UDF thread you refer to and I know the other references you gave.

    But somehow we do something wrong from AutoIt which is closer to C like language then to C++ or C#.

    As such we are looking for an example thats closer related to AutoIt.

    I made below in Excel 2010 VBA to see how to get an activator in VBA. I know thats probably not the best place to load .NET CLR but at least it gives me some indication if and how i can get an activator from a nonmanged/com like world.

    ' Add references to mscoree.tlb and mscorlib.tlb of .NET 2.0
    ' Then call the methods below
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Sub test()
        Dim clr As mscoree.CorRuntimeHost               'The runtime host
        Dim domain As mscorlib.AppDomain                'The domain we are running in
        Dim oCoreAssembly As Assembly                   'A specific assembly loaded
        
        Dim oFormAssembly As Assembly                   'A specific assembly loaded
        Dim iType As mscorlib.Type                      'A reference to the type of the FCL
        Dim loadedAsms() As Assembly                    'A variant array
        
        Dim oForm As Object                             'A reference to but an instance of a form in
        Dim oCol As mscorlib.Object                              'A (not working) reference to get the controls collection in
        Dim oInt As mscorlib.Type
    
        Dim tStr As String                              'String with the class/type to be created
        
        Set clr = New mscoree.CorRuntimeHost            'Create the CLR
        
        Dim loadedAssemblies As Variant
        
        clr.Start                                       'Start the CLR
        clr.GetDefaultDomain domain                     'Get the default running domain
     
        
        Set oCoreAssembly = domain.Load_2("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
        
        'Load the System.Windows.Forms dll
        Set oFormAssembly = domain.Load_2("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
        
        tStr = "System.Windows.Forms.Form"               'Name of the type to create
        Set iType = oFormAssembly.GetType_2(tStr)        'Get an interface so things can be invoked/get/set etc.
        Set oForm = oFormAssembly.CreateInstance_2(tStr, True) 'Create an actual instance of the given type
        oForm.Name = "Nice try from VBA"                 'Set a name property
        oForm.Text = "Nice, VBA rocks to"                'Set a title property
    
        tStr = "System.Windows.Forms.Button"             'Name of the type to create
        Set iType = oFormAssembly.GetType_2(tStr)        'Get an interface so things can be invoked/get/set etc.
        Set oBtn = oFormAssembly.CreateInstance_2(tStr, True) 'Create an actual instance of the given type
        oBtn.Text = "do not hit"                          'Set a caption property
        Set oBtn.Parent = oForm                           'Link it to the form
        
        oForm.Show                                          'Show the form
        
        loadedAsms = domain.GetAssemblies()             'See which assemblies are loaded by default
    
        For Each asm In loadedAsms                      'Just dump some information from assemblies
            Debug.Print asm.FullName
        Next
        
        
        tStr = "System.Int32"               'Name of the type to create
        Set iType = oCoreAssembly.GetType_2(tStr)        'Get an interface so things can be invoked/get/set etc.
        Set oInt = oCoreAssembly.GetType_2(tStr)
        'oFormAssembly.CreateInstance_2(tStr, True) 'Create an actual instance of the given type
     
        tStr = "System.Convert"               'Name of the type to create
        Set iType = oCoreAssembly.GetType_2(tStr)        'Get an interface so things can be invoked/get/set etc.
      '  Set oConvert = oFormAssembly.CreateInstance_2(tStr, True) 'Create an actual instance of the given type
        'oFormAssembly.CreateInstance_2(tStr, True) 'Create an actual instance of the given type
    
        
     '   Set oCol = oForm.Controls
        'Debug.Print oCol.GetType()
    'Here the problems start
            'bFlags = 0
          'Fails
          'Debug.Print oCol.Count
        
        'Set pUnwrapped = oForm.Unwrap
        
    '    Set oCol = iType.InvokeMember_3("Controls", bFlags, Null, pUnwrapp, Null, Null)
    
    End Sub

    regards,

    Elwin

    • Marked as answer by junkew Friday, May 26, 2017 4:29 PM
    • Unmarked as answer by junkew Friday, May 26, 2017 4:29 PM
    Saturday, May 6, 2017 9:37 PM
  • Finally found it out and small example made in AutoIt forums

    https://www.autoitscript.com/forum/topic/187334-using-net-libary-with-autoit-possible/?do=findComment&comment=1353264

    Wednesday, May 10, 2017 6:45 AM