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.

299 lines
10 KiB

  1. // acqmgr.h : Declaration of the CAcquisitionManager
  2. #ifndef __AcquisitionManager_H_INCLUDED
  3. #define __AcquisitionManager_H_INCLUDED
  4. #include "resource.h" // main symbols
  5. #include "acqthrd.h"
  6. #include "shmemsec.h"
  7. #include "mintrans.h"
  8. #include "stievent.h"
  9. //
  10. // Number of times we will spin before giving up on
  11. // getting the window of the previous wizard's instance
  12. //
  13. const int c_nMaxActivationRetryCount = 40;
  14. //
  15. // Amount of time to wait between efforts to obtain the previous
  16. // wizard's instance
  17. //
  18. const DWORD c_nActivationRetryWait = 500;
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CAcquisitionManager
  21. class ATL_NO_VTABLE CAcquisitionManager :
  22. public CComObjectRootEx<CComSingleThreadModel>,
  23. public CComCoClass<CAcquisitionManager, &CLSID_AcquisitionManager>,
  24. public IWiaEventCallback
  25. {
  26. public:
  27. CAcquisitionManager()
  28. {
  29. }
  30. ~CAcquisitionManager()
  31. {
  32. }
  33. DECLARE_REGISTRY_RESOURCEID(IDR_ACQUISITIONMANAGER)
  34. DECLARE_PROTECT_FINAL_CONSTRUCT()
  35. BEGIN_COM_MAP(CAcquisitionManager)
  36. COM_INTERFACE_ENTRY(IWiaEventCallback)
  37. END_COM_MAP()
  38. public:
  39. // IManager
  40. HRESULT STDMETHODCALLTYPE ImageEventCallback( const GUID *pEventGUID,
  41. BSTR bstrEventDescription,
  42. BSTR bstrDeviceID,
  43. BSTR bstrDeviceDescription,
  44. DWORD dwDeviceType,
  45. BSTR bstrFullItemName,
  46. ULONG *pulEventType,
  47. ULONG ulReserved
  48. )
  49. {
  50. WIA_PUSHFUNCTION((TEXT("CAcquisitionManager::ImageEventCallback")));
  51. //
  52. // Don't want to run if this is a scanner connection event
  53. //
  54. if (pEventGUID && *pEventGUID==WIA_EVENT_DEVICE_CONNECTED && GET_STIDEVICE_TYPE(dwDeviceType)==StiDeviceTypeScanner)
  55. {
  56. return S_FALSE;
  57. }
  58. //
  59. // Try to create or open the shared memory section.
  60. //
  61. CSharedMemorySection<HWND> *pWizardSharedMemory = new CSharedMemorySection<HWND>;
  62. if (pWizardSharedMemory)
  63. {
  64. //
  65. // Assume we'll be running the wizard.
  66. //
  67. bool bRun = true;
  68. //
  69. // Create the shared section name
  70. //
  71. CSimpleString strSharedSectionName( ACQUISITION_MANAGER_DEVICE_MUTEX_ROOT_NAME );
  72. strSharedSectionName += CSimpleStringConvert::NaturalString(CSimpleStringWide(bstrDeviceID));
  73. //
  74. // If we were able to open the memory section
  75. //
  76. if (CSharedMemorySection<HWND>::SmsOpened == pWizardSharedMemory->Open( strSharedSectionName, true ))
  77. {
  78. //
  79. // Try to get the previous instance
  80. //
  81. for (int i=0;i<c_nMaxActivationRetryCount;i++)
  82. {
  83. //
  84. // if we were able to open the shared memory section, there is already one running.
  85. // so get a mutex'ed pointer to the shared memory.
  86. //
  87. HWND *pHwnd = pWizardSharedMemory->Lock();
  88. if (pHwnd)
  89. {
  90. //
  91. // If we were able to get the pointer, get the window handle stored in it.
  92. // Set bRun to false, so we don't start up a new wizard
  93. //
  94. bRun = false;
  95. if (*pHwnd && IsWindow(*pHwnd))
  96. {
  97. //
  98. // If it is a valid window, bring it to the foreground.
  99. //
  100. SetForegroundWindow(*pHwnd);
  101. }
  102. //
  103. // Release the mutex
  104. //
  105. pWizardSharedMemory->Release();
  106. //
  107. // We found the window the window handle, so we can exit the loop now.
  108. //
  109. break;
  110. }
  111. //
  112. // Wait a while for the previous instance to be created
  113. //
  114. Sleep(c_nActivationRetryWait);
  115. }
  116. }
  117. //
  118. // We only do this if we need to open a new instance
  119. //
  120. if (bRun)
  121. {
  122. //
  123. // Prepare the event data
  124. //
  125. CEventParameters EventParameters;
  126. EventParameters.EventGUID = *pEventGUID;
  127. EventParameters.strEventDescription = static_cast<LPCWSTR>(bstrEventDescription);
  128. EventParameters.strDeviceID = static_cast<LPCWSTR>(bstrDeviceID);
  129. EventParameters.strDeviceDescription = static_cast<LPCWSTR>(bstrDeviceDescription);
  130. EventParameters.ulReserved = ulReserved;
  131. EventParameters.ulEventType = *pulEventType;
  132. EventParameters.hwndParent = NULL;
  133. EventParameters.pWizardSharedMemory = pWizardSharedMemory;
  134. //
  135. // If we are started manually, it will be with the IID_NULL event
  136. // If this is the case, we are going to treat the number stored as text as
  137. // the "parent" window handle, over which all windows will be centered
  138. //
  139. if (pEventGUID && *pEventGUID==IID_NULL)
  140. {
  141. EventParameters.hwndParent = reinterpret_cast<HWND>(static_cast<UINT_PTR>(WiaUiUtil::StringToLong(CSimpleStringConvert::NaturalString(CSimpleStringWide(bstrEventDescription)))));
  142. }
  143. //
  144. // Start up the thread that actually displays the wizard
  145. //
  146. HANDLE hThread = CAcquisitionThread::Create( EventParameters );
  147. if (hThread)
  148. {
  149. //
  150. // Prevent deletion of this structure later
  151. //
  152. pWizardSharedMemory = NULL;
  153. //
  154. // Don't need this anymore
  155. //
  156. CloseHandle(hThread);
  157. }
  158. }
  159. else
  160. {
  161. WIA_TRACE((TEXT("There is already an instance of %ws running"), bstrDeviceID ));
  162. }
  163. //
  164. // Delete this memory mapped file, to prevent leaks
  165. //
  166. if (pWizardSharedMemory)
  167. {
  168. delete pWizardSharedMemory;
  169. }
  170. }
  171. return S_OK;
  172. }
  173. };
  174. class ATL_NO_VTABLE CMinimalTransfer :
  175. public CComObjectRootEx<CComSingleThreadModel>,
  176. public CComCoClass<CMinimalTransfer, &CLSID_MinimalTransfer>,
  177. public IWiaEventCallback
  178. {
  179. public:
  180. CMinimalTransfer()
  181. {
  182. }
  183. ~CMinimalTransfer()
  184. {
  185. }
  186. DECLARE_REGISTRY_RESOURCEID(IDR_MINIMALTRANSFER)
  187. DECLARE_PROTECT_FINAL_CONSTRUCT()
  188. BEGIN_COM_MAP(CMinimalTransfer)
  189. COM_INTERFACE_ENTRY(IWiaEventCallback)
  190. END_COM_MAP()
  191. public:
  192. // IManager
  193. HRESULT STDMETHODCALLTYPE ImageEventCallback( const GUID *pEventGUID,
  194. BSTR bstrEventDescription,
  195. BSTR bstrDeviceID,
  196. BSTR bstrDeviceDescription,
  197. DWORD dwDeviceType,
  198. BSTR bstrFullItemName,
  199. ULONG *pulEventType,
  200. ULONG ulReserved
  201. )
  202. {
  203. if (pEventGUID && *pEventGUID==WIA_EVENT_DEVICE_CONNECTED && GET_STIDEVICE_TYPE(dwDeviceType)==StiDeviceTypeScanner)
  204. {
  205. return S_FALSE;
  206. }
  207. DWORD dwThreadId;
  208. _Module.Lock();
  209. HANDLE hThread = CreateThread( NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(MinimalTransferThreadProc), SysAllocString(bstrDeviceID), 0, &dwThreadId );
  210. if (hThread)
  211. {
  212. CloseHandle(hThread);
  213. return S_OK;
  214. }
  215. else
  216. {
  217. _Module.Unlock();
  218. return HRESULT_FROM_WIN32(GetLastError());
  219. }
  220. }
  221. };
  222. class ATL_NO_VTABLE CStiEventHandler :
  223. public CComObjectRootEx<CComSingleThreadModel>,
  224. public CComCoClass<CStiEventHandler, &CLSID_StiEventHandler>,
  225. public IWiaEventCallback
  226. {
  227. public:
  228. CStiEventHandler()
  229. {
  230. }
  231. ~CStiEventHandler()
  232. {
  233. }
  234. DECLARE_REGISTRY_RESOURCEID(IDR_STIEVENTHANDLER)
  235. DECLARE_PROTECT_FINAL_CONSTRUCT()
  236. BEGIN_COM_MAP(CStiEventHandler)
  237. COM_INTERFACE_ENTRY(IWiaEventCallback)
  238. END_COM_MAP()
  239. public:
  240. HRESULT STDMETHODCALLTYPE ImageEventCallback( const GUID *pEventGUID,
  241. BSTR bstrEventDescription,
  242. BSTR bstrDeviceID,
  243. BSTR bstrDeviceDescription,
  244. DWORD dwDeviceType,
  245. BSTR bstrFullItemName,
  246. ULONG *pulEventType,
  247. ULONG ulReserved
  248. )
  249. {
  250. //
  251. // Package the event data for the handler
  252. //
  253. CStiEventData StiEventData( pEventGUID, bstrEventDescription, bstrDeviceID, bstrDeviceDescription, dwDeviceType, bstrFullItemName, pulEventType, ulReserved );
  254. //
  255. // Just call the handler and return it.
  256. //
  257. return StiEventHandler( StiEventData );
  258. }
  259. };
  260. #endif //__AcquisitionManager_H_INCLUDED