E
ElyanPoujol
Guest
Hello everyone!
Using the SetupAPI and more precisely the SetupDiGetDeviceInterfaceDetail function I am able to get the device path to my usb flash drive: \\?\usbstor#disk&ven_sandisk&prod_cruzer&rev_8.02#35151307ca62eb53&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
The SetupDiGetDeviceInterfaceDetail doc states that "The interface detail returned by this function consists of a device path that can be passed to Win32 functions such as CreateFile" but I was unable to open any file on my usb flash drive.
Am I misunderstanding something or doing it wrong ?
My code responsible for enumerating the drives:
#include "stdafx.h"
int main() {
GUID classGuid = GUID_DEVINTERFACE_DISK; // We only want disks
SP_DEVICE_INTERFACE_DATA devInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA_A pDevInterfaceDetailData;
DWORD devInterfaceDetailDataSize;
HDEVINFO devInfoSet = SetupDiGetClassDevsA(&classGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (devInfoSet == INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error with SetupDiGetClassDevsW");
return 1;
}
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (int i = 0; SetupDiEnumDeviceInterfaces(devInfoSet, NULL, &classGuid, i, &devInterfaceData); i++) {
devInterfaceDetailDataSize = 0;
if (!SetupDiGetDeviceInterfaceDetailA(devInfoSet, &devInterfaceData, NULL, 0, &devInterfaceDetailDataSize, NULL)) {
/*SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error getting SP_DEVICE_INTERFACE_DETAIL_DATA_A size, error %lu", GetLastError());
return 1;*/
}
pDevInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, devInterfaceDetailDataSize);
if (!pDevInterfaceDetailData) {
SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error allocating SP_DEVICE_INTERFACE_DETAIL_DATA_A");
return 1;
}
pDevInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
if (!SetupDiGetDeviceInterfaceDetailA(devInfoSet, &devInterfaceData, pDevInterfaceDetailData, devInterfaceDetailDataSize, NULL, NULL)) {
/*SetupDiDestroyDeviceInfoList(devInfoSet);
HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
printf("Error retrieving SP_DEVICE_INTERFACE_DETAIL_DATA_A");*/
return 1;
}
printf("Path: %s\n", pDevInterfaceDetailData->DevicePath);
HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
}
SetupDiDestroyDeviceInfoList(devInfoSet);
return 0;
}
To test the path I made this code that prints the size of a given file:
#include "stdafx.h"
int main(int argc, char * argv[]) {
HANDLE hFile;
LPWSTR lpwsError;
ULONGLONG fileSize;
if (argc != 2) {
printf("No file path supplied!");
}
hFile = CreateFileA(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPWSTR)&lpwsError, 200, NULL);
printf("%ws", lpwsError);
LocalFree(lpwsError);
return 1;
}
LARGE_INTEGER largeSize;
if (!GetFileSizeEx(hFile, &largeSize)) {
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPWSTR)&lpwsError, 200, NULL);
printf("%ws", lpwsError);
LocalFree(lpwsError);
return 1;
}
fileSize = largeSize.QuadPart;
printf("File size: %llu", fileSize);
return 0;
}
Thank you advance for your contribution.
Continue reading...
Using the SetupAPI and more precisely the SetupDiGetDeviceInterfaceDetail function I am able to get the device path to my usb flash drive: \\?\usbstor#disk&ven_sandisk&prod_cruzer&rev_8.02#35151307ca62eb53&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
The SetupDiGetDeviceInterfaceDetail doc states that "The interface detail returned by this function consists of a device path that can be passed to Win32 functions such as CreateFile" but I was unable to open any file on my usb flash drive.
Am I misunderstanding something or doing it wrong ?
My code responsible for enumerating the drives:
#include "stdafx.h"
int main() {
GUID classGuid = GUID_DEVINTERFACE_DISK; // We only want disks
SP_DEVICE_INTERFACE_DATA devInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA_A pDevInterfaceDetailData;
DWORD devInterfaceDetailDataSize;
HDEVINFO devInfoSet = SetupDiGetClassDevsA(&classGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (devInfoSet == INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error with SetupDiGetClassDevsW");
return 1;
}
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (int i = 0; SetupDiEnumDeviceInterfaces(devInfoSet, NULL, &classGuid, i, &devInterfaceData); i++) {
devInterfaceDetailDataSize = 0;
if (!SetupDiGetDeviceInterfaceDetailA(devInfoSet, &devInterfaceData, NULL, 0, &devInterfaceDetailDataSize, NULL)) {
/*SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error getting SP_DEVICE_INTERFACE_DETAIL_DATA_A size, error %lu", GetLastError());
return 1;*/
}
pDevInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, devInterfaceDetailDataSize);
if (!pDevInterfaceDetailData) {
SetupDiDestroyDeviceInfoList(devInfoSet);
printf("Error allocating SP_DEVICE_INTERFACE_DETAIL_DATA_A");
return 1;
}
pDevInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
if (!SetupDiGetDeviceInterfaceDetailA(devInfoSet, &devInterfaceData, pDevInterfaceDetailData, devInterfaceDetailDataSize, NULL, NULL)) {
/*SetupDiDestroyDeviceInfoList(devInfoSet);
HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
printf("Error retrieving SP_DEVICE_INTERFACE_DETAIL_DATA_A");*/
return 1;
}
printf("Path: %s\n", pDevInterfaceDetailData->DevicePath);
HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
}
SetupDiDestroyDeviceInfoList(devInfoSet);
return 0;
}
To test the path I made this code that prints the size of a given file:
#include "stdafx.h"
int main(int argc, char * argv[]) {
HANDLE hFile;
LPWSTR lpwsError;
ULONGLONG fileSize;
if (argc != 2) {
printf("No file path supplied!");
}
hFile = CreateFileA(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPWSTR)&lpwsError, 200, NULL);
printf("%ws", lpwsError);
LocalFree(lpwsError);
return 1;
}
LARGE_INTEGER largeSize;
if (!GetFileSizeEx(hFile, &largeSize)) {
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPWSTR)&lpwsError, 200, NULL);
printf("%ws", lpwsError);
LocalFree(lpwsError);
return 1;
}
fileSize = largeSize.QuadPart;
printf("File size: %llu", fileSize);
return 0;
}
Thank you advance for your contribution.
Continue reading...