RE: How to getdrive letter of the USB flash drive by anonymous
anonymous
Mon May 31 03:26:02 CDT 2004
Hi
The following code will help
Kira
#define _WIN32_WINNT 0x050
#include <windows.h
#include <stdio.h
#include<cfgmgr32.h
#define BUFFER_SIZE MAX_PAT
struc
char letter
char *volume
} drives[26]
int count
void GetDeviceID(LPTSTR devID, DWORD size)
void GetAllRemovableDisks()
char CheckSpecificDrive(LPTSTR devID)
void GetAllRemovableDisks(
char drive[4]
drive[0] = 'A'
drive[1] = ':'
drive[2] = '\\'
drive[3] = 0
count = 0
DWORD driveMask = GetLogicalDrives()
for(int i=0; i<26; i++
BOOL b = (driveMask & 1)
if(b
drive[0] = 'A' + i
char volume[BUFFER_SIZE]
if(GetDriveType(drive) == DRIVE_REMOVABLE
if(GetVolumeNameForVolumeMountPoint(drive, volume, BUFFER_SIZE)
drives[count].letter = drive[0]
drives[count].volume = strdup(volume)
count ++
driveMask >>= 1
#include <winioctl.h
#include <setupapi.h
#pragma comment(lib, "setupapi.lib"
char CheckSpecificDrive(LPTSTR devID
//CONST GUID* guid =&GUID_DEVINTERFACE_CDROM
CONST GUID* guid = &GUID_DEVINTERFACE_VOLUME
TCHAR clsName[16]
DWORD required
HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)
if(hDevInfo == INVALID_HANDLE_VALUE
return 0
for(DWORD nIndex = 0; ;nIndex ++
SP_DEVICE_INTERFACE_DATA devInterfaceData
SP_DEVINFO_DATA devInfoData
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail
BYTE buffer[BUFFER_SIZE]
DWORD RequiredSize, dataType, required, err
TCHAR buf[60]
DEVINST devInstParent
ZeroMemory(&devInterfaceData, sizeof(devInterfaceData))
devInterfaceData.cbSize = sizeof(devInterfaceData)
devInfoData.cbSize = sizeof(devInfoData)
if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, nIndex,&devInterfaceData)
err = GetLastError()
break
ZeroMemory(&devInfoData, sizeof(devInfoData))
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA)
pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer
pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)
SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfaceData
pDevDetail, BUFFER_SIZE, &RequiredSize, &devInfoData)
CM_Get_Device_ID(devInfoData.DevInst, buf, sizeof(buf),0)
CM_Get_Parent(&devInstParent,devInfoData.DevInst, 0)
CM_Get_Device_ID(devInstParent, buf, sizeof(buf),0)
CM_Get_Parent(&devInstParent,devInstParent, 0)
CM_Get_Device_ID(devInstParent, buf, sizeof(buf),0)
if( buf != NULL && strcmp(devID,buf) == 0
int len = strlen(pDevDetail->DevicePath)
pDevDetail->DevicePath[len] = '\\'
pDevDetail->DevicePath[len+1] = 0
char volume[BUFFER_SIZE]
if(GetVolumeNameForVolumeMountPoint(pDevDetail->DevicePath, volume,
BUFFER_SIZE)
for(int i=0; i<count; i++
if(strcmp(drives[i].volume, volume)==0
return drives[i].letter
}
SetupDiDestroyDeviceInfoList(hDevInfo)
void main(
LPTSTR devID
BYTE buf[60]
devID = (LPTSTR)buf
TCHAR drive
LPTSTR lpDirectory
DWORD size = 60
TCHAR drivePath[6]
DWORD err
lpDirectory =(LPTSTR)LocalAlloc(LPTR, BUFFER_SIZE)
GetAllRemovableDisks()
// Get USB\VID_xxxx\PID_xxxx to devID
drive = CheckSpecificDrive(devID)
sprintf(drivePath, "%c:\\",drive)
sprintf(lpDirectory,"%sTest",drivePath)
CreateDirectoryEx(drivePath, lpDirectory, NULL)
err = GetLastError()
printf(" Drive corresponding to %s is %c\n", devID, drive)