daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Dec 19th, 2018 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <Setupapi.h>
  6. #define INITGUID
  7. #include <Devpkey.h>
  8.  
  9. #pragma comment(lib,"Setupapi.lib")
  10.  
  11. void ErrorMes(LPCTSTR lpszFunction)
  12. {
  13.     // Retrieve the system error message for the last-error code
  14.  
  15.     LPVOID lpMsgBuf;
  16.     LPVOID lpDisplayBuf;
  17.     DWORD dw = GetLastError();
  18.  
  19.     FormatMessage(
  20.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
  21.         FORMAT_MESSAGE_FROM_SYSTEM |
  22.         FORMAT_MESSAGE_IGNORE_INSERTS,
  23.         NULL,
  24.         dw,
  25.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  26.         (LPTSTR)&lpMsgBuf,
  27.         0, NULL);
  28.  
  29.     // Display the error message
  30.  
  31.     lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  32.         (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen(lpszFunction) + 40) * sizeof(TCHAR));
  33.     wprintf(L"%s failed with error %d: %s",
  34.         lpszFunction, dw, (LPTSTR)&lpMsgBuf);
  35.  
  36.     LocalFree(lpMsgBuf);
  37.     LocalFree(lpDisplayBuf);
  38. }
  39.  
  40. void PrintDisks(const GUID * guidInterface) {
  41.  
  42.  
  43.     DEVPROPTYPE dpt = 0;
  44.     wchar_t buffer[1024] = L"";
  45.  
  46.     DWORD RequiredSize = 0;
  47.     SP_DEVINFO_DATA devinfo = { 0 };
  48.     SP_DEVICE_INTERFACE_DATA deviceInterface = { 0 };
  49.     PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetail = NULL;
  50.     BOOL res;
  51.  
  52.     HDEVINFO deviceInfoHandle = SetupDiGetClassDevs(guidInterface, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  53.  
  54.     if (deviceInfoHandle != INVALID_HANDLE_VALUE)
  55.     {
  56.         int deviceIndex = 0;
  57.         while (true)
  58.         {
  59.             ZeroMemory(&deviceInterface, sizeof(deviceInterface));
  60.             deviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  61.  
  62.             //получение всех дисков
  63.             if (SetupDiEnumDeviceInterfaces(deviceInfoHandle, 0, guidInterface, deviceIndex, &deviceInterface))
  64.             {
  65.                 DWORD cbRequired = 0;
  66.  
  67.                 SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &deviceInterface, 0, 0, &cbRequired, 0);
  68.                 if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
  69.                 {
  70.                     deviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(new char[cbRequired]);
  71.                     memset(deviceInterfaceDetail, 0, cbRequired);
  72.                     deviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  73.  
  74.                     if (!SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &deviceInterface,
  75.                         deviceInterfaceDetail, cbRequired, &cbRequired, 0))
  76.                     {
  77.                         goto Next;
  78.                     }
  79.  
  80.                     // Initialize the structure before using it.
  81.                     memset(deviceInterfaceDetail, 0, cbRequired);
  82.                     deviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  83.  
  84.                     // Call the API a second time to retrieve the actual
  85.                     // device path string.
  86.                     BOOL status = SetupDiGetDeviceInterfaceDetail(
  87.                         deviceInfoHandle,  // Handle to device information set
  88.                         &deviceInterface,     // Pointer to current node in devinfo set
  89.                         deviceInterfaceDetail,   // Pointer to buffer to receive device path
  90.                         cbRequired,   // Length of user-allocated buffer
  91.                         &cbRequired,  // Pointer to arg to receive required buffer length
  92.                         NULL);        // Not interested in additional data
  93.  
  94.  
  95.                     //получение информации о устройстве
  96.                     ZeroMemory(&devinfo, sizeof(devinfo));
  97.                     devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
  98.                     BOOL success = SetupDiEnumDeviceInfo(deviceInfoHandle, deviceIndex, &devinfo);
  99.                     if (success == FALSE) { ErrorMes(L"SetupDiEnumDeviceInfo"); goto Next; }
  100.  
  101.                     res = SetupDiGetDeviceProperty(deviceInfoHandle, &devinfo,
  102.                         &DEVPKEY_NAME, &dpt, (PBYTE)buffer, 1000, NULL, 0);
  103.                     if (res == FALSE) { ErrorMes(L"SetupDiGetDeviceProperty"); goto Next; }
  104.  
  105.                     wprintf(L"%s: %sn", buffer, deviceInterfaceDetail->DevicePath);
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 break;
  111.             }
  112.  
  113.         Next:
  114.             if (deviceInterfaceDetail != NULL) {
  115.                 delete[] deviceInterfaceDetail;
  116.                 deviceInterfaceDetail = NULL;
  117.             }
  118.  
  119.  
  120.  
  121.             deviceIndex++; //следующее устройство
  122.         }
  123.  
  124.         SetupDiDestroyDeviceInfoList(deviceInfoHandle);
  125.     }
  126.     else ErrorMes(L"SetupDiGetClassDevs");    
  127.  
  128. }
  129.  
  130.  
  131. int __cdecl main(int argc, char **argv)
  132. {
  133.     PrintDisks(&GUID_DEVINTERFACE_DISK);
  134.     PrintDisks(&GUID_DEVINTERFACE_CDROM);
  135.     getchar();
  136.     return 0;    
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top