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.

1079 lines
28 KiB

  1. /**------------------------------------------------------------------
  2. devinst.c
  3. ------------------------------------------------------------------**/
  4. //
  5. // Includes
  6. //
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <wtypes.h>
  11. #include <cfgmgr32.h>
  12. #include <malloc.h>
  13. #include "cmtest.h"
  14. //
  15. // Private Prototypes
  16. //
  17. FillRelationsListBox(
  18. HWND hDlg,
  19. ULONG RelationType
  20. );
  21. BOOL
  22. FillEnumeratorListBox(
  23. HWND hDlg
  24. );
  25. BOOL
  26. FillDeviceListBox(
  27. HWND hDlg
  28. );
  29. BOOL
  30. FillInstanceListBox(
  31. HWND hDlg
  32. );
  33. BOOL
  34. FillDeviceInstanceListBox(
  35. HWND hDlg
  36. );
  37. BOOL
  38. GetSelectedEnumerator(
  39. HWND hDlg,
  40. LPTSTR szDevice
  41. );
  42. BOOL
  43. GetSelectedDevice(
  44. HWND hDlg,
  45. LPTSTR szDevice
  46. );
  47. BOOL
  48. GetSelectedDevNode(
  49. HWND hDlg,
  50. PDEVNODE pdnDevNode
  51. );
  52. VOID
  53. CallPnPIsaDetect(
  54. HWND hDlg,
  55. LPTSTR pszDevice
  56. );
  57. //
  58. // Globals
  59. //
  60. extern HINSTANCE hInst;
  61. extern TCHAR szDebug[MAX_PATH];
  62. extern TCHAR szAppName[MAX_PATH];
  63. extern HMACHINE hMachine;
  64. /**----------------------------------------------------------------------**/
  65. LRESULT CALLBACK
  66. DeviceListDlgProc(
  67. HWND hDlg,
  68. UINT message,
  69. WPARAM wParam,
  70. LPARAM lParam
  71. )
  72. {
  73. CONFIGRET Status = CR_SUCCESS;
  74. TCHAR szDevice[MAX_DEVICE_ID_LEN+1];
  75. DEVNODE dnDevNode;
  76. ULONG ulStatus, ulProblem;
  77. switch (message) {
  78. case WM_SHOWWINDOW:
  79. FillEnumeratorListBox(hDlg);
  80. return TRUE;
  81. case WM_COMMAND:
  82. switch(LOWORD(wParam)) {
  83. case IDOK:
  84. EndDialog(hDlg, TRUE);
  85. return TRUE;
  86. case ID_LB_ENUMERATORS:
  87. if (HIWORD(wParam) == LBN_SELCHANGE) {
  88. FillDeviceListBox(hDlg);
  89. }
  90. break;
  91. case ID_LB_DEVICES:
  92. if (HIWORD(wParam) == LBN_SELCHANGE) {
  93. FillInstanceListBox(hDlg);
  94. }
  95. break;
  96. }
  97. break;
  98. }
  99. return (FALSE);
  100. } // DeviceListDlgProc
  101. /**----------------------------------------------------------------------**/
  102. LRESULT CALLBACK
  103. ServiceListDlgProc(
  104. HWND hDlg,
  105. UINT message,
  106. WPARAM wParam,
  107. LPARAM lParam
  108. )
  109. {
  110. CONFIGRET Status = CR_SUCCESS;
  111. TCHAR szService[MAX_PATH];
  112. LPTSTR pBuffer, p;
  113. ULONG ulSize;
  114. switch (message) {
  115. case WM_COMMAND:
  116. switch(LOWORD(wParam)) {
  117. case IDOK:
  118. EndDialog(hDlg, TRUE);
  119. return TRUE;
  120. case ID_BT_SERVICE:
  121. GetDlgItemText(hDlg, ID_ED_SERVICE, szService, MAX_PATH);
  122. //
  123. // get device list size for this service
  124. //
  125. Status = CM_Get_Device_ID_List_Size_Ex(&ulSize, szService,
  126. CM_GETIDLIST_FILTER_SERVICE, hMachine);
  127. if (Status != CR_SUCCESS) {
  128. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List_Size failed (%xh)"), Status);
  129. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  130. return FALSE;
  131. }
  132. pBuffer = (PTSTR)LocalAlloc(LPTR, ulSize * sizeof(TCHAR));
  133. if (pBuffer == NULL) {
  134. MessageBeep(0);
  135. return FALSE;
  136. }
  137. //
  138. // to verify the null terminators are correct, fill with 1's
  139. //
  140. memset(pBuffer, 1, ulSize * sizeof(TCHAR));
  141. //
  142. // get device list for this service
  143. //
  144. Status = CM_Get_Device_ID_List_Ex(szService, pBuffer, ulSize,
  145. CM_GETIDLIST_FILTER_SERVICE, hMachine);
  146. if (Status != CR_SUCCESS) {
  147. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List failed (%xh)"), Status);
  148. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  149. return FALSE;
  150. }
  151. SendDlgItemMessage(
  152. hDlg, ID_LB_SERVICE, LB_RESETCONTENT, 0, 0);
  153. p = (LPTSTR)pBuffer;
  154. while (*p != '\0') {
  155. SendDlgItemMessage(
  156. hDlg, ID_LB_SERVICE, LB_ADDSTRING, 0,
  157. (LPARAM)(LPCTSTR)p);
  158. while (*p != '\0') {
  159. p++; // skip to next substring
  160. }
  161. p++; // skip over null terminator
  162. }
  163. SendDlgItemMessage(hDlg, ID_LB_SERVICE, LB_SETSEL, TRUE, 0);
  164. LocalFree(pBuffer);
  165. break;
  166. }
  167. break;
  168. }
  169. return (FALSE);
  170. } // ServiceListDlgProc
  171. /**----------------------------------------------------------------------**/
  172. LRESULT CALLBACK
  173. RelationsListDlgProc(
  174. HWND hDlg,
  175. UINT message,
  176. WPARAM wParam,
  177. LPARAM lParam
  178. )
  179. {
  180. CONFIGRET Status = CR_SUCCESS;
  181. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  182. LPTSTR pBuffer, p;
  183. ULONG Size;
  184. LONG Index;
  185. switch (message) {
  186. case WM_INITDIALOG:
  187. //
  188. // Fill list box with all known device instances so user can just
  189. // point at the device instance they want to query relations for.
  190. //
  191. SendDlgItemMessage(hDlg, ID_LB_TARGETS, LB_RESETCONTENT, 0, 0);
  192. Status = CM_Get_Device_ID_List_Size_Ex(&Size, NULL,
  193. CM_GETIDLIST_FILTER_NONE, hMachine);
  194. if (Status != CR_SUCCESS) {
  195. return FALSE;
  196. }
  197. pBuffer = (PTSTR)malloc(Size * sizeof(TCHAR));
  198. if (pBuffer == NULL) {
  199. return FALSE;
  200. }
  201. Status = CM_Get_Device_ID_List_Ex(NULL, pBuffer, Size,
  202. CM_GETIDLIST_FILTER_NONE, hMachine);
  203. if (Status != CR_SUCCESS) {
  204. return FALSE;
  205. }
  206. for (p = pBuffer; *p; p += lstrlen(p) + 1) {
  207. SendDlgItemMessage(hDlg, ID_LB_TARGETS, LB_ADDSTRING, 0,
  208. (LPARAM)(LPCTSTR)p);
  209. }
  210. SendDlgItemMessage(hDlg, ID_LB_TARGETS, LB_SETSEL, TRUE, 0);
  211. free(pBuffer);
  212. return TRUE;
  213. case WM_COMMAND:
  214. switch(LOWORD(wParam)) {
  215. case IDOK:
  216. EndDialog(hDlg, TRUE);
  217. return TRUE;
  218. case ID_BT_BUS:
  219. FillRelationsListBox(hDlg, CM_GETIDLIST_FILTER_BUSRELATIONS);
  220. break;
  221. case ID_BT_POWER:
  222. FillRelationsListBox(hDlg, CM_GETIDLIST_FILTER_POWERRELATIONS);
  223. break;
  224. case ID_BT_REMOVAL:
  225. FillRelationsListBox(hDlg, CM_GETIDLIST_FILTER_REMOVALRELATIONS);
  226. break;
  227. case ID_BT_EJECTION:
  228. FillRelationsListBox(hDlg, CM_GETIDLIST_FILTER_EJECTRELATIONS);
  229. break;
  230. }
  231. break;
  232. }
  233. return (FALSE);
  234. } // RelationsListDlgProc
  235. FillRelationsListBox(
  236. HWND hDlg,
  237. ULONG RelationType
  238. )
  239. {
  240. CONFIGRET Status = CR_SUCCESS;
  241. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  242. LPTSTR pBuffer, p;
  243. ULONG Size;
  244. LONG Index;
  245. SendDlgItemMessage(hDlg, ID_LB_RELATIONS, LB_RESETCONTENT, 0, 0);
  246. //
  247. // Which device instance was selected
  248. //
  249. Index = SendDlgItemMessage(hDlg, ID_LB_TARGETS, LB_GETCURSEL, 0, 0);
  250. if (Index == LB_ERR || Index == 0) {
  251. return FALSE;
  252. }
  253. SendDlgItemMessage(hDlg, ID_LB_TARGETS, LB_GETTEXT, (WPARAM)Index,
  254. (LPARAM)(LPCTSTR)szDevice);
  255. //
  256. // How big a buffer to hold the relations list?
  257. //
  258. Status = CM_Get_Device_ID_List_Size_Ex(&Size, szDevice, RelationType, hMachine);
  259. if (Status != CR_SUCCESS) {
  260. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List_Size failed (%xh)"), Status);
  261. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  262. return FALSE;
  263. }
  264. pBuffer = (PTSTR)malloc(Size * sizeof(TCHAR));
  265. if (pBuffer == NULL) {
  266. return FALSE;
  267. }
  268. //
  269. // to verify the null terminators are correct, fill with 1's
  270. //
  271. memset(pBuffer, 1, Size * sizeof(TCHAR));
  272. //
  273. // Retrieve and display the relations list
  274. //
  275. Status = CM_Get_Device_ID_List_Ex(szDevice, pBuffer, Size, RelationType, hMachine);
  276. if (Status != CR_SUCCESS) {
  277. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List failed (%xh)"), Status);
  278. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  279. return FALSE;
  280. }
  281. for (p = pBuffer; *p; p += lstrlen(p) + 1) {
  282. SendDlgItemMessage(hDlg, ID_LB_RELATIONS, LB_ADDSTRING, 0,
  283. (LPARAM)(LPCTSTR)p);
  284. }
  285. free(pBuffer);
  286. return TRUE;
  287. } // FillRelationsListBox
  288. /**----------------------------------------------------------------------**/
  289. LRESULT CALLBACK
  290. DeviceDlgProc(
  291. HWND hDlg,
  292. UINT message,
  293. WPARAM wParam,
  294. LPARAM lParam
  295. )
  296. {
  297. CONFIGRET Status = CR_SUCCESS;
  298. TCHAR szDevice[MAX_DEVICE_ID_LEN+1];
  299. DEVNODE dnDevNode;
  300. ULONG ulStatus, ulProblem;
  301. ULONG err;
  302. switch (message) {
  303. case WM_SHOWWINDOW:
  304. FillDeviceInstanceListBox(hDlg);
  305. return TRUE;
  306. case WM_COMMAND:
  307. switch(LOWORD(wParam)) {
  308. case IDOK:
  309. EndDialog(hDlg, TRUE);
  310. return TRUE;
  311. case ID_BT_SOFTWAREKEY:
  312. DialogBox(hInst, MAKEINTRESOURCE(SOFTWAREKEY_DIALOG), hDlg,
  313. (DLGPROC)SoftwareKeyDlgProc);
  314. break;
  315. case ID_BT_ENABLE:
  316. if (!GetSelectedDevNode(hDlg, &dnDevNode)) {
  317. MessageBeep(0);
  318. break;
  319. }
  320. Status = CM_Enable_DevNode_Ex(dnDevNode, 0, hMachine);
  321. if (Status != CR_SUCCESS) {
  322. wsprintf(szDebug, TEXT("CM_Enable_DevNode failed (%xh)"), Status);
  323. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  324. break;
  325. }
  326. break;
  327. case ID_BT_DISABLE:
  328. if (!GetSelectedDevNode(hDlg, &dnDevNode)) {
  329. MessageBeep(0);
  330. break;
  331. }
  332. Status = CM_Disable_DevNode_Ex(dnDevNode, 0, hMachine);
  333. if (Status != CR_SUCCESS) {
  334. wsprintf(szDebug, TEXT("CM_Disable_DevNode failed (%xh)"), Status);
  335. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  336. break;
  337. }
  338. break;
  339. case ID_BT_CREATE:
  340. GetSelectedDevice(hDlg, szDevice);
  341. DialogBoxParam(hInst, MAKEINTRESOURCE(CREATE_DIALOG), hDlg,
  342. (DLGPROC)CreateDlgProc, (LPARAM)(LPCTSTR)szDevice);
  343. break;
  344. case ID_BT_GETSTATUS:
  345. if (!GetSelectedDevNode(hDlg, &dnDevNode)) {
  346. MessageBeep(0);
  347. break;
  348. }
  349. Status = CM_Get_DevNode_Status_Ex(&ulStatus, &ulProblem,
  350. dnDevNode, 0, hMachine);
  351. if (Status != CR_SUCCESS) {
  352. wsprintf(szDebug, TEXT("CM_Get_DevNode_Status failed (%xh)"), Status);
  353. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  354. break;
  355. }
  356. wsprintf(szDebug, TEXT("%08xh"), ulStatus);
  357. SetDlgItemText(hDlg, ID_ST_STATUS, szDebug);
  358. wsprintf(szDebug, TEXT("%08xh"), ulProblem);
  359. SetDlgItemText(hDlg, ID_ST_PROBLEM, szDebug);
  360. break;
  361. case ID_BT_SETPROBLEM:
  362. if (!GetSelectedDevNode(hDlg, &dnDevNode)) {
  363. MessageBeep(0);
  364. break;
  365. }
  366. ulProblem = GetDlgItemInt(hDlg, ID_ST_PROBLEM, &err, FALSE);
  367. Status = CM_Set_DevNode_Problem_Ex(dnDevNode, ulProblem, 0, hMachine);
  368. if (Status != CR_SUCCESS) {
  369. wsprintf(szDebug, TEXT("CM_Get_DevNode_Status failed (%xh)"), Status);
  370. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  371. }
  372. break;
  373. case ID_BT_QUERY_REMOVE:
  374. GetSelectedDevNode(hDlg, &dnDevNode);
  375. Status = CM_Query_Remove_SubTree(dnDevNode, CM_QUERY_REMOVE_UI_OK);
  376. if (Status != CR_SUCCESS) {
  377. wsprintf(szDebug, TEXT("CM_Get_Query_Remove_SubTree failed (%xh)"), Status);
  378. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  379. break;
  380. }
  381. break;
  382. case ID_BT_REMOVE:
  383. GetSelectedDevNode(hDlg, &dnDevNode);
  384. Status = CM_Remove_SubTree(dnDevNode, CM_REMOVE_UI_OK);
  385. if (Status != CR_SUCCESS) {
  386. wsprintf(szDebug, TEXT("CM_Get_Query_Remove_SubTree failed (%xh)"), Status);
  387. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  388. break;
  389. }
  390. break;
  391. case ID_BT_RESOURCEPICKER:
  392. GetSelectedDevice(hDlg, szDevice);
  393. CallPnPIsaDetect(hDlg, szDevice);
  394. break;
  395. }
  396. break;
  397. }
  398. return (FALSE);
  399. } // DeviceDlgProc
  400. /**----------------------------------------------------------------------**/
  401. LRESULT CALLBACK
  402. DevKeyDlgProc(
  403. HWND hDlg,
  404. UINT message,
  405. WPARAM wParam,
  406. LPARAM lParam
  407. )
  408. {
  409. CONFIGRET Status = CR_SUCCESS;
  410. TCHAR szDeviceID[MAX_DEVICE_ID_LEN];
  411. DEVNODE dnDevNode;
  412. ULONG ulFlags, ulProfile, ulValue;
  413. HKEY hKey = NULL;
  414. switch (message) {
  415. case WM_INITDIALOG:
  416. CheckDlgButton(hDlg, ID_RD_HW, 1);
  417. CheckDlgButton(hDlg, ID_RD_USER, 1);
  418. SetDlgItemInt(hDlg, ID_ED_PROFILE, 0xFFFFFFFF, FALSE);
  419. return TRUE;
  420. case WM_COMMAND:
  421. switch(LOWORD(wParam)) {
  422. case IDOK:
  423. EndDialog(hDlg, TRUE);
  424. return TRUE;
  425. case ID_BT_OPENDEVKEY:
  426. GetDlgItemText(hDlg, ID_ED_DEVICEID, szDeviceID, MAX_DEVICE_ID_LEN);
  427. Status = CM_Locate_DevNode_Ex(&dnDevNode, szDeviceID, 0, hMachine);
  428. if (Status != CR_SUCCESS) {
  429. wsprintf(szDebug, TEXT("CM_Locate_DevNode failed (%xh)"), Status);
  430. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  431. return FALSE;
  432. }
  433. ulFlags = 0;
  434. if (IsDlgButtonChecked(hDlg, ID_RD_HW)) {
  435. ulFlags |= CM_REGISTRY_HARDWARE;
  436. } else {
  437. ulFlags |= CM_REGISTRY_SOFTWARE;
  438. }
  439. if (IsDlgButtonChecked(hDlg, ID_RD_USER)) {
  440. ulFlags |= CM_REGISTRY_USER;
  441. }
  442. else if (IsDlgButtonChecked(hDlg, ID_RD_CONFIG)) {
  443. ulFlags |= CM_REGISTRY_CONFIG;
  444. ulProfile = (ULONG)GetDlgItemInt(hDlg, ID_ED_PROFILE, NULL, TRUE);
  445. }
  446. Status = CM_Open_DevNode_Key_Ex(
  447. dnDevNode, KEY_READ | KEY_WRITE, ulProfile,
  448. RegDisposition_OpenAlways, &hKey, ulFlags, hMachine);
  449. if (Status != CR_SUCCESS) {
  450. wsprintf(szDebug, TEXT("CM_Open_DevNode_Key failed (%xh)"), Status);
  451. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  452. return FALSE;
  453. }
  454. ulValue = 13;
  455. RegSetValueEx(hKey, TEXT("CustomValue"), 0, REG_DWORD,
  456. (LPBYTE)&ulValue, 4);
  457. RegCloseKey(hKey);
  458. break;
  459. case ID_BT_DELDEVKEY:
  460. GetDlgItemText(hDlg, ID_ED_DEVICEID, szDeviceID, MAX_DEVICE_ID_LEN);
  461. Status = CM_Locate_DevNode_Ex(&dnDevNode, szDeviceID, 0, hMachine);
  462. if (Status != CR_SUCCESS) {
  463. wsprintf(szDebug, TEXT("CM_Locate_DevNode failed (%xh)"), Status);
  464. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  465. return FALSE;
  466. }
  467. ulFlags = 0;
  468. if (IsDlgButtonChecked(hDlg, ID_RD_HW)) {
  469. ulFlags |= CM_REGISTRY_HARDWARE;
  470. } else {
  471. ulFlags |= CM_REGISTRY_SOFTWARE;
  472. }
  473. if (IsDlgButtonChecked(hDlg, ID_RD_USER)) {
  474. ulFlags |= CM_REGISTRY_USER;
  475. }
  476. else if (IsDlgButtonChecked(hDlg, ID_RD_CONFIG)) {
  477. ulFlags |= CM_REGISTRY_CONFIG;
  478. ulProfile = (ULONG)GetDlgItemInt(hDlg, ID_ED_PROFILE, NULL, TRUE);
  479. }
  480. Status = CM_Delete_DevNode_Key_Ex(
  481. dnDevNode, ulProfile, ulFlags, hMachine);
  482. if (Status != CR_SUCCESS) {
  483. wsprintf(szDebug, TEXT("CM_Delete_DevNode_Key failed (%xh)"), Status);
  484. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  485. return FALSE;
  486. }
  487. }
  488. break;
  489. }
  490. return (FALSE);
  491. } // DevKeyDlgProc
  492. /**----------------------------------------------------------------------**/
  493. BOOL
  494. FillEnumeratorListBox(
  495. HWND hDlg
  496. )
  497. {
  498. CONFIGRET Status;
  499. ULONG ulIndex, Size;
  500. TCHAR szEnumerator[MAX_PATH];
  501. SendDlgItemMessage(
  502. hDlg, ID_LB_ENUMERATORS, LB_RESETCONTENT, 0, 0);
  503. SendDlgItemMessage(
  504. hDlg, ID_LB_ENUMERATORS, LB_ADDSTRING, 0,
  505. (LPARAM)(LPCTSTR)TEXT("(All)"));
  506. ulIndex = 0;
  507. Status = CR_SUCCESS;
  508. while (Status == CR_SUCCESS) {
  509. Size = MAX_PATH;
  510. Status = CM_Enumerate_Enumerators_Ex(
  511. ulIndex, szEnumerator, &Size, 0, hMachine);
  512. if (Status == CR_NO_SUCH_VALUE) {
  513. // no more enumerators, break out of the loop
  514. break;
  515. }
  516. if (Status != CR_SUCCESS) {
  517. wsprintf(szDebug, TEXT("CM_Enumerate_Enumerators failed (%xh)"), Status);
  518. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  519. return FALSE;
  520. }
  521. SendDlgItemMessage(
  522. hDlg, ID_LB_ENUMERATORS, LB_ADDSTRING, 0,
  523. (LPARAM)(LPCTSTR)szEnumerator);
  524. ulIndex++;
  525. }
  526. SendDlgItemMessage(
  527. hDlg, ID_LB_ENUMERATORS, LB_SETSEL, TRUE, 0);
  528. return TRUE;
  529. } // FillEnumeratorListBox
  530. /**----------------------------------------------------------------------**/
  531. BOOL
  532. FillDeviceListBox(
  533. HWND hDlg
  534. )
  535. {
  536. CONFIGRET Status;
  537. ULONG Size, i;
  538. TCHAR szEnumerator[MAX_PATH];
  539. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  540. PTSTR pBuffer, p;
  541. if (!GetSelectedEnumerator(hDlg, szEnumerator)) {
  542. MessageBeep(0);
  543. return FALSE;
  544. }
  545. if (*szEnumerator == '\0') {
  546. Status = CM_Get_Device_ID_List_Size_Ex(&Size, NULL,
  547. CM_GETIDLIST_FILTER_NONE, hMachine);
  548. }
  549. else {
  550. Status = CM_Get_Device_ID_List_Size_Ex(&Size, szEnumerator,
  551. CM_GETIDLIST_FILTER_ENUMERATOR, hMachine);
  552. }
  553. if (Status != CR_SUCCESS) {
  554. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List_Size failed (%xh)"), Status);
  555. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  556. return FALSE;
  557. }
  558. i = Size * sizeof(TCHAR);
  559. pBuffer = (PTSTR)malloc(i);
  560. if (pBuffer == NULL) {
  561. MessageBeep(0);
  562. return FALSE;
  563. }
  564. if (*szEnumerator == '\0') {
  565. Status = CM_Get_Device_ID_List_Ex(NULL, pBuffer, Size,
  566. CM_GETIDLIST_FILTER_NONE, hMachine);
  567. }
  568. else {
  569. Status = CM_Get_Device_ID_List_Ex(szEnumerator, pBuffer, Size,
  570. CM_GETIDLIST_FILTER_ENUMERATOR, hMachine);
  571. }
  572. if (Status != CR_SUCCESS) {
  573. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List failed (%xh)"), Status);
  574. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  575. return FALSE;
  576. }
  577. SendDlgItemMessage(
  578. hDlg, ID_LB_DEVICES, LB_RESETCONTENT, 0, 0);
  579. for (p = pBuffer; *p; p += lstrlen(p) + 1) {
  580. SendDlgItemMessage(
  581. hDlg, ID_LB_DEVICES, LB_ADDSTRING, 0,
  582. (LPARAM)(LPCTSTR)p);
  583. }
  584. SendDlgItemMessage(
  585. hDlg, ID_LB_DEVICES, LB_SETSEL, TRUE, 0);
  586. free(pBuffer);
  587. return TRUE;
  588. } // FillDeviceListBox
  589. /**----------------------------------------------------------------------**/
  590. BOOL
  591. FillInstanceListBox(
  592. HWND hDlg
  593. )
  594. {
  595. CONFIGRET Status;
  596. ULONG Size;
  597. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  598. PTSTR pBuffer, p;
  599. LONG Index;
  600. Index = SendDlgItemMessage(
  601. hDlg, ID_LB_DEVICES, LB_GETCURSEL, 0, 0);
  602. if (Index == LB_ERR) {
  603. MessageBeep(0);
  604. return FALSE;
  605. }
  606. SendDlgItemMessage(
  607. hDlg, ID_LB_DEVICES, LB_GETTEXT, (WPARAM)Index,
  608. (LPARAM)(LPCTSTR)szDevice);
  609. // truncate the instance part
  610. p = szDevice;
  611. while (*p != '\\') p++;
  612. p++;
  613. while (*p != '\\') p++;
  614. *p = '\0';
  615. Status = CM_Get_Device_ID_List_Size_Ex(&Size, szDevice,
  616. CM_GETIDLIST_FILTER_ENUMERATOR, hMachine);
  617. if (Status != CR_SUCCESS) {
  618. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List_Size failed (%xh)"), Status);
  619. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  620. return FALSE;
  621. }
  622. pBuffer = (PTSTR)LocalAlloc(LPTR, Size * sizeof(TCHAR));
  623. if (pBuffer == NULL) {
  624. MessageBeep(0);
  625. return FALSE;
  626. }
  627. Status = CM_Get_Device_ID_List_Ex(szDevice, pBuffer, Size,
  628. CM_GETIDLIST_FILTER_ENUMERATOR, hMachine);
  629. if (Status != CR_SUCCESS) {
  630. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List failed (%xh)"), Status);
  631. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  632. return FALSE;
  633. }
  634. SendDlgItemMessage(
  635. hDlg, ID_LB_INSTANCES, LB_RESETCONTENT, 0, 0);
  636. p = (LPTSTR)pBuffer;
  637. while (*p != '\0') {
  638. SendDlgItemMessage(
  639. hDlg, ID_LB_INSTANCES, LB_ADDSTRING, 0,
  640. (LPARAM)(LPCTSTR)p);
  641. while (*p != '\0') {
  642. p++; // skip to next substring
  643. }
  644. p++; // skip over null terminator
  645. }
  646. SendDlgItemMessage(
  647. hDlg, ID_LB_INSTANCES, LB_SETSEL, TRUE, 0);
  648. LocalFree(pBuffer);
  649. return TRUE;
  650. } // FillInstanceListBox
  651. /**----------------------------------------------------------------------**/
  652. BOOL
  653. FillDeviceInstanceListBox(
  654. HWND hDlg
  655. )
  656. {
  657. CONFIGRET Status;
  658. ULONG Size;
  659. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  660. PTSTR pBuffer, p;
  661. //
  662. // get device list size for all enumerators
  663. //
  664. Status = CM_Get_Device_ID_List_Size_Ex(&Size, NULL,
  665. CM_GETIDLIST_FILTER_NONE, hMachine);
  666. if (Status != CR_SUCCESS) {
  667. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List_Size failed (%xh)"), Status);
  668. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  669. return FALSE;
  670. }
  671. pBuffer = (PTSTR)LocalAlloc(LPTR, Size * sizeof(TCHAR));
  672. if (pBuffer == NULL) {
  673. MessageBeep(0);
  674. return FALSE;
  675. }
  676. //
  677. // to verify the null terminators are correct, fill with 1's
  678. //
  679. memset(pBuffer, 1, Size * sizeof(TCHAR));
  680. //
  681. // get device list for all enumerators
  682. //
  683. Status = CM_Get_Device_ID_List_Ex(NULL, pBuffer, Size,
  684. CM_GETIDLIST_FILTER_NONE, hMachine);
  685. if (Status != CR_SUCCESS) {
  686. wsprintf(szDebug, TEXT("CM_Get_Device_ID_List failed (%xh)"), Status);
  687. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  688. return FALSE;
  689. }
  690. SendDlgItemMessage(
  691. hDlg, ID_LB_DEVICEIDS, LB_RESETCONTENT, 0, 0);
  692. p = (LPTSTR)pBuffer;
  693. while (*p != '\0') {
  694. SendDlgItemMessage(
  695. hDlg, ID_LB_DEVICEIDS, LB_ADDSTRING, 0,
  696. (LPARAM)(LPCTSTR)p);
  697. while (*p != '\0') {
  698. p++; // skip to next substring
  699. }
  700. p++; // skip over null terminator
  701. }
  702. SendDlgItemMessage(hDlg, ID_LB_DEVICEIDS, LB_SETSEL, TRUE, 0);
  703. LocalFree(pBuffer);
  704. return TRUE;
  705. } // FillDeviceInstanceListBox
  706. /**----------------------------------------------------------------------**/
  707. BOOL
  708. GetSelectedEnumerator(
  709. HWND hDlg,
  710. LPTSTR szEnumerator
  711. )
  712. {
  713. LONG Index;
  714. Index = SendDlgItemMessage(
  715. hDlg, ID_LB_ENUMERATORS, LB_GETCURSEL, 0, 0);
  716. if (Index == LB_ERR) {
  717. MessageBeep(0);
  718. return FALSE;
  719. }
  720. SendDlgItemMessage(
  721. hDlg, ID_LB_ENUMERATORS, LB_GETTEXT, (WPARAM)Index,
  722. (LPARAM)(LPCTSTR)szEnumerator);
  723. if (lstrcmpi(szEnumerator, TEXT("(All)")) == 0) {
  724. *szEnumerator = '\0'; // if All selected, then no Enumerator specified
  725. }
  726. return TRUE;
  727. } // GetSeletectedEnumerator
  728. /**----------------------------------------------------------------------**/
  729. BOOL
  730. GetSelectedDevice(
  731. HWND hDlg,
  732. LPTSTR szDevice
  733. )
  734. {
  735. LONG Index;
  736. Index = SendDlgItemMessage(
  737. hDlg, ID_LB_DEVICEIDS, LB_GETCURSEL, 0, 0);
  738. if (Index == LB_ERR || Index == 0) {
  739. MessageBeep(0);
  740. return FALSE;
  741. }
  742. SendDlgItemMessage(
  743. hDlg, ID_LB_DEVICEIDS, LB_GETTEXT, (WPARAM)Index,
  744. (LPARAM)(LPCTSTR)szDevice);
  745. return TRUE;
  746. } // GetSeletectedDevice
  747. /**----------------------------------------------------------------------**/
  748. BOOL
  749. GetSelectedDevNode(
  750. HWND hDlg,
  751. PDEVNODE pdnDevNode
  752. )
  753. {
  754. CONFIGRET Status = CR_SUCCESS;
  755. TCHAR szDevice[MAX_DEVICE_ID_LEN];
  756. if (!GetSelectedDevice(hDlg, szDevice)) {
  757. return FALSE;
  758. }
  759. Status = CM_Locate_DevNode_Ex(pdnDevNode, szDevice, 0, hMachine);
  760. if (Status != CR_SUCCESS) {
  761. wsprintf(szDebug, TEXT("CM_Locate_DevNode failed (%xh)"), Status);
  762. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  763. return FALSE;
  764. }
  765. return TRUE;
  766. } // GetSelectedDevNode
  767. /**----------------------------------------------------------------------**/
  768. LRESULT CALLBACK
  769. SoftwareKeyDlgProc(
  770. HWND hDlg,
  771. UINT message,
  772. WPARAM wParam,
  773. LPARAM lParam
  774. )
  775. {
  776. CONFIGRET Status = CR_SUCCESS;
  777. switch (message) {
  778. case WM_INITDIALOG:
  779. return TRUE;
  780. case WM_COMMAND:
  781. switch(LOWORD(wParam)) {
  782. case IDOK:
  783. EndDialog(hDlg, TRUE);
  784. return TRUE;
  785. default:
  786. break;
  787. }
  788. }
  789. return (FALSE);
  790. } // SoftwareKeyDlgProc
  791. /**----------------------------------------------------------------------**/
  792. LRESULT CALLBACK
  793. CreateDlgProc(
  794. HWND hDlg,
  795. UINT message,
  796. WPARAM wParam,
  797. LPARAM lParam
  798. )
  799. {
  800. CONFIGRET Status = CR_SUCCESS;
  801. TCHAR szDeviceID[MAX_DEVICE_ID_LEN];
  802. TCHAR szParentID[MAX_DEVICE_ID_LEN];
  803. DEVNODE dnDevNode, dnParentDevNode;
  804. ULONG ulFlags;
  805. switch (message) {
  806. case WM_INITDIALOG:
  807. SetDlgItemText(hDlg, ID_ST_PARENT, (LPCTSTR)lParam);
  808. CheckDlgButton(hDlg, ID_RD_NORMAL, 1);
  809. return TRUE;
  810. case WM_COMMAND:
  811. switch(LOWORD(wParam)) {
  812. case IDOK:
  813. EndDialog(hDlg, TRUE);
  814. return TRUE;
  815. case ID_BT_CREATE:
  816. SetDlgItemText(hDlg, ID_ST_STATUS, TEXT(""));
  817. GetDlgItemText(hDlg, ID_ED_DEVICEID, szDeviceID, MAX_DEVICE_ID_LEN);
  818. GetDlgItemText(hDlg, ID_ST_PARENT, szParentID, MAX_DEVICE_ID_LEN);
  819. ulFlags = CM_CREATE_DEVNODE_NORMAL;
  820. if (IsDlgButtonChecked(hDlg, ID_CHK_GENERATEID)) {
  821. ulFlags |= CM_CREATE_DEVNODE_GENERATE_ID;
  822. }
  823. if (IsDlgButtonChecked(hDlg, ID_CHK_PHANTOM)) {
  824. ulFlags |= CM_CREATE_DEVNODE_PHANTOM;
  825. }
  826. Status = CM_Locate_DevNode(
  827. &dnParentDevNode, szParentID, 0);
  828. if (Status != CR_SUCCESS) {
  829. wsprintf(szDebug, TEXT("CM_Locate_DevNode failed (%xh)"), Status);
  830. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  831. return FALSE;
  832. }
  833. Status = CM_Create_DevNode(
  834. &dnDevNode, szDeviceID, dnParentDevNode, ulFlags);
  835. if (Status != CR_SUCCESS) {
  836. wsprintf(szDebug, TEXT("CM_Create_DevNode failed (%xh)"), Status);
  837. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  838. return FALSE;
  839. }
  840. memset(szDeviceID, 0, MAX_DEVICE_ID_LEN * sizeof(TCHAR));
  841. Status = CM_Get_Device_ID(
  842. dnDevNode, szDeviceID, MAX_DEVICE_ID_LEN, 0);
  843. if (Status != CR_SUCCESS) {
  844. wsprintf(szDebug, TEXT("CM_Create_DevNode failed (%xh)"), Status);
  845. MessageBox(hDlg, szDebug, szAppName, MB_OK);
  846. return FALSE;
  847. }
  848. wsprintf(szDebug, TEXT("%s created"),
  849. szDeviceID);
  850. SetDlgItemText(hDlg, ID_ST_STATUS, szDebug);
  851. break;
  852. }
  853. break;
  854. }
  855. return (FALSE);
  856. } // CreateDlgProc