0

我正在学习COM的基础知识。现在我正在编写进程外服务器。我编写了非常基本的服务器应用程序、dll/stub 和客户端应用程序。

如果我注册服务器并使用进程内的 CoCreateInstance 创建一个对象的实例,它可以工作:

服务器/客户端:

int _tmain(int argc, _TCHAR* argv[])
{
    IClassFactory *factory = new ISimpleServerFactory();
    DWORD classToken;

    ::CoInitialize(NULL);
    CoRegisterClassObject(
        IID_ISimpleServer, 
        factory, 
        CLSCTX_LOCAL_SERVER, 
        REGCLS_MULTIPLEUSE, 
        &classToken);

    ISimpleServer *pISimpleServer = NULL;

    HRESULT hr = CoCreateInstance(
        CLSID_CSimpleServer, 
        NULL, 
        CLSCTX_LOCAL_SERVER, 
        IID_ISimpleServer,
        (void **)&pISimpleServer);           //<===========SUCCESS

    if(SUCCEEDED(hr))
        printf("Instantiation successful\n");

    if(pISimpleServer != NULL)
        pISimpleServer->Release();

    std::cin.ignore();
    CoRevokeClassObject(classToken);
    ::CoUninitialize();
    return 0;
}

现在我尝试将其拆分为单独的应用程序:

服务器:

int _tmain(int argc, _TCHAR* argv[])
{
    IClassFactory *factory = new ISimpleServerFactory();
    DWORD classToken;

    ::CoInitialize(NULL);
    CoRegisterClassObject(
        IID_ISimpleServer, 
        factory, 
        CLSCTX_LOCAL_SERVER, 
        REGCLS_MULTIPLEUSE, 
        &classToken);

    if(SUCCEEDED(hr))
        printf("Instantiation successful\n");

    if(pISimpleServer != NULL)
        pISimpleServer->Release();

    std::cin.ignore();
    CoRevokeClassObject(classToken);
    ::CoUninitialize();
    return 0;
}

客户:

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);

    SimpleServer::ISimpleServer *pISimpleServer = NULL;

    HRESULT hr = CoCreateInstance(
        CLSID_CSimpleServer, 
        NULL, 
        CLSCTX_LOCAL_SERVER, 
        IID_ISimpleServer,
        (void **)&pISimpleServer);       // HERE IT HANGS

    if (SUCCEEDED(hr))
    {
        //*****SMTH***
    }
    else
    {
        printf("Failed to load COM object (server not loaded?)\n");
    }

    if(pISimpleServer != NULL)
        pISimpleServer->Release();

    CoUninitialize();

    std::cin.ignore();

    return 0;
}

客户端挂起运行。如果服务器没有启动,客户端类型“加载COM对象失败(服务器没有加载?)”,所以我想这不是服务器注册的问题。

但那会是什么呢?

4

1 回答 1

0

好的,所以,就像 Raymond 指出的那样,我的服务器没有发送消息。我添加了

MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0) 
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    if(_kbhit())
        break;
}

进入服务器主体,它变得更好:)

于 2013-07-13T02:12:32.167 回答