System.Activator.CreateComInstanceFrom

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scorpion53061

    System.Activator.CreateComInstanceFrom

    Hello to all and hope everyone on holiday is enjoying themselves!!

    I am studying this namespace and method. In the docs it says:

    "Creates an instance of the COM object whose name is specified, using the
    named assembly file and the constructor that best matches the specified
    parameters."

    The syntax is:

    Public Overloads Shared Function CreateComInstan ceFrom( _
    ByVal assemblyName As String, _
    ByVal typeName As String _
    ) As ObjectHandle
    End Function

    Is my understanding correct to say we could create an instance of say EXCEL
    without add a reference in the project? Probably not but here is what I
    tried to do. And yes I know it is easier to add a reference than to do it
    this way. I am just the curious type.

    CreateComInstan ceFrom("EXCEL.E XE", "Microsoft.Offi ce.Core")

    No instance of EXCEL appeared in my Task List though. Any thoughts on this
    method?


  • Rob Teixeira [MVP]

    #2
    Re: System.Activato r.CreateComInst anceFrom

    What you really want is:

    Dim xApp As Object = Microsoft.Visua lBasic.CreateOb ject("Excel.App lication")

    And while this will indeed create an instance without adding a reference,
    you will have to operate on the object completely through late-bound
    techniques, whether you use VB's native late-bound access (with Option
    Strict Off) or Reflection. In either case, you'll get no intellisense and
    lose all type safety.

    -Rob Teixeira [MVP]


    "scorpion53 061" <scorpion_53061 @nospamhereever yahoo.com> wrote in message
    news:OblkQv0REH A.2520@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hello to all and hope everyone on holiday is enjoying themselves!!
    >
    > I am studying this namespace and method. In the docs it says:
    >
    > "Creates an instance of the COM object whose name is specified, using the
    > named assembly file and the constructor that best matches the specified
    > parameters."
    >
    > The syntax is:
    >
    > Public Overloads Shared Function CreateComInstan ceFrom( _
    > ByVal assemblyName As String, _
    > ByVal typeName As String _
    > ) As ObjectHandle
    > End Function
    >
    > Is my understanding correct to say we could create an instance of say[/color]
    EXCEL[color=blue]
    > without add a reference in the project? Probably not but here is what I
    > tried to do. And yes I know it is easier to add a reference than to do it
    > this way. I am just the curious type.
    >
    > CreateComInstan ceFrom("EXCEL.E XE", "Microsoft.Offi ce.Core")
    >
    > No instance of EXCEL appeared in my Task List though. Any thoughts on this
    > method?
    >
    >[/color]


    Comment

    • scorpion53061

      #3
      Re: System.Activato r.CreateComInst anceFrom

      I know about late binding.

      What is the purpose of System.Activato r.CreateComInst anceFrom method? Do you
      know of a code sample or can you devise one that uses it?


      Comment

      • Rob Teixeira [MVP]

        #4
        Re: System.Activato r.CreateComInst anceFrom

        It's sort of the same thing I showed you, but it assumes you have a runtime
        callable wrapper assembly.
        You should be able to use this, for example, with the Office PIAs. But that
        being the case, you're better off referencing the PIAs directly.

        -Rob Teixeira [MVP]

        "scorpion53 061" <scorpion_53061 @nospamhereever yahoo.com> wrote in message
        news:uFgi3N1REH A.1392@TK2MSFTN GP09.phx.gbl...[color=blue]
        > I know about late binding.
        >
        > What is the purpose of System.Activato r.CreateComInst anceFrom method? Do[/color]
        you[color=blue]
        > know of a code sample or can you devise one that uses it?
        >
        >[/color]


        Comment

        • scorpion53061

          #5
          Re: System.Activato r.CreateComInst anceFrom

          Sure but indulge me for a second.....

          If I were to use this could you show me how it would be written?

          CreateComInstan ceFrom("Excel.d ll", "C:\Documen ts and Settings\Kelly
          Martens\My Documents\Visua l Studio
          Projects\XMLSet tingsDemo\obj\I nterop.Excel.dl l")

          When I do this no instance shows in my task manager demonstrating that an
          Excel instance is alive...Why or how did I write this wrong?
          "Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
          news:%23AZV$S1R EHA.3628@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > It's sort of the same thing I showed you, but it assumes you have a[/color]
          runtime[color=blue]
          > callable wrapper assembly.
          > You should be able to use this, for example, with the Office PIAs. But[/color]
          that[color=blue]
          > being the case, you're better off referencing the PIAs directly.
          >
          > -Rob Teixeira [MVP]
          >
          > "scorpion53 061" <scorpion_53061 @nospamhereever yahoo.com> wrote in message
          > news:uFgi3N1REH A.1392@TK2MSFTN GP09.phx.gbl...[color=green]
          > > I know about late binding.
          > >
          > > What is the purpose of System.Activato r.CreateComInst anceFrom method? Do[/color]
          > you[color=green]
          > > know of a code sample or can you devise one that uses it?
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Rob Teixeira [MVP]

            #6
            Re: System.Activato r.CreateComInst anceFrom

            You should be getting an error.
            The arguments are (assembly, typeName), which in this case would be the full
            path to the DLL, followed by the type name, which has to be an exported type
            that has a constructor (IOW, a class where the underlying COM object can be
            instantiated from CoCreateInstanc e - for example, "Excel.Applicat ionClass").
            If you use this type name, you will see an instance of Excel.exe on the
            running process list. However, this method gives you an object handle
            instance, which is fine for dealing with cross-app-domain object
            indirection, but stick to the Microsoft.Visua lBasic.CreateOb ject method if
            you want to directly call methods from this using late-bound calls or
            reflection.

            -Rob Teixeira [MVP]

            "scorpion53 061" <scorpion_53061 @nospamhereever yahoo.com> wrote in message
            news:eZyeZY1REH A.1388@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Sure but indulge me for a second.....
            >
            > If I were to use this could you show me how it would be written?
            >
            > CreateComInstan ceFrom("Excel.d ll", "C:\Documen ts and Settings\Kelly
            > Martens\My Documents\Visua l Studio
            > Projects\XMLSet tingsDemo\obj\I nterop.Excel.dl l")
            >
            > When I do this no instance shows in my task manager demonstrating that an
            > Excel instance is alive...Why or how did I write this wrong?[/color]


            Comment

            Working...