HighTechTalks DotNet Forums  

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

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss How can one enumerate COM object properties exposed via IDispatch in .NET? in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default How can one enumerate COM object properties exposed via IDispatch in .NET? - 11-16-2005 , 03:51 PM






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.


Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default Re: How can one enumerate COM object properties exposed via IDispatch in .NET? - 11-16-2005 , 04:22 PM






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);
}
}
}


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.