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

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include "Componentcomponent.h"
  8. class CMessageFilter : public IMessageFilter
  9. {
  10. public:
  11. // IUnknown
  12. ULONG __stdcall AddRef();
  13. ULONG __stdcall Release();
  14. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  15. // IMessageFilter
  16.     DWORD __stdcall HandleInComingCall(DWORD dwCallType, HTASK htaskCaller, DWORD dwTickCount, LPINTERFACEINFO lpInterfaceInfo);
  17.     DWORD __stdcall RetryRejectedCall(HTASK htaskCallee, DWORD dwTickCount, DWORD dwRejectType);
  18.     DWORD __stdcall MessagePending(HTASK htaskCallee, DWORD dwTickCount, DWORD dwPendingType);
  19. };
  20. ULONG CMessageFilter::AddRef()
  21. {
  22. return 2;
  23. }
  24. ULONG CMessageFilter::Release()
  25. {
  26. return 1;
  27. }
  28. HRESULT CMessageFilter::QueryInterface(REFIID riid, void **ppv)
  29. {
  30. if(riid == IID_IUnknown || riid == IID_IMessageFilter)
  31. *ppv = (IMessageFilter*)this;
  32. else
  33. {
  34. *ppv = NULL;
  35. return E_NOINTERFACE;
  36. }
  37. return S_OK;
  38. }
  39. DWORD CMessageFilter::HandleInComingCall(DWORD dwCallType, HTASK htaskCaller, DWORD dwTickCount, LPINTERFACEINFO lpInterfaceInfo)
  40. {
  41. return E_NOTIMPL;
  42. }
  43. DWORD CMessageFilter::RetryRejectedCall(HTASK htaskCallee, DWORD dwTickCount, DWORD dwRejectType)
  44. {
  45. cout << "Client: CMessageFilter::RetryRejectedCall" << endl;
  46. static counter = 0;
  47. static int randa = (int)((((float)rand())/RAND_MAX)*10);
  48. cout << "randa = " << randa << endl;
  49. if(counter++ < randa)
  50. return 500;
  51. return -1;
  52. }
  53. DWORD CMessageFilter::MessagePending(HTASK htaskCallee, DWORD dwTickCount, DWORD dwPendingType)
  54. {
  55. cout << "Client: CMessageFilter::MessagePending" << endl;
  56. return PENDINGMSG_WAITNOPROCESS;
  57. }
  58. void ErrorMessage(char* szMessage, HRESULT hr)
  59. {
  60.     if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
  61.         hr = HRESULT_CODE(hr);
  62.     char* szError;
  63.     if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
  64.         FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 
  65.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
  66.         (LPTSTR)&szError, 0, NULL) != 0)
  67.     {
  68.         printf("%s: (%0x) %s", szMessage, hr, szError);
  69.         LocalFree(szError);
  70.     }
  71. }
  72. void main()
  73. {
  74. srand(GetTickCount());
  75. cout << "Client: Calling CoInitialize()" << endl;
  76. CoInitialize(NULL);
  77. IMessageFilter* pMF = new CMessageFilter;
  78. IMessageFilter* pOldMF;
  79. CoRegisterMessageFilter(pMF, &pOldMF);
  80. IUnknown* pUnknown;
  81. ISum* pSum;
  82. cout << "Client: Calling CoCreateInstance() " << endl;
  83. CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnknown);
  84. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  85. HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  86. if(FAILED(hr))
  87. cout << "QueryInterface FAILED" << endl;
  88. cout << "Client: Calling Release() for pUnknown" << endl;
  89. hr = pUnknown->Release();
  90. cout << "Client: pSum = " << pSum << endl;
  91. int sum;
  92. hr = pSum->Sum(4, 9, &sum);
  93. if(SUCCEEDED(hr))
  94. cout << "Client: Calling Sum() return value is " << sum << endl;
  95. else
  96. ErrorMessage("Sum", hr);
  97. cout << "Client: Calling Release() for pSum" << endl;
  98. hr = pSum->Release();
  99. cout << "Client: Calling CoUninitialize()" << endl;
  100. CoUninitialize();
  101. }