[英]CoCreateInstance hangs in single-threaded exe COM client
我正在學習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;
}
進入服務器主體,它會變得更好:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.