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. Getting COM type information in C#

Getting COM type information in C#

Scheduled Pinned Locked Moved C#
csharpcomquestion
7 Posts 2 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.
  • S Offline
    S Offline
    Simon_uk
    wrote on last edited by
    #1

    Morning guys and girls, I'm trying to get a list of methods from a COM object that I only know the ProgID of. This is the code I thought would work... --- Type t = Type.GetTypeFromProgID("xxx.yyy"); if (t != null) { MemberInfo[] members = t.GetMembers(); for (int i = 0; i < members.Length; i++) MessageBox.Show(members[i].Name); } --- But this only seems to return methods that exist in c#'s object object (if you know what I mean!). Anyone have any ideas? Thanks in advance, Simon.

    K 1 Reply Last reply
    0
    • S Simon_uk

      Morning guys and girls, I'm trying to get a list of methods from a COM object that I only know the ProgID of. This is the code I thought would work... --- Type t = Type.GetTypeFromProgID("xxx.yyy"); if (t != null) { MemberInfo[] members = t.GetMembers(); for (int i = 0; i < members.Length; i++) MessageBox.Show(members[i].Name); } --- But this only seems to return methods that exist in c#'s object object (if you know what I mean!). Anyone have any ideas? Thanks in advance, Simon.

      K Offline
      K Offline
      Kant
      wrote on last edited by
      #2

      I did it from C++ like this. (I am still learning C#) 1. Get the ITypeLib by using LoadTypeLibEx[^] function. 2. Get the number of type descriptions from typelib via GetTypeInfoCount[^] method. 3. Then I used GetTypeInfo[^] and GetTypeAttr [^] methods. 4. Now play with TYPEATTR[^] members which contains all the info. Use the links provided to find the solution for C#. Let me know how it works.
      "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
      This signature was created by "Code Project Quoter".

      S 1 Reply Last reply
      0
      • K Kant

        I did it from C++ like this. (I am still learning C#) 1. Get the ITypeLib by using LoadTypeLibEx[^] function. 2. Get the number of type descriptions from typelib via GetTypeInfoCount[^] method. 3. Then I used GetTypeInfo[^] and GetTypeAttr [^] methods. 4. Now play with TYPEATTR[^] members which contains all the info. Use the links provided to find the solution for C#. Let me know how it works.
        "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
        This signature was created by "Code Project Quoter".

        S Offline
        S Offline
        Simon_uk
        wrote on last edited by
        #3

        Thanks Kant, I am using the LoadTypeLibEx method, and then using a System.Runtime.InteropServices.TypeLibConverter to convert it to an assembly. I can then get all the objects in the DLL by using reflection (GetTypes etc). Cheers. :)

        K 1 Reply Last reply
        0
        • S Simon_uk

          Thanks Kant, I am using the LoadTypeLibEx method, and then using a System.Runtime.InteropServices.TypeLibConverter to convert it to an assembly. I can then get all the objects in the DLL by using reflection (GetTypes etc). Cheers. :)

          K Offline
          K Offline
          Kant
          wrote on last edited by
          #4

          Hmm.. Using TypeLibConverter, if you don't mind can you post the code snippet. I am just curious. Anyway I tried like this C#. // via DllImport LoadTypeLibEx(strCOMLibName, REGKIND.REGKIND_DEFAULT, out oCOMTypeLib); if(oCOMTypeLib != null) {    TYPELIBATTR oTypeLibAttributes = new TYPELIBATTR();    Type oType = oTypeLibAttributes.GetType(); .. ..    oCOMTypeLib.GetLibAttr(out hwndTLibAttr);
          "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
          This signature was created by "Code Project Quoter".

          S 1 Reply Last reply
          0
          • K Kant

            Hmm.. Using TypeLibConverter, if you don't mind can you post the code snippet. I am just curious. Anyway I tried like this C#. // via DllImport LoadTypeLibEx(strCOMLibName, REGKIND.REGKIND_DEFAULT, out oCOMTypeLib); if(oCOMTypeLib != null) {    TYPELIBATTR oTypeLibAttributes = new TYPELIBATTR();    Type oType = oTypeLibAttributes.GetType(); .. ..    oCOMTypeLib.GetLibAttr(out hwndTLibAttr);
            "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
            This signature was created by "Code Project Quoter".

            S Offline
            S Offline
            Simon_uk
            wrote on last edited by
            #5

            This is what I did Kant:LoadTypeLibEx(lstrFilename, RegKind.RegKind_None, out typeLib ); if (typeLib != null) { AssemblyBuilder asm = converter.ConvertTypeLibToAssembly(typeLib, "tmplib.dll", 0, eventHandler, null, null, null, null ); Type[] types = asm.GetTypes(); //Get an array of types contained in the assembly }
            Once you have this, you can loop around the types to get the methods... for (int i = 0; i < types.Length; i++) { MemberInfo[] meths = types[i].GetMembers(); for (int j = 0; j < meths.Length; j++) { MessageBox.Show(meths[j].Name); } }
            Hope this is useful to you. Thanks again for your help, Simon.

            K 1 Reply Last reply
            0
            • S Simon_uk

              This is what I did Kant:LoadTypeLibEx(lstrFilename, RegKind.RegKind_None, out typeLib ); if (typeLib != null) { AssemblyBuilder asm = converter.ConvertTypeLibToAssembly(typeLib, "tmplib.dll", 0, eventHandler, null, null, null, null ); Type[] types = asm.GetTypes(); //Get an array of types contained in the assembly }
              Once you have this, you can loop around the types to get the methods... for (int i = 0; i < types.Length; i++) { MemberInfo[] meths = types[i].GetMembers(); for (int j = 0; j < meths.Length; j++) { MessageBox.Show(meths[j].Name); } }
              Hope this is useful to you. Thanks again for your help, Simon.

              K Offline
              K Offline
              Kant
              wrote on last edited by
              #6

              After your post, I read the information about TypeLibConverter. I went in a different route to get the info. (I followed the same path I did in C++) Anyway, I want to write a small article about this. Do you want to jump in? Except for MSDN, I haven't found any useful info about reading COM information via C# anywhere.
              "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
              This signature was created by "Code Project Quoter".

              S 1 Reply Last reply
              0
              • K Kant

                After your post, I read the information about TypeLibConverter. I went in a different route to get the info. (I followed the same path I did in C++) Anyway, I want to write a small article about this. Do you want to jump in? Except for MSDN, I haven't found any useful info about reading COM information via C# anywhere.
                "...if you don't want to hear things that piss you off don't piss off other people. SIMPLE." - Steven Hicks
                This signature was created by "Code Project Quoter".

                S Offline
                S Offline
                Simon_uk
                wrote on last edited by
                #7

                Hi I don't really think I know enough to write an article to be honest. Also, I'm not very good at explaining stuff in a concise fashion! :) Feel free to ask me any questions or use any of my code in the article though (and I'll rate it a 5 for ya! ;) ). Cheers, Simon.

                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