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