Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.2 KiB

  1. // WiaDataCallback.cpp: implementation of the CWiaEventCallback class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "wiatest.h"
  6. #include "WiaEventCallback.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CWiaEventCallback::CWiaEventCallback()
  16. {
  17. m_cRef = 0;
  18. for(LONG lEventIndex = 0; lEventIndex < MAX_REGISTERED_EVENTS; lEventIndex++){
  19. if(m_pIUnkRelease[lEventIndex]){
  20. m_pIUnkRelease[lEventIndex] = NULL;
  21. }
  22. }
  23. memset(m_szWindowText,0,sizeof(m_szWindowText));
  24. }
  25. CWiaEventCallback::~CWiaEventCallback()
  26. {
  27. for(LONG lEventIndex = 0; lEventIndex < MAX_REGISTERED_EVENTS; lEventIndex++){
  28. if(m_pIUnkRelease[lEventIndex]){
  29. m_pIUnkRelease[lEventIndex]->Release();
  30. m_pIUnkRelease[lEventIndex] = NULL;
  31. }
  32. }
  33. }
  34. HRESULT _stdcall CWiaEventCallback::QueryInterface(const IID& iid, void** ppv)
  35. {
  36. *ppv = NULL;
  37. if (iid == IID_IUnknown || iid == IID_IWiaEventCallback)
  38. *ppv = (IWiaEventCallback*) this;
  39. else
  40. return E_NOINTERFACE;
  41. AddRef();
  42. return S_OK;
  43. }
  44. ULONG _stdcall CWiaEventCallback::AddRef()
  45. {
  46. InterlockedIncrement((long*) &m_cRef);
  47. return m_cRef;
  48. }
  49. ULONG _stdcall CWiaEventCallback::Release()
  50. {
  51. ULONG ulRefCount = m_cRef - 1;
  52. if (InterlockedDecrement((long*) &m_cRef) == 0)
  53. {
  54. delete this;
  55. return 0;
  56. }
  57. return ulRefCount;
  58. }
  59. HRESULT _stdcall CWiaEventCallback::ImageEventCallback(
  60. const GUID *pEventGUID,
  61. BSTR bstrEventDescription,
  62. BSTR bstrDeviceID,
  63. BSTR bstrDeviceDescription,
  64. DWORD dwDeviceType,
  65. BSTR bstrFullItemName,
  66. ULONG *plEventType,
  67. ULONG ulReserved)
  68. {
  69. TCHAR szStatusText[255];
  70. memset(szStatusText,0,sizeof(szStatusText));
  71. TSPRINTF(szStatusText,TEXT("Description: %ws\nDeviceID: %ws\nDevice Description: %ws\nDevice Type: %d\nFull Item Name: %ws"),
  72. bstrEventDescription,
  73. bstrDeviceID,
  74. bstrDeviceDescription,
  75. dwDeviceType,
  76. bstrFullItemName);
  77. if(lstrlen(m_szWindowText) == 0){
  78. SetViewWindowHandle(m_hViewWindow);
  79. }
  80. ::MessageBox(m_hViewWindow,szStatusText, m_szWindowText, MB_ICONINFORMATION);
  81. if (NULL != m_hViewWindow) {
  82. BSTR bstrTargetDevice = SysAllocString(bstrDeviceID);
  83. // handle known events
  84. if (*pEventGUID == WIA_EVENT_DEVICE_DISCONNECTED) {
  85. ::PostMessage(m_hViewWindow,WM_DEVICE_DISCONNECTED,0,(LPARAM)bstrTargetDevice);
  86. } else if (*pEventGUID == WIA_EVENT_DEVICE_CONNECTED) {
  87. ::PostMessage(m_hViewWindow,WM_DEVICE_CONNECTED,0,0);
  88. } else if (*pEventGUID == WIA_EVENT_ITEM_DELETED) {
  89. ::PostMessage(m_hViewWindow,WM_ITEM_DELETED,0,0);
  90. } else if (*pEventGUID == WIA_EVENT_ITEM_CREATED) {
  91. ::PostMessage(m_hViewWindow,WM_ITEM_CREATED,0,0);
  92. } else if (*pEventGUID == WIA_EVENT_TREE_UPDATED) {
  93. ::PostMessage(m_hViewWindow,WM_TREE_UPDATED,0,0);
  94. } else if (*pEventGUID == WIA_EVENT_STORAGE_CREATED) {
  95. ::PostMessage(m_hViewWindow,WM_STORAGE_CREATED,0,0);
  96. } else if (*pEventGUID == WIA_EVENT_STORAGE_DELETED) {
  97. ::PostMessage(m_hViewWindow,WM_STORAGE_DELETED,0,0);
  98. }
  99. }
  100. return S_OK;
  101. }
  102. void CWiaEventCallback::SetViewWindowHandle(HWND hWnd)
  103. {
  104. m_hViewWindow = hWnd;
  105. TCHAR szWindowText[MAX_PATH];
  106. memset(szWindowText,0,sizeof(szWindowText));
  107. GetWindowText(hWnd,szWindowText,(sizeof(szWindowText)/sizeof(TCHAR)));
  108. TSPRINTF(m_szWindowText,TEXT("Event Notification [%s]"),szWindowText);
  109. }
  110. void CWiaEventCallback::SetNumberOfEventsRegistered(LONG lEventsRegistered)
  111. {
  112. m_lNumEventsRegistered = lEventsRegistered;
  113. }