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

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define TypeSaferQI(ppObject) QueryInterface(__uuidof(*ppObject), (void**)ppObject)
  3. #define _WIN32_DCOM
  4. #include <iostream.h>
  5. #include <stdio.h>
  6. #include "Component with Registrationcomponent.h"
  7. // {10000002-0000-0000-0000-000000000001}
  8. const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
  9. void main()
  10. {
  11. cout << "Client: Calling CoInitialize()" << endl;
  12. CoInitialize(NULL);
  13. IUnknown* pUnknown;
  14. ISum* pSum;
  15. cout << "Client: Calling CoCreateInstance() " << endl;
  16. CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
  17. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  18. // HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  19. HRESULT hr = pUnknown->TypeSaferQI(&pSum);
  20. if(FAILED(hr))
  21. cout << "QueryInterface FAILED" << endl;
  22. cout << "Client: Calling Release() for pUnknown" << endl;
  23. hr = pUnknown->Release();
  24. cout << "Client: pSum = " << pSum << endl;
  25. int sum;
  26. hr = pSum->Sum(123, 43, &sum);
  27. if(SUCCEEDED(hr))
  28. cout << "Client: Calling Sum() return value is " << sum << endl;
  29. cout << "Client: Calling Release() for pSum" << endl;
  30. hr = pSum->Release();
  31. cout << "Client: Calling CoUninitialize()" << endl;
  32. CoUninitialize();
  33. }