Cannot get interface from different process via ROT

旧时模样 提交于 2020-01-07 07:01:18

问题


my app is an .exe, it registers itself to ROT.

[ComVisible(true)]
[ProgId("My.App")]
public class MyApp
{
    public Interop_MyApp.IXXX XXX
    {
        get { return XXXImpl.Instance; } // -> Instance is derived from Interop_MyApp.IXXX, and static
    }

    public MyApp() { }
};

I start the .exe above, it's running.

Then I start an other .exe, which tries to get the XXX.

        object o = Marshal.GetActiveObject("My.App"); // -> returns a __ComObject, fine
        if (o == null)
            throw new InvalidOperationException("Could not connect to My.App");
        Type t = o.GetType();
        object r = t.InvokeMember("XXX", BindingFlags.GetProperty | BindingFlags.Public, null, o, null);  //--> returns a __ComObject, fine
        Interop_MyApp.IXXX xxx = r as Interop_MyApp.IXXX;    //----> here xxx is null?!

If I call t.GetProperties(), returns 0?? Where is the "XXX"?? Calling t.InvokeMember("XXX"...) succeeds!

Any help is appreciated, thanks.


回答1:


Thanks to Hans Passant hints, I could solve the problem. First I used a direct cast (Interop_MyApp.IXXX)r to get a more detailed error:

Unable to cast COM object of type 'System.__ComObject' to interface type 'IXXX'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{26C830E0-B0B5-7EAE-85F3-B23455654F47A}' failed due to the following error: 
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))

Then according to Hans' comment:

E_NOINTERFACE is also generated if COM cannot figure out how to marshal the interface across the process boundary. Did you forget to register the type library? If you don't use the "Register for COM interop" IDE build option then running Regasm.exe with the /tlb option is required

After registering the original tlb file, it worked like a charm. The proper way to do it is the run Regasm on the assembly which contains the interface, however in my situation there is no assembly, only a pure idl interface file, and a type library created by midl.exe. So not knowing other option, I used the regtlibv12.

Thanks all your helps



来源:https://stackoverflow.com/questions/24612691/cannot-get-interface-from-different-process-via-rot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!