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

Windows编程

开发平台:

Visual C++

  1.  // client.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>  // For cout
  4. #include "Component with GITcomponent.h" // Generated by MIDL
  5. void __stdcall MyThread(IStream* pScream)
  6. {
  7. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  8. if(FAILED(hr))
  9. cout << "CoInitializeEx failed" << endl;
  10. ISum* pSum;
  11. CoGetInterfaceAndReleaseStream(pScream, IID_ISum, (void**)&pSum);
  12. for(int count = 0; count < 10; count++)
  13. {
  14. int sum;
  15. hr = pSum->Sum(count, count, &sum);
  16. cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
  17. }
  18. pSum->Release();
  19. CoUninitialize();
  20. }
  21. void main()
  22. {
  23. cout << "Client: Calling CoInitialize()" << endl;
  24. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  25. if(FAILED(hr))
  26. cout << "CoInitializeEx failed" << endl;
  27. cout << "Client: Calling CoCreateInstance()" << endl;
  28. ISum* pSum;
  29. hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_ISum, (void**)&pSum);
  30. if(FAILED(hr))
  31. cout << "CoCreateInstance failed" << endl;
  32. IStream* pStream;
  33. CoMarshalInterThreadInterfaceInStream(IID_ISum, pSum, &pStream);
  34. DWORD threadid;
  35. HANDLE thread_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)MyThread, (void*)pStream, 0, &threadid);
  36. int sum;
  37. hr = pSum->Sum(2, 3, &sum);
  38. if(SUCCEEDED(hr))
  39. cout << "Client: Calling Sum(2, 3) = " << sum << endl;
  40. hr = pSum->Release();
  41. cout << "Client: Calling pSum->Release() reference count = " << hr << endl;
  42. WaitForSingleObject(thread_handle, INFINITE);
  43. cout << "Client: Calling CoUninitialize()" << endl;
  44. CoUninitialize();
  45. }