我正在学习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对象(服务器未加载?)”,因此我想这不是服务器注册的问题。
但是那会是什么呢?
好的,就像雷蒙德指出的那样,我的服务器未在发送消息。我加了
MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(_kbhit())
break;
}
进入服务器主体,它会变得更好:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句