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.

192 lines
6.8 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: RUNWIZ.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 6/14/2000
  12. *
  13. * DESCRIPTION: Present the device selection dialog and allow the user to select
  14. * a device, then cocreate the server and generate the connection
  15. * event.
  16. *
  17. *******************************************************************************/
  18. #include "precomp.h"
  19. #pragma hdrstop
  20. #include "simstr.h"
  21. #include "simbstr.h"
  22. #include "runwiz.h"
  23. #include "shmemsec.h"
  24. namespace RunWiaWizard
  25. {
  26. static const GUID CLSID_AcquisitionManager = { 0xD13E3F25,0x1688,0x45A0,{ 0x97,0x43,0x75,0x9E,0xB3, 0x5C,0xDF,0x9A} };
  27. HRESULT RunWizard( LPCTSTR pszDeviceId, HWND hWndParent, LPCTSTR pszUniqueIdentifier )
  28. {
  29. //
  30. // Assume failure
  31. //
  32. HRESULT hr = E_FAIL;
  33. //
  34. // Get the device ID if one was not provided
  35. //
  36. CSimpleStringWide strwDeviceId;
  37. if (!pszDeviceId || !lstrlen(pszDeviceId))
  38. {
  39. //
  40. // Assume we will be asking for the device
  41. //
  42. bool bAskForDevice = true;
  43. //
  44. // This will automatically be cleaned up when we exit this scope
  45. //
  46. CSharedMemorySection<HWND> SelectionDialogSharedMemory;
  47. //
  48. // We only want to enforce uniqueness if we have a unique ID for this instance of the UI
  49. //
  50. if (pszUniqueIdentifier && *pszUniqueIdentifier)
  51. {
  52. //
  53. // First, try to open it. If it exists, that means there is another instance running already.
  54. //
  55. CSharedMemorySection<HWND>::COpenResult OpenResult = SelectionDialogSharedMemory.Open( pszUniqueIdentifier, true );
  56. if (CSharedMemorySection<HWND>::SmsOpened == OpenResult)
  57. {
  58. //
  59. // We don't want to display the selection dialog
  60. //
  61. bAskForDevice = false;
  62. //
  63. // Tell the caller we cancelled
  64. //
  65. hr = S_FALSE;
  66. //
  67. // If we were able to open the shared memory section, there is already one running.
  68. // so get a mutex'ed pointer to the shared memory.
  69. //
  70. HWND *pHwnd = SelectionDialogSharedMemory.Lock();
  71. if (pHwnd)
  72. {
  73. //
  74. // If we were able to get the pointer, get the window handle stored in it.
  75. // Set bRun to false, so we don't start up a new wizard
  76. //
  77. if (*pHwnd && IsWindow(*pHwnd))
  78. {
  79. //
  80. // Try to get any active windows
  81. //
  82. HWND hWndPopup = GetLastActivePopup(*pHwnd);
  83. //
  84. // If it is a valid window, bring it to the foreground.
  85. //
  86. SetForegroundWindow(hWndPopup);
  87. }
  88. //
  89. // Release the mutex
  90. //
  91. SelectionDialogSharedMemory.Release();
  92. }
  93. }
  94. else if (CSharedMemorySection<HWND>::SmsCreated == OpenResult)
  95. {
  96. //
  97. // If we couldn't open it, we are the first instance, so store the parent window handle
  98. //
  99. HWND *phWnd = SelectionDialogSharedMemory.Lock();
  100. if (phWnd)
  101. {
  102. *phWnd = hWndParent;
  103. SelectionDialogSharedMemory.Release();
  104. }
  105. }
  106. }
  107. if (bAskForDevice)
  108. {
  109. //
  110. // Create the device manager
  111. //
  112. CComPtr<IWiaDevMgr> pWiaDevMgr;
  113. hr = CoCreateInstance( CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)&pWiaDevMgr );
  114. if (SUCCEEDED(hr))
  115. {
  116. //
  117. // Get the device ID
  118. //
  119. BSTR bstrDeviceId = NULL;
  120. hr = pWiaDevMgr->SelectDeviceDlgID( hWndParent, 0, 0, &bstrDeviceId );
  121. if (hr == S_OK && bstrDeviceId != NULL)
  122. {
  123. //
  124. // Save the device ID and free the bstring
  125. //
  126. strwDeviceId = bstrDeviceId;
  127. SysFreeString(bstrDeviceId);
  128. }
  129. }
  130. }
  131. }
  132. else
  133. {
  134. //
  135. // Save the provided device ID
  136. //
  137. strwDeviceId = CSimpleStringConvert::WideString(CSimpleString(pszDeviceId));
  138. }
  139. //
  140. // If we have a valid device ID, continue
  141. //
  142. if (strwDeviceId.Length())
  143. {
  144. //
  145. // Create the wizard
  146. //
  147. CComPtr<IWiaEventCallback> pWiaEventCallback;
  148. hr = CoCreateInstance( CLSID_AcquisitionManager, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaEventCallback, (void**)&pWiaEventCallback );
  149. if (SUCCEEDED(hr))
  150. {
  151. //
  152. // Convert the parent window handle to a string, which we will pass as the event description
  153. // The wizard will only use it this way if the event GUID is IID_NULL
  154. //
  155. CSimpleBStr bstrParentWindow( CSimpleString().Format( TEXT("%d"), hWndParent ) );
  156. //
  157. // Allow this process to set the foreground window
  158. //
  159. CoAllowSetForegroundWindow( pWiaEventCallback, NULL );
  160. //
  161. // Call the callback function
  162. //
  163. ULONG ulEventType = 0;
  164. hr = pWiaEventCallback->ImageEventCallback( &IID_NULL,
  165. bstrParentWindow.BString(),
  166. CSimpleBStr(strwDeviceId),
  167. NULL,
  168. 0,
  169. NULL,
  170. &ulEventType,
  171. 0);
  172. }
  173. }
  174. return hr;
  175. }
  176. }