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.

143 lines
3.6 KiB

  1. // Private nt headers.
  2. //
  3. #include <nt.h>
  4. #include <ntrtl.h>
  5. #include <nturtl.h>
  6. #include <objbase.h> // CLSIDFromString
  7. #include <setupapi.h>
  8. #include <conio.h> // _kbhit
  9. #include <stdio.h> // printf
  10. BOOL
  11. FGetClassGuidFromCmdLineParam (
  12. PCWSTR pszParam,
  13. GUID* pguidClass)
  14. {
  15. static const struct
  16. {
  17. GUID guidClass;
  18. PCWSTR pszName;
  19. } MapClassGuidToName [] =
  20. {
  21. { {0xad498944, 0x762f, 0x11d0, 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c},
  22. L"ndislan" },
  23. };
  24. if (TEXT('{' == *pszParam))
  25. {
  26. return SUCCEEDED(CLSIDFromString ((LPOLESTR)pszParam, pguidClass));
  27. }
  28. else
  29. {
  30. INT i;
  31. for (i = 0;
  32. i < sizeof(MapClassGuidToName) / sizeof(MapClassGuidToName[0]);
  33. i++)
  34. {
  35. if (0 == lstrcmpiW (pszParam, MapClassGuidToName[i].pszName))
  36. {
  37. *pguidClass = MapClassGuidToName[i].guidClass;
  38. return TRUE;
  39. }
  40. }
  41. }
  42. return FALSE;
  43. }
  44. void
  45. __cdecl
  46. wmain (
  47. int argc,
  48. PCWSTR argv[])
  49. {
  50. GUID guidClass;
  51. HDEVINFO hdi;
  52. // Argument check
  53. //
  54. if (2 != argc)
  55. {
  56. printf ("%S {class guid}\n", argv[0]);
  57. return;
  58. }
  59. // Conver the string argument for the guid into a GUID.
  60. //
  61. if (!FGetClassGuidFromCmdLineParam (argv[1], &guidClass))
  62. {
  63. printf ("error: invalid class guid.\n");
  64. }
  65. // Get the devices in this class.
  66. //
  67. hdi = SetupDiGetClassDevs (&guidClass, NULL, NULL,
  68. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  69. if (hdi)
  70. {
  71. BYTE abBuffer [ sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) +
  72. (MAX_PATH * sizeof(WCHAR)) ];
  73. SP_DEVICE_INTERFACE_DATA did;
  74. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  75. DWORD i;
  76. ZeroMemory (&did, sizeof(did));
  77. did.cbSize = sizeof(did);
  78. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)abBuffer;
  79. // Enumerate the device interfaces of the class.
  80. //
  81. for (i = 0;
  82. SetupDiEnumDeviceInterfaces (hdi, NULL, &guidClass, i, &did);
  83. i++)
  84. {
  85. pDetail->cbSize = sizeof(*pDetail);
  86. if (SetupDiGetDeviceInterfaceDetail (hdi, &did,
  87. pDetail, sizeof(abBuffer), NULL, NULL))
  88. {
  89. HANDLE hFile;
  90. printf ("opening %S\n", pDetail->DevicePath);
  91. hFile = CreateFile (pDetail->DevicePath,
  92. GENERIC_READ | GENERIC_WRITE,
  93. 0, NULL, OPEN_EXISTING,
  94. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  95. NULL);
  96. if (hFile && (INVALID_HANDLE_VALUE != hFile))
  97. {
  98. printf ("press any key to close device...");
  99. while (!_kbhit())
  100. {
  101. Sleep (20);
  102. }
  103. _getch ();
  104. printf ("\n\n");
  105. CloseHandle (hFile);
  106. }
  107. else
  108. {
  109. printf ("error: CreateFile failed. (%d)\n\n",
  110. GetLastError ());
  111. }
  112. }
  113. else
  114. {
  115. printf ("error: SetupDiGetDeviceInterfaceDetail failed "
  116. "for item %d. (%d)\n", i, GetLastError ());
  117. }
  118. }
  119. SetupDiDestroyDeviceInfoList (hdi);
  120. }
  121. else
  122. {
  123. printf ("error: SetupDiGetClassDevs returned %d", GetLastError ());
  124. }
  125. }