1

我正在尝试使用工厂创建 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;
}

我的 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;
};
4

0 回答 0