无法通过ROT从不同的进程获取接口 [英] Cannot get interface from different process via ROT

查看:265
本文介绍了无法通过ROT从不同的进程获取接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个.exe,它会注册到ROT。

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

我在上面启动.exe,它正在运行。

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

然后我启动另一个.exe,尝试获取XXX。

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?!

如果我调用t.GetProperties XXX在哪里?调用t.InvokeMember(XXX...)成功!

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

任何帮助都感谢,谢谢。

Any help is appreciated, thanks.

推荐答案

感谢 Hans Passant 提示,我可以解决问题。
首先我使用直接转换(Interop_MyApp.IXXX)r 得到更详细的错误:

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

然后根据Hans的注释:

Then according to Hans' comment:

如果COM无法弄清如何封装跨过程边界的接口,也会生成E_NOINTERFACE。您忘记注册类型库吗?如果不使用Register for COM interopIDE构建选项,则需要使用/ tlb选项运行Regasm.exe

注册原始的tlb文件,它工作像一个魅力。
正确的方法是在包含接口的程序集上运行Regasm,但是在我的情况下没有程序集,只有一个纯idl接口文件和一个由midl.exe创建的类型库。
所以不知道其他选项,我使用regtlibv12。

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.

感谢您的帮助

这篇关于无法通过ROT从不同的进程获取接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆