Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Directly reading/writing a HDD

10 views
Skip to first unread message

galapogos

unread,
May 6, 2007, 11:44:52 PM5/6/07
to
Hi,

I'm writing a console program to directly read from and write to an
external USB HDD in sector level(so that I can access the drive w/o it
even being initialized/partitioned/formatted, and I'm wondering how to
get started. I've only done file level I/O before on directly
connected drives so I'm not sure how to identify the USB drive(USB
VID/
PID/serial number maybe?) and also how to address it directly. Are
there any primers for this to help me get started?

I tried playing around with GetLogicalDrives(),
GetLogicalDriveStrings() and CreateFile(), but these seem to work at
the logical level, so I don't know exactly which physical drive I'm
accessing.

Thanks!

Kellie Fitton

unread,
May 7, 2007, 1:25:54 PM5/7/07
to


Hi,

The removable MS-DOS devices such as USBs/Flash Drives can be
accessed through the \\\\.\\PhysicalDrive#. Simply call the API
QueryDosDevice() and get a list of all existing MS-DOS device
names, and use the API DefineDosDevice() to define a device name.

That said, when the function returns the listing of the devices,
you can use each of those strings in another call to the function,
to get the device name which is the physical mapping for each device.

char logical [2048];
char physical[1024];

QueryDosDevice(NULL, logical, sizeof(logical));

for (char *pos = buffer; *pos; pos+=strlen(pos)+1)
QueryDosDevice(pos, physical, sizeof(physical));

return 0;

http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B235128

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/querydosdevice.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/definedosdevice.asp

Kellie.


galapogos

unread,
May 7, 2007, 11:34:42 PM5/7/07
to
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/file...
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/file...
>
> Kellie.

Hi,
I tried your code, but after the QueryDosDevice() calls, both
logical[] and physical[] were both still undefined. Anyway, I'm trying
to access physical drives rather than logical volumes. I managed to
find some code that allows me to list all connected physical drives,
including drive vendor and product id. I can therefore identify the
actual connected drive and then presumably use that particular
physical drive number to access the drive with CreateFile().

The weird thing is that I'm unable to see the USB enclosure device's
Vendor ID and Product ID, but only the drive that's installed in the
USB enclosure. While listing all drives on the system, I am however
able to see my USB flash drive's vendor/product ID, so I'm not sure
why I'm unable to see the USB enclosure's VID/PID...matching to the
USB enclosure's VID/PID would be a better solution since it would make
it drive independent, i.e. I can write to whatever drive is installed
in the enclosure rather than a specific drive.

0 new messages