Create new automation OLE object

Welcome Forums General PowerShell Q&A Create new automation OLE object

This topic contains 3 replies, has 2 voices, and was last updated by

 
Moderator
4 years, 4 months ago.

  • Author
    Posts
  • #17745

    Participant
    Points: 0
    Rank: Member

    Hello,

    I have been using the Amibroker program . It provides SDK to automate the work programatically.
    The application webpage http://www.amibroker.com/guide/objects.html#AWindow describes all methods and properties of the application objects.
    The quote from the help guide:

    Application object is main OLE automation object for AmiBroker. You have to create it prior to accesing any other objects. To create Application object use the following code:

    JScript:

    AB = new ActiveXObject("Broker.Application");

    VB/VBScript:

    AB = CreateObject("Broker.Application")

    However when I try to use the object the cmdlet new-object seems to fail.

    PS C:\..\SkyDrive\Powershell\  $AB = New-Object  Broker.Application
    New-Object : Cannot find type [Broker.Application]: verify that the assembly containing this type is loaded.
    At line:1 char:7
    + $AB = New-Object  Broker.Application
    +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

    I tried to add the following lines that I found on the web but it still does not work.

    PS C:\..\SkyDrive\Powershell\  Add-Type Broker.Application
    Add-Type : c:\Users\Tomasz\AppData\Local\Temp\hduuug3r.0.cs(1) : A namespace cannot directly contain members such as fields or methods
    c:\Users\Tomasz\AppData\Local\Temp\hduuug3r.0.cs(1) : >>> Broker.Application
    At line:1 char:1
    + Add-Type Broker.Application
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (c:\Users\Tomasz...elds or methods:CompilerError) [Add-Type], Exception
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

    The following code that I found on the web displays Broker.Application object in the list

    gci HKLM:\Software\Classes -ea 0| ? {$_.PSChildName -match '^\w+\.\w+$' -and
    (gp "$($_.PSPath)\CLSID" -ea 0)} | Out-GridView

    So the question is how do I create a new Broker.Application object in Powershell ?

    Thanks
    Tomasz

  • #17747

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Hello Tomasz,

    The New-Object cmdlet defaults to create an instance of the .NET Framework class specified. You'll need to tell it to create a COM object instead.

    $AB = New-Object -ComObject Broker.Application
    

    Best,
    Daniel

    • #17761

      Participant
      Points: 0
      Rank: Member

      Hi Daniel,

      The COM object was the first I tried. The object created in such way did not have any expected methods or properties what confused me.
      Please see below.:

      $AB = New-Object -ComObject Broker.Application
      PS C:\..\SkyDrive\Powershell\  $AB = New-Object -ComObject Broker.Application
      
      PS C:\..\SkyDrive\Powershell\  $AB | gm
      
      
         TypeName: System.__ComObject
      
      Name                      MemberType Definition                                                     
      ----                      ---------- ----------                                                     
      CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
      Equals                    Method     bool Equals(System.Object obj)                                 
      GetHashCode               Method     int GetHashCode()                                              
      GetLifetimeService        Method     System.Object GetLifetimeService()                             
      GetType                   Method     type GetType()                                                 
      InitializeLifetimeService Method     System.Object InitializeLifetimeService()                      
      ToString                  Method     string ToString()                                              
      
      
      
      PS C:\..\SkyDrive\Powershell\  $AB.visible
      
      PS C:\..\SkyDrive\Powershell\  $AB.visible | gm
      gm : You must specify an object for the Get-Member cmdlet.
      At line:1 char:15
      + $AB.visible | gm
      +               ~~
          + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
          + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

      The Broker.Application object is supposed to have the following properties

      Properties:
      •ActiveDocument As Object
      •Stocks As Object
      •Version As String
      •Documents As Object
      •Markets As Object
      •DatabasePath As String
      •Analysis As Object
      •Commentary As Object
      •ActiveWindow As Object
      •Visible As Integer

      Am I missing something ?

      Regards,
      Tomasz

  • #17763

    Moderator
    Points: 24
    Team Member
    Rank: Member

    The library is missing information that .NET/PowerShell requires to create the wrapper for the COM object with the correct properties and methods.

    Check out below page which explains the issue and a clunky workaround:

    http://www.sorrell.mcleod.co.uk/Scotty/powershell/COMinterop.htm

    Best,
    Daniel

The topic ‘Create new automation OLE object’ is closed to new replies.