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 FTMcomponent.h" // Generated by MIDL
  5. DWORD threadid;
  6. void __stdcall MyThread(IStream* pScream)
  7. {
  8. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  9. if(FAILED(hr))
  10. cout << "CoInitializeEx failed" << endl;
  11. ISum* pSum;
  12. hr = CoGetInterfaceAndReleaseStream(pScream, IID_ISum, (void**)&pSum);
  13. if(FAILED(hr))
  14. cout << "CoGetInterfaceAndReleaseStream failed" << endl;
  15. cout << "Adding numbers 0 to 50000 in MTA thread " << GetCurrentThreadId() << endl;
  16. for(int count = 0; count < 50000; count++)
  17. {
  18. int sum;
  19. hr = pSum->Sum(count, count, &sum);
  20. // cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
  21. }
  22. pSum->Release();
  23. CoUninitialize();
  24. PostThreadMessage(threadid, WM_QUIT, 0, 0);
  25. }
  26. void main()
  27. {
  28. DWORD time = GetTickCount();
  29. threadid = GetCurrentThreadId();
  30. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  31. if(FAILED(hr))
  32. cout << "CoInitializeEx failed" << endl;
  33. ISum* pSum;
  34. hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_ISum, (void**)&pSum);
  35. if(FAILED(hr))
  36. cout << "CoCreateInstance failed" << endl;
  37. IStream* pStream;
  38. CoMarshalInterThreadInterfaceInStream(IID_ISum, pSum, &pStream);
  39. DWORD newthreadid;
  40. HANDLE thread_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)MyThread, (void*)pStream, 0, &newthreadid);
  41. cout << "Adding numbers 0 to 50000 in STA thread " << GetCurrentThreadId() << endl;
  42. for(int count = 0; count < 50000; count++)
  43. {
  44. int sum = 0;
  45. hr = pSum->Sum(count, count, &sum);
  46. // cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
  47. }
  48. hr = pSum->Release();
  49. MSG msg;
  50. while(GetMessage(&msg, 0, 0, 0))
  51. DispatchMessage(&msg);
  52. CoUninitialize();
  53. cout << "Time elapsed = " << GetTickCount() - time << " milliseconds." << endl;
  54. }