Using a COM dll from C# without a type library

前端 未结 7 2188
梦谈多话
梦谈多话 2020-12-14 12:23

I need to use a COM component (a dll) developed in Delphi ages ago. The problem is: the dll does not contain a type library... and every interop feature (eg. TlbImp) in .NET

7条回答
  •  半阙折子戏
    2020-12-14 13:04

    If you've managed to create an instance of the object, you're over the first major hurdle!

    Now try this:

    myObject.GetType().InvokeMember(
                          "ResObjOpen",  // method name goes here
                          BindingFlags.InvokeMethod,
                          null,
                          myObject,
                          new object[] { 
                             someClientID,   // arguments go here
                             someSubId, 
                             somFileName, 
                             someInt} );
    

    The reason I think you may need to do this is if the Delphi COM object is not a "dual" object. It may only support late binding, i.e. the kind of invocation you see above.

    (In C# 4.0 they're making this easier with the dynamic keyword.)

    EDIT: Just noticed something very suspicious. The IID for the interface and the CLSID for the object itself appear to be the same. That's not right.

    Given that you've succeeded in creating the object, it would appear to be the CLSID of the object. So it's not the right IID. You need to go back to your Delphi folks and ask them to tell you what the IID of the interface IResSrvDll is.

    Edit again: You could try changing the enum member you specify from ComInterfaceType. There should be ones for IDispatch and "dual" - although as your object doesn't support IDispatch, neither of those should be the right choice. The IUnknown setting (which appears in your sample code) should work - suggesting that the IID is wrong.

提交回复
热议问题