1

this is my first SO question! I would like to instantiate a COM object and cast it to IDispatchEx so that I can enumerate its members. Here is an example:

    Type _COMType = System.Type.GetTypeFromProgID("Scripting.FileSystemObject");
    var _COMObject = (IDispatchEx)Activator.CreateInstance(_COMType);

My IDispatchEx is identical to the one on this website (not my website), except that GetNextDispID and GetMemberName return an int (which I wish to use for HRESULT as described on MSDN).

The example above does not work. Is there any way to instantiate COM objects as you would from Active Scripting cast to the IDispatchEx interface?

Thanks for any and all help/suggestions!

svick
  • 236,525
  • 50
  • 385
  • 514
aikeru
  • 3,773
  • 3
  • 33
  • 48

2 Answers2

5

This operation failed because the QueryInterface call on the COM component for the interface with IID '{A6EF9860-C720-11D0-9337-00A0C90DCAA9}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

The exception message you get is as clear as a bell, the Scripting.FileSystemObject simply doesn't implement the IDispatchEx interface. Only IDispatch. This works fine:

        Type t = System.Type.GetTypeFromProgID("Scripting.FileSystemObject");
        var obj = Activator.CreateInstance(t);
        var iobj = (stdole.IDispatch)obj;

You're done, you cannot force a COM coclass to implement an interface. I would not expect very many COM classes to implement it, IDispatchEx is pretty obscure. It fits the JScript mold.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks, Hans. It seems that it's also not possible to enumerate properties/methods using IDispatch. I may have to go at this another way... – aikeru May 03 '11 at 13:48
  • Coming back to this -- I was wrong :) It is totally possible to enumerate members using IDispatch for late-bound COM objects (that implement it). I'm still learning a lot about this. – aikeru Jun 18 '12 at 15:46
1

It seems that you will need to define this interface yourself if you wish to use it in C# (source)

This thread may be relevant - it seems that someone has found an existing implementation of IDispatchEx to use.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367