Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Advanced C# Question

Advanced C# Question

Scheduled Pinned Locked Moved C#
questioncsharpcomdesign
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave Kerr
    wrote on last edited by
    #1

    Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr

    L M 2 Replies Last reply
    0
    • D Dave Kerr

      Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Creating the COM object is no problem, but such objects do not support reflection. The only way to get the information you need is through the object's type library. There are a number of COM articles here on CodeProject that include suggestions as to how to proceed with this.

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • D Dave Kerr

        Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        You can use GetTypeFromCLSID to get a COM Type give its GUID. Then you can use Activator.CreateInstance to create an object instance from that Type.

        T 1 Reply Last reply
        0
        • M Mirko1980

          You can use GetTypeFromCLSID to get a COM Type give its GUID. Then you can use Activator.CreateInstance to create an object instance from that Type.

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          Mirko1980 wrote:

          Then you can use Activator.CreateInstance to create an object instance from that Type.

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
          Never mind - my own stupidity is the source of every "problem" - Mixture

          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

          M 1 Reply Last reply
          0
          • T ThatsAlok

            Mirko1980 wrote:

            Then you can use Activator.CreateInstance to create an object instance from that Type.

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
            Never mind - my own stupidity is the source of every "problem" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            Refer also to Richard's answer: I fear you can't get full Reflection information from a COM object.

            D T 2 Replies Last reply
            0
            • M Mirko1980

              Refer also to Richard's answer: I fear you can't get full Reflection information from a COM object.

              D Offline
              D Offline
              Dave Kerr
              wrote on last edited by
              #6

              Thanks for the info, so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection. However, can I programmatically load the type library and get the function names from that? Again, I don't want to create a class from the type library, but dynamically retrieve the function names and parameters they take from a type library at runtime...

              P 1 Reply Last reply
              0
              • M Mirko1980

                Refer also to Richard's answer: I fear you can't get full Reflection information from a COM object.

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

                extremly sorry, am very new to c# enviornment!

                1 Reply Last reply
                0
                • D Dave Kerr

                  Thanks for the info, so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection. However, can I programmatically load the type library and get the function names from that? Again, I don't want to create a class from the type library, but dynamically retrieve the function names and parameters they take from a type library at runtime...

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  DaveKerr wrote:

                  so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection

                  You can, as long as the component supports IDispatch you can use the CustomMarshaller assembly (you'll commonly find it in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727). The following code sample displays the members of DirControl.DirList.8.0 using reflection:

                  using System;
                  using System.Collections.Generic;
                  using System.Text;
                  using System.Runtime.InteropServices;
                  using System.Runtime.InteropServices.CustomMarshalers;
                  using System.Reflection;

                  namespace ComMarshaller
                  {
                  [
                  ComImport,
                  Guid("00020400-0000-0000-C000-000000000046"),
                  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
                  ]
                  public interface IDispatch
                  {
                  void Reserved();
                  [PreserveSig]
                  int GetTypeInfo(uint nInfo, int lcid,
                  [MarshalAs(
                  UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))]
                  out Type typeInfo);
                  }

                  class Program
                  {
                  static void Main(string[] args)
                  {
                  Type type = Type.GetTypeFromProgID("DirControl.DirList.8.0");
                  object o = Activator.CreateInstance(type);
                  IDispatch disp = o as IDispatch;
                  if (disp != null)
                  {
                  Type tp = null;
                  disp.GetTypeInfo(0, 0, out tp);
                  MemberInfo[] mi = tp.GetMembers();

                      foreach (MemberInfo minfo in mi)
                      {
                        Console.WriteLine(minfo.Name);
                      }
                    }
                  
                    Console.WriteLine("Done");
                    Console.ReadKey();
                  }
                  

                  }
                  }

                  Remember, this needs CustomMarshaller.dll added as a reference in your application.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups