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.

337 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. devices.c
  5. Abstract: Control Panel Applet for OLE POS Devices
  6. Author:
  7. Karan Mehra [t-karanm]
  8. Environment:
  9. Win32 mode
  10. Revision History:
  11. --*/
  12. #include "pos.h"
  13. #include <initguid.h>
  14. #include <usbioctl.h>
  15. BOOL InitializeDevicesDlg(HWND hwndDlg, BOOL bFirstTime)
  16. {
  17. RECT rc;
  18. HWND hwndListCtrl;
  19. DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
  20. /*
  21. * Register to receive device notifications from the PnP manager.
  22. */
  23. notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  24. notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  25. notificationFilter.dbcc_classguid = GUID_CLASS_USB_DEVICE;
  26. ghNotify = RegisterDeviceNotification(hwndDlg, (LPVOID)&notificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
  27. /*
  28. * Split the List Control [report view] into 3/4 for the NAME and 1/4 for the PORT.
  29. */
  30. hwndListCtrl = GetDlgItem(hwndDlg,IDC_DEVICES_LIST);
  31. if(bFirstTime) {
  32. GetClientRect(hwndListCtrl, &rc);
  33. rc.right >>= 2;
  34. InsertColumn(hwndListCtrl, NAME_COLUMN, IDS_DEVICES_LIST_NAME, (INT)(rc.right*3));
  35. InsertColumn(hwndListCtrl, PORT_COLUMN, IDS_DEVICES_LIST_PORT, (INT)(rc.right+2));
  36. MoveOK(GetParent(hwndDlg));
  37. }
  38. else
  39. ListView_DeleteAllItems(hwndListCtrl);
  40. /*
  41. * Create an Image List and add installed devices to the List View Control.
  42. */
  43. gDeviceCount = 0;
  44. if(!InitializeImageList(hwndListCtrl) || !FillListViewItems(hwndListCtrl)) {
  45. DestroyWindow(hwndListCtrl);
  46. return FALSE;
  47. }
  48. return TRUE;
  49. }
  50. BOOL CALLBACK DevicesDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  51. {
  52. BOOL fProcessed = TRUE;
  53. switch(uMsg) {
  54. case WM_INITDIALOG:
  55. InitializeDevicesDlg(hwndDlg, TRUE);
  56. break;
  57. case WM_HELP: // F1
  58. WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle, POS_HELP_FILE, HELP_WM_HELP, (DWORD_PTR)gaPosHelpIds);
  59. break;
  60. case WM_CONTEXTMENU: // Right Mouse Click
  61. WinHelp((HWND)wParam, POS_HELP_FILE, HELP_CONTEXTMENU, (DWORD_PTR)gaPosHelpIds);
  62. break;
  63. case WM_COMMAND:
  64. switch(LOWORD(wParam)) {
  65. case IDC_DEVICES_REFRESH:
  66. InitializeDevicesDlg(hwndDlg, FALSE);
  67. break;
  68. case IDC_DEVICES_TSHOOT:
  69. LaunchTroubleShooter();
  70. break;
  71. }
  72. ListView_SetItemState(GetDlgItem(hwndDlg,IDC_DEVICES_LIST), gDeviceCount-1, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
  73. break;
  74. case WM_DEVICECHANGE:
  75. switch((UINT)wParam) {
  76. case DBT_DEVICEARRIVAL:
  77. case DBT_DEVICEREMOVECOMPLETE:
  78. InitializeDevicesDlg(hwndDlg, FALSE);
  79. }
  80. break;
  81. case WM_CLOSE:
  82. case WM_DESTROY:
  83. if(ghNotify) {
  84. UnregisterDeviceNotification(ghNotify);
  85. ghNotify = NULL;
  86. }
  87. break;
  88. default:
  89. fProcessed = FALSE;
  90. break;
  91. }
  92. return fProcessed;
  93. }
  94. VOID InsertColumn(HWND hwndListCtrl, INT iCol, UINT uMsg, INT iWidth)
  95. {
  96. LVCOLUMN lvcol;
  97. lvcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  98. lvcol.fmt = LVCFMT_LEFT;
  99. lvcol.cx = iWidth;
  100. lvcol.iSubItem = iCol;
  101. lvcol.pszText = (LPTSTR)_alloca(BUFFER_SIZE);
  102. LoadString(ghInstance, uMsg, lvcol.pszText, BUFFER_SIZE);
  103. ListView_InsertColumn(hwndListCtrl, iCol, &lvcol);
  104. }
  105. BOOL InitializeImageList(HWND hwndListCtrl)
  106. {
  107. HICON hiconItem;
  108. HIMAGELIST himlSmall;
  109. /*
  110. * Create the Image List for Report View.
  111. */
  112. himlSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
  113. GetSystemMetrics(SM_CYSMICON), TRUE, 1, 1);
  114. /*
  115. * Add an icon to the Image List.
  116. */
  117. hiconItem = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_POS));
  118. ImageList_AddIcon(himlSmall, hiconItem);
  119. DeleteObject(hiconItem);
  120. /*
  121. * Link the Image List to the List View Control.
  122. */
  123. ListView_SetImageList(hwndListCtrl, himlSmall, LVSIL_SMALL);
  124. return TRUE;
  125. }
  126. BOOL FillListViewItems(HWND hwndListCtrl)
  127. {
  128. HKEY hKey;
  129. LONG Error;
  130. LPTSTR pszName, pszPort;
  131. DWORD dwType, dwSize;
  132. INT localCount = 0;
  133. Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  134. SERIALCOMM,
  135. 0,
  136. KEY_READ,
  137. &hKey);
  138. if(Error != ERROR_SUCCESS)
  139. return FALSE;
  140. pszName = _alloca(MAX_PATH);
  141. pszPort = _alloca(MAX_PATH);
  142. while(TRUE) {
  143. dwType = dwSize = MAX_PATH * sizeof(TCHAR);
  144. Error = RegEnumValue(hKey,
  145. localCount++,
  146. pszName,
  147. &dwType,
  148. NULL,
  149. NULL,
  150. (LPBYTE)pszPort,
  151. &dwSize);
  152. if(Error != ERROR_SUCCESS) {
  153. /*
  154. * We break out of this loop is when there are no more items.
  155. * This is just an extra check in case we fail by some other way.
  156. */
  157. if(Error != ERROR_NO_MORE_ITEMS)
  158. DisplayErrorMessage();
  159. break;
  160. }
  161. if(QueryPrettyName(pszName, pszPort))
  162. AddItem(hwndListCtrl, gDeviceCount++, pszName, pszPort);
  163. }
  164. RegCloseKey(hKey);
  165. ListView_SetItemState(hwndListCtrl, gDeviceCount-1, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
  166. return TRUE;
  167. }
  168. BOOL AddItem(HWND hwndListCtrl, INT iItem, LPTSTR pszName, LPTSTR pszPort)
  169. {
  170. LVITEM lvi;
  171. lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
  172. lvi.state = 0;
  173. lvi.stateMask = 0;
  174. lvi.iImage = 0;
  175. lvi.pszText = pszName;
  176. lvi.iItem = iItem;
  177. lvi.iSubItem = NAME_COLUMN;
  178. ListView_InsertItem(hwndListCtrl, &lvi);
  179. ListView_SetItemText(hwndListCtrl, iItem, PORT_COLUMN, pszPort);
  180. return TRUE;
  181. }
  182. VOID DisplayErrorMessage()
  183. {
  184. LPVOID lpMsgBuf;
  185. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
  186. | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  187. NULL,
  188. GetLastError(),
  189. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  190. (LPTSTR) &lpMsgBuf,
  191. 0,
  192. NULL);
  193. MessageBox(NULL, (LPTSTR)lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION);
  194. LocalFree(lpMsgBuf);
  195. }
  196. BOOL QueryPrettyName(LPTSTR pszName, LPTSTR pszPort)
  197. {
  198. HANDLE comFile;
  199. DWORD dwSize;
  200. BOOL fProcessed;
  201. TCHAR portBuffer[BUFFER_SIZE];
  202. /*
  203. * Ports beyond COM9 need an explicit [\\.\] in front of them.
  204. */
  205. wsprintf(portBuffer, TEXT("\\\\.\\%s"), pszPort);
  206. comFile = CreateFile(portBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  207. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  208. /*
  209. * Making sure that we are going to query the right device.
  210. */
  211. if (comFile == INVALID_HANDLE_VALUE || wcsncmp(pszName, TEXT("\\Device\\POS"), 11))
  212. return FALSE;
  213. fProcessed = DeviceIoControl(comFile,
  214. IOCTL_SERIAL_QUERY_DEVICE_NAME,
  215. NULL,
  216. 0,
  217. (LPVOID)pszName,
  218. MAX_PATH,
  219. &dwSize,
  220. NULL);
  221. CloseHandle(comFile);
  222. return fProcessed;
  223. }
  224. VOID MoveOK(HWND hwndParent)
  225. {
  226. HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
  227. if(hwndButton) {
  228. RECT rc;
  229. GetWindowRect(hwndButton, &rc);
  230. DestroyWindow(hwndButton);
  231. MapWindowPoints(NULL, hwndParent, (LPPOINT)&rc, 2);
  232. hwndButton = GetDlgItem(hwndParent, IDOK);
  233. SetWindowPos(hwndButton, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  234. }
  235. }
  236. VOID LaunchTroubleShooter()
  237. {
  238. TCHAR lpszCmd[MAX_PATH];
  239. STARTUPINFO si;
  240. PROCESS_INFORMATION pi;
  241. if(LoadString(ghInstance, IDS_TSHOOT_CMD, lpszCmd, MAX_PATH)) {
  242. ZeroMemory(&si, sizeof(STARTUPINFO));
  243. si.cb = sizeof(STARTUPINFO);
  244. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
  245. si.wShowWindow = SW_NORMAL;
  246. if (CreateProcess(NULL, lpszCmd, NULL, NULL, FALSE, 0, 0, NULL, &si, &pi)) {
  247. CloseHandle(pi.hThread);
  248. CloseHandle(pi.hProcess);
  249. }
  250. }
  251. }