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.

484 lines
15 KiB

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "msgwrap.h"
  4. #include "clistbox.h"
  5. #ifdef _OVERRIDE_LIST_BOXES
  6. //////////////////////////////////////////////////////////////////////////
  7. //
  8. // extern global functions
  9. // Details: DeviceListBox() - Controls the WIA device list box
  10. // EventListBox() - Controls the WIA event list box
  11. //
  12. //////////////////////////////////////////////////////////////////////////
  13. extern LRESULT CALLBACK DeviceListBox(HWND, UINT, WPARAM, LPARAM);
  14. extern WNDPROC DefDeviceListBox;
  15. #endif
  16. #define _REGISTER_ON
  17. //////////////////////////////////////////////////////////////////////////
  18. //
  19. // Function: CMessageWrapper()
  20. // Details: Constructor
  21. //
  22. //
  23. //////////////////////////////////////////////////////////////////////////
  24. CMessageWrapper::CMessageWrapper()
  25. {
  26. m_hInstance = NULL;
  27. m_hSmallIcon = NULL;
  28. }
  29. //////////////////////////////////////////////////////////////////////////
  30. //
  31. // Function: ~CMessageWrapper()
  32. // Details: Destructor
  33. //
  34. //
  35. //////////////////////////////////////////////////////////////////////////
  36. CMessageWrapper::~CMessageWrapper()
  37. {
  38. //
  39. // free Icon??? (m_hSmallIcon)
  40. //
  41. }
  42. //////////////////////////////////////////////////////////////////////////
  43. //
  44. // Function: Initialize()
  45. // Details: This function handles all initialization for the message wrapper
  46. //
  47. // hInstance - handle to the application's instance
  48. //
  49. //////////////////////////////////////////////////////////////////////////
  50. VOID CMessageWrapper::Initialize(HINSTANCE hInstance)
  51. {
  52. m_hInstance = hInstance;
  53. }
  54. //////////////////////////////////////////////////////////////////////////
  55. //
  56. // Function: OnInitDialog()
  57. // Details: This function handles all initialization for the dialog.
  58. // This includes control initialization.
  59. //
  60. // hDlg - handle to the dialog's window
  61. //
  62. //////////////////////////////////////////////////////////////////////////
  63. BOOL CMessageWrapper::OnInitDialog(HWND hDlg)
  64. {
  65. TCHAR szString[255];
  66. //
  67. // Set dialog's title
  68. //
  69. if(!SetWindowText(hDlg,GetResourceString(IDS_DIALOG_TITLE, szString))) {
  70. Trace(TEXT("Could not set dialog's window title."));
  71. }
  72. //
  73. // Set dialog's small icon
  74. //
  75. m_hSmallIcon = LoadIcon(m_hInstance,MAKEINTRESOURCE(IDI_SMALL));
  76. if(m_hSmallIcon) {
  77. SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)m_hSmallIcon);
  78. } else {
  79. Trace(TEXT("Could not load Small icon from dialog resource."));
  80. }
  81. //
  82. // Initialize WIA Device List box
  83. //
  84. if(!OnRefreshDeviceListBox(hDlg)){
  85. EnableAllControls(hDlg,FALSE);
  86. } else {
  87. EnableAllControls(hDlg,TRUE);
  88. }
  89. #ifdef _OVERRIDE_LIST_BOXES
  90. HWND hListBox = NULL;
  91. hListBox = GetDlgItem(hDlg,IDC_WIA_DEVICE_LIST);
  92. if(NULL != hListBox) {
  93. DefDeviceListBox = (WNDPROC)GetWindowLongPtr(hListBox,GWL_WNDPROC);
  94. SetWindowLongPtr(hListBox,GWL_WNDPROC,(LONG_PTR)DeviceListBox);
  95. OnRefreshDeviceListBox(hDlg);
  96. }
  97. #endif
  98. return TRUE;
  99. }
  100. //////////////////////////////////////////////////////////////////////////
  101. //
  102. // Function: OnAbout()
  103. // Details: This function handles the "About" information for the dialog.
  104. //
  105. // hDlg - handle to the dialog's window
  106. //
  107. //////////////////////////////////////////////////////////////////////////
  108. BOOL CMessageWrapper::OnAbout(HWND hDlg)
  109. {
  110. TCHAR szString[255];
  111. TCHAR szStringTitle[255];
  112. MessageBox(hDlg,GetResourceString(IDS_DIALOG_ABOUT_TEXT, szString),
  113. GetResourceString(IDS_DIALOG_ABOUT_TITLE, szStringTitle),MB_OK);
  114. return TRUE;
  115. }
  116. //////////////////////////////////////////////////////////////////////////
  117. //
  118. // Function: OnExit()
  119. // Details: This function handles the exiting for the dialog.
  120. //
  121. // hDlg - handle to the dialog's window
  122. // wParam - WPARAM parameter (used for windows data/argument passing)
  123. //
  124. //////////////////////////////////////////////////////////////////////////
  125. BOOL CMessageWrapper::OnExit(HWND hDlg, WPARAM wParam)
  126. {
  127. //
  128. // clean up any things here, and exit using the Window's API EndDialog()
  129. //
  130. return EndDialog(hDlg, LOWORD(wParam));
  131. }
  132. //////////////////////////////////////////////////////////////////////////
  133. //
  134. // Function: OnBrowse()
  135. // Details: This function handles the Browse functionality for the dialog.
  136. //
  137. // hDlg - handle to the dialog's window
  138. // szApplicationFilePath - File path to selected application
  139. //
  140. //////////////////////////////////////////////////////////////////////////
  141. BOOL CMessageWrapper::OnBrowse(HWND hDlg, LPTSTR szApplicationFilePath)
  142. {
  143. OPENFILENAME ofn; // common dialog box structure
  144. TCHAR szString[255]; // string for title display
  145. // Initialize OPENFILENAME
  146. char szFile[260]; // buffer for file name
  147. char szBrowseDialogTitle[260]; // buffer for dialog title
  148. ZeroMemory(szFile,sizeof(szFile));
  149. ZeroMemory(szBrowseDialogTitle,sizeof(szBrowseDialogTitle));
  150. GetResourceString(IDS_DIALOG_BROWSE_TITLE,szBrowseDialogTitle,sizeof(szBrowseDialogTitle));
  151. HWND hListBox = NULL;
  152. hListBox = GetDlgItem(hDlg,IDC_WIA_DEVICE_LIST);
  153. if(NULL != hListBox) {
  154. CListBoxUtil DevListBox(hListBox);
  155. BSTR bstrDeviceID = NULL;
  156. DevListBox.GetCurSelTextAndData(szString,(void**)&bstrDeviceID);
  157. lstrcat(szBrowseDialogTitle,szString);
  158. } else {
  159. return FALSE;
  160. }
  161. ZeroMemory(&ofn, sizeof(OPENFILENAME));
  162. ofn.lStructSize = sizeof(OPENFILENAME);
  163. ofn.hwndOwner = hDlg;
  164. ofn.lpstrFile = szFile;
  165. ofn.nMaxFile = sizeof(szFile);
  166. ofn.lpstrFilter = "*.EXE\0*.EXE\0";
  167. ofn.nFilterIndex = 1;
  168. ofn.lpstrFileTitle = NULL;
  169. ofn.nMaxFileTitle = 0;
  170. ofn.lpstrInitialDir = NULL;
  171. ofn.lpstrTitle = szBrowseDialogTitle;
  172. ofn.Flags = 0;
  173. //
  174. // Display the Open dialog box.
  175. //
  176. if (GetOpenFileName(&ofn) == TRUE){
  177. lstrcpy(szApplicationFilePath, szFile);
  178. } else {
  179. return FALSE;
  180. }
  181. return TRUE;
  182. }
  183. //////////////////////////////////////////////////////////////////////////
  184. //
  185. // Function: OnRefreshDeviceListBox()
  186. // Details: This function handles refreshing the WIA device ListBox for the dialog.
  187. //
  188. // hDlg - handle to the dialog's window
  189. //
  190. //////////////////////////////////////////////////////////////////////////
  191. BOOL CMessageWrapper::OnRefreshDeviceListBox(HWND hDlg)
  192. {
  193. HWND hListBox = NULL;
  194. int iDeviceCount = 0;
  195. //
  196. // grab the WIA device list box
  197. //
  198. hListBox = GetDlgItem(hDlg,IDC_WIA_DEVICE_LIST);
  199. if(NULL != hListBox) {
  200. //
  201. // setup utils, and continue
  202. //
  203. CListBoxUtil DevListBox(hListBox);
  204. //
  205. // clean device listbox
  206. //
  207. DevListBox.ResetContent();
  208. HRESULT hr = S_OK;
  209. ULONG ulFetched = 0;
  210. IWiaDevMgr *pIWiaDevMgr = NULL;
  211. hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER,
  212. IID_IWiaDevMgr,(void**)&pIWiaDevMgr);
  213. if(SUCCEEDED(hr)){
  214. if(NULL != pIWiaDevMgr){
  215. IWiaPropertyStorage *pIWiaPropStg = NULL;
  216. IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
  217. //
  218. // enumerate WIA devices
  219. //
  220. hr = pIWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL,&pWiaEnumDevInfo);
  221. if (SUCCEEDED(hr)){
  222. //
  223. // call reset, just in case
  224. //
  225. hr = pWiaEnumDevInfo->Reset();
  226. if (SUCCEEDED(hr)) {
  227. do {
  228. //
  229. // call NEXT()
  230. //
  231. hr = pWiaEnumDevInfo->Next(1,&pIWiaPropStg,&ulFetched);
  232. if (hr == S_OK) {
  233. if(ulFetched > 0){
  234. //
  235. // we have a device, so increment the
  236. // device counter
  237. //
  238. iDeviceCount++;
  239. PROPSPEC PropSpec[2];
  240. PROPVARIANT PropVar [2];
  241. //
  242. // clean the propvar
  243. //
  244. memset(PropVar,0,sizeof(PropVar));
  245. PropSpec[0].ulKind = PRSPEC_PROPID;
  246. PropSpec[0].propid = WIA_DIP_DEV_ID;
  247. PropSpec[1].ulKind = PRSPEC_PROPID;
  248. PropSpec[1].propid = WIA_DIP_DEV_NAME;
  249. //
  250. // read the device name, and device ID
  251. //
  252. hr = pIWiaPropStg->ReadMultiple(sizeof(PropSpec)/sizeof(PROPSPEC),
  253. PropSpec,
  254. PropVar);
  255. if (hr == S_OK) {
  256. //
  257. // write device name to the listbox, and save the
  258. // device ID for later use (EVENT ENUMERATION)
  259. //
  260. Trace(TEXT("Device Name: %ws"),PropVar[1].bstrVal);
  261. Trace(TEXT("Device ID: %ws"),PropVar[0].bstrVal);
  262. //
  263. // convert the BSTR to a CHAR, and copy the BSTR
  264. // for later use (DEVICE CREATION)
  265. //
  266. TCHAR szString[255];
  267. sprintf(szString,TEXT("%ws"),PropVar[1].bstrVal);
  268. BSTR bstrDeviceID = SysAllocString(PropVar[0].bstrVal);
  269. //
  270. // add information to the listbox
  271. //
  272. DevListBox.AddStringAndData(szString,(void*)bstrDeviceID);
  273. //
  274. // free propvariant array
  275. //
  276. FreePropVariantArray(sizeof(PropSpec)/sizeof(PROPSPEC),PropVar);
  277. //
  278. // release property storage
  279. //
  280. pIWiaPropStg->Release();
  281. pIWiaPropStg = NULL;
  282. } else
  283. Trace(TEXT("ReadMultiple() Failed while reading device name,server,and deviceID"));
  284. } else {
  285. //
  286. // force enumeration to exit cleanly
  287. //
  288. hr = S_FALSE;
  289. }
  290. } else if (hr == S_FALSE) {
  291. //
  292. // end of enumeration
  293. //
  294. } else
  295. Trace(TEXT("Next() Failed requesting 1 item"));
  296. } while (hr == S_OK);
  297. } else
  298. Trace(TEXT("Reset() Failed"));
  299. } else{
  300. Trace(TEXT("EnumDeviceInfo Failed"));
  301. return FALSE;
  302. }
  303. } else {
  304. Trace(TEXT("WIA Device Manager is NULL"));
  305. return FALSE;
  306. }
  307. //
  308. // release WIA device manager
  309. //
  310. if(pIWiaDevMgr){
  311. pIWiaDevMgr->Release();
  312. pIWiaDevMgr = NULL;
  313. }
  314. }
  315. //
  316. // if no WIA devices were found during enumeration
  317. // set a nice message inthe list box for the users to
  318. // see.
  319. //
  320. if(iDeviceCount == 0){
  321. TCHAR szString[255];
  322. GetResourceString(IDS_NO_WIA_DEVICES, szString, sizeof(szString));
  323. DevListBox.AddStringAndData(szString,NULL);
  324. //
  325. // always default to the first selection in the listbox
  326. //
  327. DevListBox.SetCurSel(0);
  328. return FALSE; // no devices
  329. }
  330. //
  331. // always default to the first selection in the listbox
  332. //
  333. DevListBox.SetCurSel(0);
  334. return TRUE;
  335. }
  336. return FALSE;
  337. }
  338. //////////////////////////////////////////////////////////////////////////
  339. //
  340. // Function: EnableAllControls()
  341. // Details: This function enables/disables buttons, on the main dialog.
  342. //
  343. // bEnable - Resource ID of the Error Code string
  344. // hDlg - Handle to parent window
  345. //
  346. //////////////////////////////////////////////////////////////////////////
  347. VOID CMessageWrapper::EnableAllControls(HWND hDlg, bool bEnable)
  348. {
  349. HWND hWindow = NULL;
  350. }
  351. //////////////////////////////////////////////////////////////////////////
  352. //
  353. // Function: DisplayError()
  354. // Details: This function fills a string, loaded from the application's
  355. // resource, and display's it as an error dialog to the user.
  356. //
  357. // ErrorCode - Resource ID of the Error Code string
  358. //
  359. //////////////////////////////////////////////////////////////////////////
  360. VOID CMessageWrapper::DisplayError(INT ErrorCode)
  361. {
  362. TCHAR szString[255];
  363. GetResourceString(ErrorCode, szString);
  364. #ifdef _ERROR_POPUP
  365. TCHAR szStringTitle[255];
  366. MessageBox(NULL,szString,
  367. GetResourceString(IDS_DIALOG_ERROR_TITLE, szStringTitle),
  368. MB_OK|MB_ICONERROR);
  369. #endif
  370. Trace(TEXT("Error Dialog: %s\n"),szString);
  371. }
  372. //////////////////////////////////////////////////////////////////////////
  373. //
  374. // Function: GetResourceString()
  375. // Details: This function fills a string, loaded from the application's
  376. // resource.
  377. //
  378. // ResourceID - Resource ID of the error code's text
  379. // szString - String to be filled with the resource value
  380. // isize - Size of the string buffer, in BYTES
  381. //
  382. //////////////////////////////////////////////////////////////////////////
  383. LPTSTR CMessageWrapper::GetResourceString(INT ResourceID, LPTSTR szString, INT isize)
  384. {
  385. LoadString(m_hInstance,ResourceID,szString,isize);
  386. return szString;
  387. }