Thread: Raw volume access (to a usb volume)

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    Raw volume access (to a usb volume)

    I need to read and write to an USB mounted volume at raw level. It's a SD card in a card reader.

    It has a custom file format I made- and a custom-made electronic device writes to it/reads from it. Now I need to make this filesystem accesible to the computer (although not through explorer.exe).
    I need to make an app which will show the files and read/write/delete them. I haven't found anything like this by googling but I know it's possible - software like winHex can do it.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just use CreateFile() with the devicename (e.g "H:" itself).

    Edit: You may want to disable autorun, so that Windows doesn't open the device - you can only open a raw device if nothing else has it opened.

    Edit2: Changed to "CreateFile", as OpenFile is an old API function.

    --
    Mats
    Last edited by matsp; 09-04-2008 at 07:44 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    oookay, I'm not exactly familliar with windows api file manipulation. may i ask you what other functios i will use - for closing, reading with an offset, writing to an offset ? but this seems to be the solution.

    here's a catch - sd cards are read/written in hardware in blocks of 512 bytes. is windows aware of this, or if i'll choose to write in the 253rd byte of block 2, the whole block gets deleted (blocks must be deleted before beeing changed).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The MSDN page: CreateFile has some help on this function, and if you scroll down (quite a long way) it will have links to CloseFile, ReadFile, WriteFile, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    I don't have a card reader handy - but if I insert an unformatted volume, will windows allocate it a volume letter?

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    yes it will

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    So, I got a card reader, wrote data from the device, put the card in the reader, saw it in winhex.

    Next step, doing it programatically.
    Code:
        cout << "attempting CreateFile() on F:" << endl;
    
        HANDLE vol = CreateFile(
            "\\.\F:",               //read a volume
            0, //GENERIC_READ | GENERIC_WRITE, //access
            0,                      //disables any sharing operation
            NULL,                   //no security descriptor
            OPEN_EXISTING,          //it's a volume, that's what MSDN recommends
            FILE_ATTRIBUTE_NORMAL,  //wtf should go here?
            NULL);                  //a template file something
    
        if (GetLastError != 0)
            coutError("main");
    
        CloseHandle(vol);
    (coutError is simply a wrapper to show the windows system error in cout).
    Running the code above returns 123: System error #123 in main: The filename, directory name, or volume label syntax is incorrect.
    MSDN says volumes should be opened this way: \\.\letter:

    Anyway, I try using F:, and I get access denied, although nothing else is using it (no explorer open on it).

    Maybe I should open it as a \\.\PhysicalDrive
    To get a volume's physical drive, i have to pass to DeviceIOControl a handle from.. CreateFile! Which of course, dies in access denied. Maybe I'm not using the right sharing/access request?

    I've also read at some point that I can request an explicit unmount. How do I do that?
    edit: Hmm, it looks like "\\\\.\\F:" does the trick. it figures.
    Last edited by _izua_; 09-05-2008 at 08:27 AM. Reason: stupid problem between chair and monitor

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    Well, it works! Thanks!
    I was able to read/write data from the card. Windows proves again a very useful sdk, you can only write in 512 bytes clusters, same for reading.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Hello _izua_,

    I want to do the same. Wich API's did you use?
    Can you post the funktions :-)

    Thanks!

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    hans75,

    We discourage people from reserecting old threads. If you want to discuss this, could you please start a new thread.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot access my usb partition! PLEASE HELP!
    By geek@02 in forum Tech Board
    Replies: 0
    Last Post: 03-23-2008, 05:45 AM
  2. C functions to access USB port.
    By nethunter in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-10-2006, 02:25 PM
  3. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 07:49 PM
  4. Printers On USB
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-10-2002, 11:48 AM
  5. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM

Tags for this Thread