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.

337 lines
11 KiB

  1. /*-----------------------------------------------------------------------------
  2. *
  3. * File: Wia.h
  4. * Author: Samuel Clement (samclem)
  5. * Date: Thu Aug 12 11:29:07 1999
  6. * Description:
  7. * Declares the CWia class which wraps an IWiaDevMgr with a IDispatch
  8. * interface.
  9. *
  10. * Copyright (c) 1999 Microsoft Corporation
  11. *
  12. * History:
  13. * 12 Aug 1999: Created. (samclem)
  14. * 27 Aug 1999: Added, _DebugDialog for debugging only
  15. *----------------------------------------------------------------------------*/
  16. #ifndef __WIA_H_
  17. #define __WIA_H_
  18. #include "resource.h" // main symbols
  19. #include "wiaeventscp.h"
  20. #include "wiaeventscp.h"
  21. // windows event messages
  22. // signals a transfer complete, wParam = IDispatch*, lParam = BSTR
  23. extern const UINT WEM_TRANSFERCOMPLETE;
  24. class CWiaEventCallback;
  25. /*-----------------------------------------------------------------------------
  26. *
  27. * Class: CWia
  28. * Synopsis: Exposes the functionality of the IWiaDevMgr using IDispatch
  29. *
  30. *---------------------------------------------------------------------------*/
  31. class ATL_NO_VTABLE CWia :
  32. public CComObjectRootEx<CComSingleThreadModel>,
  33. public CComCoClass<CWia, &CLSID_Wia>,
  34. public IDispatchImpl<IWia, &IID_IWia, &LIBID_WIALib>,
  35. public IObjectSafetyImpl<CWia, 0 /*INTERFACESAFE_FOR_UNTRUSTED_CALLER*/>,
  36. /*public IWiaEventCallback,*/
  37. public CProxy_IWiaEvents< CWia >,
  38. public IConnectionPointContainerImpl<CWia>,
  39. public IProvideClassInfo2Impl<&CLSID_Wia, &DIID__IWiaEvents, &LIBID_WIALib>
  40. {
  41. public:
  42. CWia();
  43. DECLARE_TRACKED_OBJECT
  44. DECLARE_REGISTRY_RESOURCEID(IDR_WIA)
  45. DECLARE_PROTECT_FINAL_CONSTRUCT()
  46. BEGIN_COM_MAP(CWia)
  47. COM_INTERFACE_ENTRY(IWia)
  48. COM_INTERFACE_ENTRY(IDispatch)
  49. //COM_INTERFACE_ENTRY(IWiaEventCallback)
  50. COM_INTERFACE_ENTRY(IProvideClassInfo)
  51. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  52. COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
  53. END_COM_MAP()
  54. BEGIN_CONNECTION_POINT_MAP(CWia)
  55. CONNECTION_POINT_ENTRY(DIID__IWiaEvents)
  56. END_CONNECTION_POINT_MAP()
  57. STDMETHOD(FinalConstruct)();
  58. STDMETHOD_(void, FinalRelease)();
  59. // event methods
  60. inline LRESULT SendEventMessage( UINT iMsg, WPARAM wParam, LPARAM lParam )
  61. { return PostMessage( m_hwndEvent, iMsg, wParam, lParam ); }
  62. // IWia
  63. public:
  64. STDMETHOD(_DebugDialog)( BOOL fWait );
  65. STDMETHOD(get_Devices)( ICollection** ppCol );
  66. STDMETHOD(Create)( VARIANT* pvaDevice, IWiaDispatchItem** ppDevice );
  67. // IWiaEventCallback
  68. STDMETHOD(ImageEventCallback)( const GUID* pEventGUID, BSTR bstrEventDescription,
  69. BSTR bstrDeviceID, BSTR bstrDeviceDescription, DWORD dwDeviceType,
  70. BSTR bstrFullItemName,
  71. /*in,out*/ ULONG* pulEventType, ULONG Reserved );
  72. protected:
  73. IWiaDevMgr* m_pWiaDevMgr;
  74. ICollection* m_pDeviceCollectionCache;
  75. HWND m_hwndEvent;
  76. CComObject<CWiaEventCallback> *m_pCWiaEventCallback;
  77. // event window proc
  78. static LRESULT CALLBACK EventWndProc( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam );
  79. private:
  80. };
  81. /*-----------------------------------------------------------------------------
  82. *
  83. * Class: CWiaEventCallback
  84. * Synopsis: Exposes the functionality of the IWiaEventCallback interface
  85. * used by the CWia object to receive notifications of
  86. * device arrival/removals.
  87. *
  88. *---------------------------------------------------------------------------*/
  89. class ATL_NO_VTABLE CWiaEventCallback :
  90. public CComObjectRootEx<CComSingleThreadModel>,
  91. public IWiaEventCallback
  92. {
  93. public:
  94. BEGIN_COM_MAP(CWiaEventCallback)
  95. COM_INTERFACE_ENTRY(IWiaEventCallback)
  96. END_COM_MAP()
  97. STDMETHOD(FinalConstruct)()
  98. {
  99. m_pCWia = NULL;
  100. m_pWiaDevConCookie = NULL;
  101. m_pWiaDevDisCookie = NULL;
  102. return S_OK;
  103. }
  104. STDMETHOD_(void, FinalRelease)()
  105. {
  106. }
  107. //
  108. // This method is used to store a pointer to the owning CWia object. When the owning CWia object is going
  109. // away (i.e. in FinalRelease()), it should call this method with NULL.
  110. // Note that this method is used because the owning CWia object cannot be AddRef'd/Release'd like normal
  111. // due to the circular reference it introduces.
  112. //
  113. VOID setOwner(CWia *pCWia)
  114. {
  115. m_pCWia = pCWia;
  116. }
  117. //
  118. // Register this interface for run-time event notifications. Specifically,
  119. // we are interested in WIA_EVENT_DEVICE_CONNECTED and WIA_EVENT_DEVICE_DISCONNECTED.
  120. //
  121. HRESULT RegisterForConnectDisconnect(IWiaDevMgr *pWiaDevMgr)
  122. {
  123. HRESULT hr = S_OK;
  124. if (pWiaDevMgr)
  125. {
  126. IUnknown* pWiaDevConCookie = NULL;
  127. IUnknown* pWiaDevDisCookie = NULL;
  128. //
  129. // Register for connect events
  130. //
  131. hr = pWiaDevMgr->RegisterEventCallbackInterface(
  132. WIA_REGISTER_EVENT_CALLBACK,
  133. NULL,
  134. &WIA_EVENT_DEVICE_CONNECTED,
  135. static_cast<IWiaEventCallback*>(this),
  136. &pWiaDevConCookie);
  137. if (hr == S_OK)
  138. {
  139. //
  140. // Save the registration cookie for the connect events
  141. //
  142. m_pWiaDevConCookie = pWiaDevConCookie;
  143. //
  144. // Register for disconnect events
  145. //
  146. hr = pWiaDevMgr->RegisterEventCallbackInterface(
  147. WIA_REGISTER_EVENT_CALLBACK,
  148. NULL,
  149. &WIA_EVENT_DEVICE_DISCONNECTED,
  150. static_cast<IWiaEventCallback*>(this),
  151. &pWiaDevDisCookie);
  152. if (hr == S_OK)
  153. {
  154. //
  155. // Save the rregistration cookie for the disconnect events
  156. //
  157. m_pWiaDevDisCookie = pWiaDevDisCookie;
  158. }
  159. else
  160. {
  161. hr = E_UNEXPECTED;
  162. }
  163. }
  164. else
  165. {
  166. hr = E_UNEXPECTED;
  167. }
  168. }
  169. else
  170. {
  171. hr = E_POINTER;
  172. }
  173. return hr;
  174. }
  175. //
  176. // Ensures that this object is no longer registered for events. Note that you may safely
  177. // call this method multiple times within the lifetime of this object.
  178. //
  179. VOID UnRegisterForConnectDisconnect()
  180. {
  181. if (m_pWiaDevConCookie)
  182. {
  183. m_pWiaDevConCookie->Release();
  184. }
  185. m_pWiaDevConCookie = NULL;
  186. if (m_pWiaDevDisCookie)
  187. {
  188. m_pWiaDevDisCookie->Release();
  189. }
  190. m_pWiaDevDisCookie = NULL;
  191. }
  192. //
  193. // This is called by Wia when something interesting happens. We simply pass this to the owning
  194. // CWia object to fire these events off to scripting for them to do do something.
  195. //
  196. HRESULT STDMETHODCALLTYPE ImageEventCallback(const GUID *pEventGUID, BSTR bstrEventDescription, BSTR bstrDeviceID, BSTR bstrDeviceDescription, DWORD dwDeviceType, BSTR bstrFullItemName, ULONG *pulEventType, ULONG ulReserved)
  197. {
  198. if (m_pCWia)
  199. {
  200. HRESULT hr = m_pCWia->ImageEventCallback(pEventGUID, bstrEventDescription, bstrDeviceID, bstrDeviceDescription, dwDeviceType, bstrFullItemName, pulEventType, ulReserved);
  201. }
  202. return S_OK;
  203. }
  204. private:
  205. CWia* m_pCWia;
  206. IUnknown* m_pWiaDevConCookie;
  207. IUnknown* m_pWiaDevDisCookie;
  208. };
  209. //
  210. // Separate "safe" class wrapper
  211. //
  212. class ATL_NO_VTABLE CSafeWia :
  213. public CComObjectRootEx<CComSingleThreadModel>,
  214. public CComCoClass<CSafeWia, &CLSID_SafeWia>,
  215. public IDispatchImpl<IWia, &IID_IWia, &LIBID_WIALib>,
  216. public IObjectSafetyImpl<CSafeWia, INTERFACESAFE_FOR_UNTRUSTED_CALLER >,
  217. public CProxy_IWiaEvents< CSafeWia >,
  218. public IConnectionPointContainerImpl<CSafeWia>,
  219. public IProvideClassInfo2Impl<&CLSID_SafeWia, &DIID__IWiaEvents, &LIBID_WIALib>
  220. {
  221. public:
  222. CSafeWia();
  223. DECLARE_TRACKED_OBJECT
  224. DECLARE_REGISTRY_RESOURCEID(IDR_WIA)
  225. DECLARE_PROTECT_FINAL_CONSTRUCT()
  226. BEGIN_COM_MAP(CSafeWia)
  227. COM_INTERFACE_ENTRY(IWia)
  228. COM_INTERFACE_ENTRY(IDispatch)
  229. COM_INTERFACE_ENTRY(IProvideClassInfo)
  230. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  231. COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
  232. END_COM_MAP()
  233. BEGIN_CATEGORY_MAP(CSafeWia)
  234. IMPLEMENTED_CATEGORY(CATID_SafeForScripting)
  235. IMPLEMENTED_CATEGORY(CATID_SafeForInitializing)
  236. END_CATEGORY_MAP()
  237. BEGIN_CONNECTION_POINT_MAP(CSafeWia)
  238. CONNECTION_POINT_ENTRY(DIID__IWiaEvents)
  239. END_CONNECTION_POINT_MAP()
  240. STDMETHOD(FinalConstruct)();
  241. STDMETHOD_(void, FinalRelease)();
  242. // event methods
  243. inline LRESULT SendEventMessage( UINT iMsg, WPARAM wParam, LPARAM lParam )
  244. { return PostMessage( m_hwndEvent, iMsg, wParam, lParam ); }
  245. // IWia
  246. public:
  247. STDMETHOD(_DebugDialog)( BOOL fWait );
  248. STDMETHOD(get_Devices)( ICollection** ppCol );
  249. STDMETHOD(Create)( VARIANT* pvaDevice, IWiaDispatchItem** ppDevice );
  250. // Used to process IWiaEventCallback::ImageEventCallback messages from a CWiaEventCallback object
  251. STDMETHOD(ImageEventCallback)( const GUID* pEventGUID, BSTR bstrEventDescription,
  252. BSTR bstrDeviceID, BSTR bstrDeviceDescription, DWORD dwDeviceType,
  253. BSTR bstrFullItemName,
  254. /*in,out*/ ULONG* pulEventType, ULONG Reserved );
  255. protected:
  256. IWiaDevMgr* m_pWiaDevMgr;
  257. IUnknown* m_pWiaDevConEvent;
  258. IUnknown* m_pWiaDevDisEvent;
  259. ICollection* m_pDeviceCollectionCache;
  260. HWND m_hwndEvent;
  261. // Flag indicating whether current instance is safe , i.e. all methods should check
  262. // access rights
  263. BOOL m_SafeInstance;
  264. // event window proc
  265. static LRESULT CALLBACK EventWndProc( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam );
  266. private:
  267. BOOL IsAllowed(HRESULT *phr)
  268. {
  269. BOOL bRet = FALSE;
  270. *phr = E_FAIL;
  271. if (m_SafeInstance) {
  272. // BUGBUG Placeholder for strict access rights checks, based on client site
  273. // security zone. For now return FALSE always
  274. *phr = E_ACCESSDENIED;
  275. bRet = FALSE;
  276. }
  277. else {
  278. *phr = S_OK;
  279. bRet = TRUE;
  280. }
  281. return bRet;
  282. }
  283. };
  284. #endif //__WIA_H_