Leaked source code of windows server 2003
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.

177 lines
5.5 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: GWIAEVNT.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 12/29/1999
  12. *
  13. * DESCRIPTION: Generic reusable WIA event handler that posts the specified
  14. * message to the specified window.
  15. *
  16. * The message will be sent with the following arguments:
  17. *
  18. *
  19. * WPARAM = NULL
  20. * LPARAM = CGenericWiaEventHandler::CEventMessage *pEventMessage
  21. *
  22. * pEventMessage MUST be freed in the message handler using delete
  23. *
  24. * pEventMessage is allocated using an overloaded new operator, to ensure that
  25. * the same allocator and de-allocator are used.
  26. *
  27. *******************************************************************************/
  28. #include "precomp.h"
  29. #pragma hdrstop
  30. #include "gwiaevnt.h"
  31. CGenericWiaEventHandler::CGenericWiaEventHandler(void)
  32. : m_hWnd(NULL),
  33. m_nWiaEventMessage(0),
  34. m_cRef(0)
  35. {
  36. }
  37. STDMETHODIMP CGenericWiaEventHandler::Initialize( HWND hWnd, UINT nWiaEventMessage )
  38. {
  39. m_hWnd = hWnd;
  40. m_nWiaEventMessage = nWiaEventMessage;
  41. return S_OK;
  42. }
  43. STDMETHODIMP CGenericWiaEventHandler::QueryInterface( REFIID riid, LPVOID *ppvObject )
  44. {
  45. WIA_PUSHFUNCTION(TEXT("CWiaDefaultUI::QueryInterface"));
  46. if (IsEqualIID( riid, IID_IUnknown ))
  47. {
  48. *ppvObject = static_cast<IWiaEventCallback*>(this);
  49. }
  50. else if (IsEqualIID( riid, IID_IWiaEventCallback ))
  51. {
  52. *ppvObject = static_cast<IWiaEventCallback*>(this);
  53. }
  54. else
  55. {
  56. *ppvObject = NULL;
  57. return(E_NOINTERFACE);
  58. }
  59. reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();
  60. return(S_OK);
  61. }
  62. STDMETHODIMP_(ULONG) CGenericWiaEventHandler::AddRef(void)
  63. {
  64. DllAddRef();
  65. return(InterlockedIncrement(&m_cRef));
  66. }
  67. STDMETHODIMP_(ULONG) CGenericWiaEventHandler::Release(void)
  68. {
  69. DllRelease();
  70. LONG nRefCount = InterlockedDecrement(&m_cRef);
  71. if (!nRefCount)
  72. {
  73. delete this;
  74. }
  75. return(nRefCount);
  76. }
  77. STDMETHODIMP CGenericWiaEventHandler::ImageEventCallback( const GUID *pEventGUID, BSTR bstrEventDescription, BSTR bstrDeviceID, BSTR bstrDeviceDescription, DWORD dwDeviceType, BSTR bstrFullItemName, ULONG *pulEventType, ULONG ulReserved )
  78. {
  79. WIA_PUSHFUNCTION(TEXT("CGenericWiaEventHandler::ImageEventCallback"));
  80. //
  81. // Make sure (as best we can) that everything is OK before we allocate any memory
  82. //
  83. if (m_hWnd && m_nWiaEventMessage && IsWindow(m_hWnd))
  84. {
  85. //
  86. // Allocate the new message
  87. //
  88. CEventMessage *pEventMessage = new CEventMessage( *pEventGUID, bstrEventDescription, bstrDeviceID, bstrDeviceDescription, dwDeviceType, bstrFullItemName );
  89. if (pEventMessage)
  90. {
  91. //
  92. // Send the message to the notify window
  93. //
  94. LRESULT lRes = SendMessage( m_hWnd, m_nWiaEventMessage, NULL, reinterpret_cast<LPARAM>(pEventMessage) );
  95. //
  96. // If the callee didn't handle the message, delete it
  97. //
  98. if (HANDLED_EVENT_MESSAGE != lRes)
  99. {
  100. delete pEventMessage;
  101. }
  102. }
  103. }
  104. return S_OK;
  105. }
  106. HRESULT CGenericWiaEventHandler::RegisterForWiaEvent( LPCWSTR pwszDeviceId, const GUID &guidEvent, IUnknown **ppUnknown, HWND hWnd, UINT nMsg )
  107. {
  108. WIA_PUSHFUNCTION(TEXT("CGenericWiaEventHandler::RegisterForWiaEvent"));
  109. //
  110. // Create the device manager
  111. //
  112. CComPtr<IWiaDevMgr> pWiaDevMgr;
  113. HRESULT hr = CoCreateInstance( CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)&pWiaDevMgr );
  114. if (SUCCEEDED(hr) && pWiaDevMgr)
  115. {
  116. //
  117. // Create our event handler
  118. //
  119. CGenericWiaEventHandler *pEventHandler = new CGenericWiaEventHandler();
  120. if (pEventHandler)
  121. {
  122. //
  123. // Initialize it with the window handle and message we will be sending
  124. //
  125. hr = pEventHandler->Initialize( hWnd, nMsg );
  126. if (SUCCEEDED(hr))
  127. {
  128. //
  129. // Get the callback interface pointer
  130. //
  131. CComPtr<IWiaEventCallback> pWiaEventCallback;
  132. hr = pEventHandler->QueryInterface( IID_IWiaEventCallback, (void**)&pWiaEventCallback );
  133. if (SUCCEEDED(hr) && pWiaEventCallback)
  134. {
  135. //
  136. // Register for the event
  137. //
  138. hr = pWiaDevMgr->RegisterEventCallbackInterface( 0, pwszDeviceId ? CSimpleBStr(pwszDeviceId).BString() : NULL, &guidEvent, pWiaEventCallback, ppUnknown );
  139. if (!SUCCEEDED(hr))
  140. {
  141. WIA_PRINTHRESULT((hr,TEXT("pWiaDevMgr->RegisterEventCallbackInterface failed")));
  142. }
  143. }
  144. else
  145. {
  146. WIA_PRINTHRESULT((hr,TEXT("pEventHandler->QueryInterface( IID_IWiaEventCallback, ... ) failed")));
  147. }
  148. }
  149. else
  150. {
  151. WIA_PRINTHRESULT((hr,TEXT("pEventHandler->Initialize failed")));
  152. }
  153. }
  154. else
  155. {
  156. hr = E_OUTOFMEMORY;
  157. WIA_PRINTHRESULT((hr,TEXT("Unable to allocate pEventHandler")));
  158. }
  159. }
  160. else
  161. {
  162. WIA_PRINTHRESULT((hr,TEXT("CoCreateInstance of dev mgr failed")));
  163. }
  164. return hr;
  165. }