Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

How can one enumerate COM object properties exposed via IDispatch in .NET?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dblock@dblock.org - 16 Nov 2005 20:51 GMT
I have a COM object implementing IFoo and a property get_Property,
put_Property... I can get a CLSID representing an object that
implements this and other interfaces, then create it with
Activator.CreateInstance and cal object.GetType().InvokeMember for any
member on that unmanaged COM object, such as "Property".

I want to expose additional properties that will be unknown in advance
and extend this interface. How do I enumerate all the members of this
object in c#?

Thx
dB.
dblock@dblock.org - 16 Nov 2005 21:22 GMT
I found the solution in some other post ... a little involved but
works.

[Guid("00020400-0000-0000-C000-000000000046")]
   [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
   public interface IDispatch
   {
       [PreserveSig]
       int GetTypeInfoCount(out int Count);
       [PreserveSig]
       int GetTypeInfo( [MarshalAs(UnmanagedType.U4)] int iTInfo,
           [MarshalAs(UnmanagedType.U4)] int lcid,  out UCOMITypeInfo
typeInfo);
       [PreserveSig]
       int GetIDsOfNames( ref Guid riid,
           [MarshalAs(UnmanagedType.LPArray,
ArraySubType=UnmanagedType.LPWStr)]
           string[] rgsNames,
           int cNames,   int lcid,
[MarshalAs(UnmanagedType.LPArray)] int[]
           rgDispId);
       [PreserveSig]
       int Invoke(int dispIdMember, ref Guid riid, uint lcid, ushort
wFlags,
           ref DISPPARAMS pDispParams, out object pVarResult, ref
EXCEPINFO
           pExcepInfo, IntPtr[] pArgErr);
   }

               if (component.GetType().IsCOMObject)
               {
                   IDispatch cd = (IDispatch) component;
                   int cnt;
                   cd.GetTypeInfoCount(out cnt);
                   if (cnt == 1)
                   {
                       UCOMITypeInfo ti;
                       cd.GetTypeInfo(0, 0, out ti);
                       IntPtr pti;
                       ti.GetTypeAttr(out pti);
                       TYPEATTR ta = (TYPEATTR)
Marshal.PtrToStructure(pti, typeof(TYPEATTR));
                       int funcs = ta.cFuncs;
                       ti.ReleaseTypeAttr(pti);
                       for (int i = 0; i < funcs; i++)
                       {
                           IntPtr pfd;
                           ti.GetFuncDesc(i, out pfd);
                           FUNCDESC fd = (FUNCDESC)
Marshal.PtrToStructure(pfd, typeof(FUNCDESC));
                           string[] names = { "" };
                           int ret;
                           ti.GetNames(fd.memid, names, names.Length,
out ret);
                           string name = names[0];
                           ti.ReleaseFuncDesc(pfd);
                       }
                   }                    
               }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.