I want to mount all partitions from USB drives in Windows (XP). I mean I want to assign to each of them drive letters (when they don't have one). The OS is doing this automatically but there are situations when such a program is useful.
I know how to find if a drive is on USB or not. My code so far is:
type
STORAGE_QUERY_TYPE = (PropertyStandardQuery = 0, PropertyExistsQuery, PropertyMaskQuery, PropertyQueryMaxDefined);
TStorageQueryType = STORAGE_QUERY_TYPE;
STORAGE_PROPERTY_ID = (StorageDeviceProperty = 0, StorageAdapterProperty);
TStoragePropertyID = STORAGE_PROPERTY_ID;
STORAGE_PROPERTY_QUERY = packed record
PropertyId: STORAGE_PROPERTY_ID;
QueryType: STORAGE_QUERY_TYPE;
AdditionalParameters: array[0..9] of AnsiChar;
end;
TStoragePropertyQuery = STORAGE_PROPERTY_QUERY;
STORAGE_BUS_TYPE = (BusTypeUnknown = 0, BusTypeScsi, BusTypeAtapi, BusTypeAta, BusType1394, BusTypeSsa, BusTypeFibre,
BusTypeUsb, BusTypeRAID, BusTypeiScsi, BusTypeSas, BusTypeSata, BusTypeMaxReserved = $7F);
TStorageBusType = STORAGE_BUS_TYPE;
STORAGE_DEVICE_DESCRIPTOR = packed record
Version: DWORD;
Size: DWORD;
DeviceType: Byte;
DeviceTypeModifier: Byte;
RemovableMedia: Boolean;
CommandQueueing: Boolean;
VendorIdOffset: DWORD;
ProductIdOffset: DWORD;
ProductRevisionOffset: DWORD;
SerialNumberOffset: DWORD;
BusType: STORAGE_BUS_TYPE;
RawPropertiesLength: DWORD;
RawDeviceProperties: array[0..0] of AnsiChar;
end;
TStorageDeviceDescriptor = STORAGE_DEVICE_DESCRIPTOR;
const
IOCTL_STORAGE_QUERY_PROPERTY = $002D1400;
var i: Integer;
H: THandle;
USBDrives: array of Byte;
Query: TStoragePropertyQuery;
dwBytesReturned: DWORD;
Buffer: array[0..1023] of Byte;
sdd: TStorageDeviceDescriptor absolute Buffer;
begin
SetLength(UsbDrives, 0);
SetErrorMode(SEM_FAILCRITICALERRORS);
for i := 0 to 99 do
begin
H := CreateFile(PChar('\\.\PhysicalDrive' + IntToStr(i)), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if H <> INVALID_HANDLE_VALUE then
begin
try
dwBytesReturned := 0;
FillChar(Query, SizeOf(Query), 0);
FillChar(Buffer, SizeOf(Buffer), 0);
sdd.Size := SizeOf(Buffer);
Query.PropertyId := StorageDeviceProperty;
Query.QueryType := PropertyStandardQuery;
if DeviceIoControl(H, IOCTL_STORAGE_QUERY_PROPERTY, @Query, SizeOf(Query), @Buffer, SizeOf(Buffer), dwBytesReturned, nil) then
if sdd.BusType = BusTypeUsb then
begin
SetLength(USBDrives, Length(USBDrives) + 1);
UsbDrives[High(USBDrives)] := Byte(i);
end;
finally
CloseHandle(H);
end;
end;
end;
for i := 0 to High(USBDrives) do
begin
//
end;
end.
But I don't know how to access partitions on each drive and mounts them. Can you please help me?
Have a look at the volume management functions - http://msdn.microsoft.com/en-us/library/aa365730%28v=vs.85%29.aspx
FirstFirstVolume, FindNextVolume, FindVolumeClose will list off the partitions as \\\\?\\Volume{Guid} names.
Createfile should be able to option the volume names so your code above can check it's USB.
SetVolumeMountPoint lets you mount a Volume as a drive letter or a folder mount point.
This blog entry talks about Volume names - http://blogs.msdn.com/b/adioltean/archive/2005/04/16/408947.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.