我正在学习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对象失败(服务器没有加载?)”,所以我想这不是服务器注册的问题。
但那会是什么呢?