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

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include "Componentcomponent.h"
  5. void main()
  6. {
  7. cout << "Client: Calling CoInitialize()" << endl;
  8. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  9. IUnknown* pUnknown = 0;
  10. ISum* pSum = 0;
  11. cout << "Client: Calling CoGetClassObject() " << endl;
  12. IClassFactory* pClassFactory = 0;
  13. CoGetClassObject(CLSID_InsideCOM, CLSCTX_LOCAL_SERVER, NULL, IID_IClassFactory, (void**)&pClassFactory);
  14. cout << "Client: CreateInstance() " << endl;
  15. pClassFactory->CreateInstance(NULL, IID_IUnknown, (void**)&pUnknown);
  16. cout << "Client: Release() " << endl;
  17. pUnknown->Release();
  18. pUnknown = 0;
  19. cout << "Client: CreateInstance again() " << endl;
  20. pClassFactory->CreateInstance(NULL, IID_IUnknown, (void**)&pUnknown);
  21. pClassFactory->Release();
  22. // cout << "Client: Calling CoCreateInstance() " << endl;
  23. // CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnknown);
  24. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  25. HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  26. if(FAILED(hr))
  27. cout << "QueryInterface FAILED" << endl;
  28. cout << "Client: Calling Release() for pUnknown" << endl;
  29. hr = pUnknown->Release();
  30. cout << "Client: pSum = " << pSum << endl;
  31. int sum;
  32. hr = pSum->Sum(4, 9, &sum);
  33. if(SUCCEEDED(hr))
  34. cout << "Client: Calling Sum() return value is " << sum << endl;
  35. myDataType test;
  36. test.x = 5;
  37. test.y = 3;
  38. hr = pSum->Sum2(test, &sum);
  39. if(SUCCEEDED(hr))
  40. cout << "Client: Calling Sum2() return value is " << sum << endl;
  41. cout << "Client: Calling Release() for pSum" << endl;
  42. hr = pSum->Release();
  43. cout << "Client: Calling CoUninitialize()" << endl;
  44. CoUninitialize();
  45. }