0

I am new to COM programming, Solution might be simple but I couldn't. My COM client code is working fine with early binding. Problem with late binding.

Client Code : Using .net 4.0 framework

System.Type objType = System.Type.GetTypeFromProgID("Simple_ATL.First_ATL.1", "localhost", true);
object obj = System.Activator.CreateInstance(objType);
Type actualobj = obj.GetType();
actualobj.InvokeMember("AddNumbers", BindingFlags.InvokeMethod | BindingFlags.Instance |    BindingFlags.Public, null, obj, new object[] { 10, 20 }); 

actualobj.InvokeMember throwing "COM target does not implement IDispatch" exception.

IDL file:

[
    object,
    uuid(C8F6E230-2672-11D3-A8A8-00105AA943DF),
    dual,
    helpstring("IFirst_ATL Interface"),
    pointer_default(unique)
]
interface IFirst_ATL : IDispatch
{
    [id(1), helpstring("method MultipleNumbers")] HRESULT MultipleNumbers([in] long Num1, [in] long Num2, [out, retval] long *ReturnVal);
    [id(2), helpstring("method AddNumbers")] HRESULT AddNumbers([in] long Num1, [in] long Num2, [out , retval] long *ReturnVal);
};

[
     uuid(34C534A0-2671-11D3-A8A8-00105AA943DF),
     version(1.1),
     helpstring("Simple_ATL 1.1 Type Library")
]
library SIMPLE_ATLLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");

[
    uuid(970599E0-2673-11D3-A8A8-00105AA943DF),
    version(2.1),
    helpstring(" New First_ATL Class")
]
coclass First_ATL
{
    [default] interface IFirst_ATL;
};

};

Please suggest me what went wrong in my code?

Thanks, Pavan

David Crowell
  • 3,711
  • 21
  • 28
Pavan Kumar kota
  • 45
  • 1
  • 1
  • 9
  • Adding more I had followed http://stackoverflow.com/questions/4105194/how-to-call-a-dcom-object-from-c but didn't work for me.. – Pavan Kumar kota Sep 22 '14 at 13:05
  • The IDL only *promises* that you implement IDispatch, you didn't actually do so according to the error message. That's normally pretty hard to get wrong, the ATL wizards auto-generate a lot of code. Including this IDL, no hints there. Inopportune editing, perhaps, somehow screwing up inheriting IDispatchImpl. Not knowing what code to post is a strong hint that you'll have a hard time figuring out what you did wrong. So do we. – Hans Passant Sep 22 '14 at 13:17
  • Thanks @HansPassant. I found the mistake in the auto-generated code BEGIN_COM_MAP(CFirst_ATL) COM_INTERFACE_ENTRY(IFirst_ATL) **COM_INTERFACE_ENTRY(IDispatch)** when I add it is working END_COM_MAP() – Pavan Kumar kota Sep 23 '14 at 11:45

0 Answers0