How to demo Async COM? QI(IID_ICallFactory) returns E_NOINTERFACE!
I'm trying to follow the instructions in "The Essence of COM" 3rd Edition by
David Platt page 224 and line 40 (see my listing below) is returning
NO_INTERFACE. I"ve google searched and can find no help.
I'm using VS2005 on XP Server 2003 sp1. I tried to update VS2005 it but some
how FireFox became my browser and VS2005 fires up the Microsoft Update page
with Firefox instead of IE and this page does not work with Firefox. How do
restore IE as the default browser so I can update VS2005?
What am I doing wrong in the listing below?
(1) Why does not the #import support the aynchronous interface? Can I make
#import work? I found the AsychIPlain defined in sum_array.h indicating that
I had correctly inserted the async_uuid in the IDL so I used that instead.
(2) How do I use smart pointers with Async COM?
Thanks,
Siegfried
1 // client_console.cpp : Defines the entry point for the console
application.
2 //
3
4 #include "stdafx.h"
5 #include "client_console.h"
6
7 #ifdef _DEBUG
8 #define new DEBUG_NEW
9 #endif
10 //#import "..\sum_array\Debug\sum_array.dll"
11 #include "..\sum_array\sum_array.h"
12 //_COM_SMARTPTR_TYPEDEF(ICallFactory, __uuidof(ICallFactory));
13 //_COM_SMARTPTR_TYPEDEF(IPlain, __uuidof(IPlain));
14
15 // The one and only application object
16
17 CWinApp theApp;
18
19 using namespace std;
20 struct COM { COM(){::CoInitialize(0); } ~COM(){
::CoUninitialize(); }}g_COM;
21 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
22 {
23 int nRetCode = 0;
24
25 // initialize MFC and print and error on failure
26 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
27 {
28 // TODO: change error code to suit your needs
29 _tprintf(_T("Fatal Error: MFC initialization failed\n"));
30 nRetCode = 1;
31 }
32 else
33 {
34 IPlain *pPlain;
35 HRESULT hr = ::CoCreateInstance(__uuidof(Plain), 0, CLSCTX_ALL,
__uuidof(IPlain), (void**)&pPlain);
36 if(SUCCEEDED(hr)){
37 std::cout<<"Creation succeeded!"<<std::endl;
38 pPlain->add(1);
39 ICallFactory* pCallFactory=0;
40 if(SUCCEEDED(hr = pPlain->QueryInterface(IID_ICallFactory,
(void**)&pCallFactory))){
41 std::cout<<"ICallFactory QI succeeded"<<std::endl;
42 AsyncIPlain* pAsync;
43 if(hr = pCallFactory->CreateCall(__uuidof(AsyncIPlain),
44 0,
45 __uuidof(AsyncIPlain),
46 (IUnknown**)pAsync))
47 {
48 std::cout<<"ICallFactory QI succeeded"<<std::endl;
49 pAsync->Begin_add(10);
50 int jj = 0;
51 while(FAILED(hr = pAsync->Finish_add()))
52 {
53 ::Sleep(500);
54 std::cout<<"Waiting for add jj = "<<
jj++<<std::endl;
55 }
56 pAsync->Release();
57 }
58 pCallFactory->Release();
59 }else{
60 std::cout<<" QI(IID_ICallFactory) failed
hr="<<std::hex<<hr<<std::endl;
61 }
62 pPlain->Release();
63 }
64 }
65 std::cout<<"Press any key to continue!"<<std::endl;
66 getch();
67 return nRetCode;
68 }
69