client with CoGetClassObject.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #include <iostream.h>
  3. #include "Component with registrationcomponent.h" // Generated by MIDL
  4. // {10000002-0000-0000-0000-000000000001}
  5. const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
  6. void main()
  7. {
  8. cout << "Client: Calling CoInitialize()" << endl;
  9. HRESULT hr = CoInitialize(NULL);
  10. if(FAILED(hr))
  11. cout << "CoInitialize failed" << endl;
  12. IUnknown* pUnknown;
  13. ISum* pSum;
  14. // cout << "Client: Calling CoCreateInstance()" << endl;
  15. // hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
  16. // if(FAILED(hr))
  17. // cout << "CoCreateInstance failed" << endl;
  18. IClassFactory* pClassFactory;
  19. hr = CoGetClassObject(CLSID_InsideCOM, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, (void**)&pClassFactory);
  20. if(FAILED(hr))
  21. cout << "CoGetClassObject failed" << endl;
  22. pClassFactory->CreateInstance(NULL, IID_IUnknown, (void**)&pUnknown);
  23. pClassFactory->Release();
  24. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  25. hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  26. if(FAILED(hr))
  27. cout << "IID_ISum not supported" << endl;
  28. hr = pUnknown->Release();
  29. cout << "Client: Calling pUnknown->Release() reference count = " << hr << endl;
  30. int sum;
  31. hr = pSum->Sum(2, 3, &sum);
  32. if(SUCCEEDED(hr))
  33. cout << "Client: Calling Sum(2, 3) = " << sum << endl;
  34. hr = pSum->Release();
  35. cout << "Client: Calling pSum->Release() reference count = " << hr << endl;
  36. cout << "Client: Calling CoUninitialize()" << endl;
  37. CoUninitialize();
  38. }