Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <windows.h>
- #include <Setupapi.h>
- #define INITGUID
- #include <Devpkey.h>
- #pragma comment(lib,"Setupapi.lib")
- void ErrorMes(LPCTSTR lpszFunction)
- {
- // Retrieve the system error message for the last-error code
- LPVOID lpMsgBuf;
- LPVOID lpDisplayBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&lpMsgBuf,
- 0, NULL);
- // Display the error message
- lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
- (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen(lpszFunction) + 40) * sizeof(TCHAR));
- wprintf(L"%s failed with error %d: %s",
- lpszFunction, dw, (LPTSTR)&lpMsgBuf);
- LocalFree(lpMsgBuf);
- LocalFree(lpDisplayBuf);
- }
- void PrintDisks(const GUID * guidInterface) {
- DEVPROPTYPE dpt = 0;
- wchar_t buffer[1024] = L"";
- DWORD RequiredSize = 0;
- SP_DEVINFO_DATA devinfo = { 0 };
- SP_DEVICE_INTERFACE_DATA deviceInterface = { 0 };
- PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetail = NULL;
- BOOL res;
- HDEVINFO deviceInfoHandle = SetupDiGetClassDevs(guidInterface, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (deviceInfoHandle != INVALID_HANDLE_VALUE)
- {
- int deviceIndex = 0;
- while (true)
- {
- ZeroMemory(&deviceInterface, sizeof(deviceInterface));
- deviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- //получение всех дисков
- if (SetupDiEnumDeviceInterfaces(deviceInfoHandle, 0, guidInterface, deviceIndex, &deviceInterface))
- {
- DWORD cbRequired = 0;
- SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &deviceInterface, 0, 0, &cbRequired, 0);
- if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
- {
- deviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(new char[cbRequired]);
- memset(deviceInterfaceDetail, 0, cbRequired);
- deviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- if (!SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &deviceInterface,
- deviceInterfaceDetail, cbRequired, &cbRequired, 0))
- {
- goto Next;
- }
- // Initialize the structure before using it.
- memset(deviceInterfaceDetail, 0, cbRequired);
- deviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- // Call the API a second time to retrieve the actual
- // device path string.
- BOOL status = SetupDiGetDeviceInterfaceDetail(
- deviceInfoHandle, // Handle to device information set
- &deviceInterface, // Pointer to current node in devinfo set
- deviceInterfaceDetail, // Pointer to buffer to receive device path
- cbRequired, // Length of user-allocated buffer
- &cbRequired, // Pointer to arg to receive required buffer length
- NULL); // Not interested in additional data
- //получение информации о устройстве
- ZeroMemory(&devinfo, sizeof(devinfo));
- devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
- BOOL success = SetupDiEnumDeviceInfo(deviceInfoHandle, deviceIndex, &devinfo);
- if (success == FALSE) { ErrorMes(L"SetupDiEnumDeviceInfo"); goto Next; }
- res = SetupDiGetDeviceProperty(deviceInfoHandle, &devinfo,
- &DEVPKEY_NAME, &dpt, (PBYTE)buffer, 1000, NULL, 0);
- if (res == FALSE) { ErrorMes(L"SetupDiGetDeviceProperty"); goto Next; }
- wprintf(L"%s: %sn", buffer, deviceInterfaceDetail->DevicePath);
- }
- }
- else
- {
- break;
- }
- Next:
- if (deviceInterfaceDetail != NULL) {
- delete[] deviceInterfaceDetail;
- deviceInterfaceDetail = NULL;
- }
- deviceIndex++; //следующее устройство
- }
- SetupDiDestroyDeviceInfoList(deviceInfoHandle);
- }
- else ErrorMes(L"SetupDiGetClassDevs");
- }
- int __cdecl main(int argc, char **argv)
- {
- PrintDisks(&GUID_DEVINTERFACE_DISK);
- PrintDisks(&GUID_DEVINTERFACE_CDROM);
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment