进程外COM工厂CreateInstance返回E_UNEXPECTED

时间:2013-08-01 07:29:42

标签: c++ com

我正在尝试使用factory创建COM对象的实例。

我的服务器代码是:

int main(int, char* argv[])
{
    HRESULT result = CoInitializeEx(NULL, COINIT_MULTITHREADED);

    Factory factory;
    DWORD regID = 0;
    result = CoRegisterClassObject(CLSID_InterractionInterfaceFactory, (IClassFactory*) &factory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &regID);

    MSG ms;
    while(GetMessage(&ms, 0, 0, 0))
    {
        TranslateMessage(&ms);
        DispatchMessage(&ms);
    }

    CoRevokeClassObject(regID);

    CoUninitialize();

    return 0;
}

我的客户代码是:

int main(int, char*[])
{
    HRESULT result;
    result = CoInitialize(NULL);

    IClassFactory* factory = NULL;

    result = CoGetClassObject(CLSID_InterractionInterfaceFactory, CLSCTX_LOCAL_SERVER, NULL, IID_IClassFactory, (void**)&factory);

    if (S_OK == result)
    {
        IInterractionInterface* iface = NULL;
        result = factory->CreateInstance(NULL, IID_InterractionInterface, (void**)&iface);
        **// HERE IT RETURNS E_UNEXPECTED**
    }

    CoUninitialize();

    return 0;
}

My QueryInterface方法:

STDMETHODIMP InterractionInterface::QueryInterface(REFIID riid, LPVOID FAR * ppv)
{
    *ppv = NULL;

    if (riid == IID_IUnknown)
    {
        *ppv = (IUnknown*)this;
    }
    if (riid == IID_InterractionInterface)
    {
        *ppv = (IInterractionInterface*)this;
    }
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
    return S_OK;
}

我无法理解我做错了什么。它进入QueryInterface,对象有效,引用计数器递增,我从Factory :: CreateInstance(...,LPVOID * obj)S_OK返回,并且obj成功填充,但客户端获取空对象并返回代码E_UNEXPECTED。

我做错了什么?

我的意见是:

DEFINE_GUID(CLSID_InterractionInterfaceFactory, 0x84d55f4a, 0xff3c, 0x43b2, 0xa1, 0xa3, 0x3a, 0xd3, 0xe9, 0x96, 0xc4, 0x26);

DEFINE_GUID(IID_InterractionInterface, 0xc191f3ac, 0x79e1, 0x4be3, 0xbb, 0x87, 0xac, 0x7a, 0xb8, 0x24, 0xd6, 0xb9);


class IInterractionInterface : public IUnknown
{
public:
    STDMETHOD (Test(int)) PURE;
};

0 个答案:

没有答案