2013-07-12 31 views
0

我正在學習COM的基礎知識。現在我正在編寫進程外服務器。 我寫了非常基本的服務器應用程序,DLL /存根和客戶端應用程序。CoCreateInstance掛在單線程EXE COM客戶端

如果我註冊服務器並創建過程中的使用CoCreateInstance的一個對象的實例,它的工作原理:

SERVER/CLIENT:

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; 
} 

現在我嘗試它分割成獨立的程序:

SERVER:

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對象(服務器未加載?)」,所以我想這不是服務器註冊的問題。

但是它會是什麼呢?

+0

您的服務器不抽的消息。 –

+0

這是什麼意思? –

+1

一個運行在單線程單元中的線程(當你調用':: CoInitialize(NULL)'時,你加入了這個線程)決不能阻塞,而是必須運行一個消息泵 - 也就是調用GetMessage和DispatchMessage (至少)在一個循環中。傳入的跨公寓COM調用以窗口消息的形式發送到STA線程,所述線程必須檢索並處理。 –

回答

0

好吧,就像雷蒙德指出的那樣,我的服務器並沒有抽出信息。我加

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

到服務器的身體和它變得更好:)