C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 09-04-2008, 07:39 AM   #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.
_izua_ is offline  
Old 09-04-2008, 07:40 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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
__________________
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.

Last edited by matsp; 09-04-2008 at 07:44 AM.
matsp is offline  
Old 09-04-2008, 07:59 AM   #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).
_izua_ is offline  
Old 09-04-2008, 08:05 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline  
Old 09-04-2008, 09:27 PM   #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?
_izua_ is offline  
Old 09-04-2008, 11:17 PM   #6
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,929
yes it will
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline  
Old 09-05-2008, 08:21 AM   #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
_izua_ is offline  
Old 09-05-2008, 11:03 PM   #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.
_izua_ is offline  
Old 11-02-2009, 06:23 AM   #9
Registered User
 
Join Date: Nov 2009
Posts: 6
Hello _izua_,

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

Thanks!
hans75 is offline  
Old 11-02-2009, 07:05 AM   #10
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
hans75,

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

Thanks
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline  
Closed Thread

Tags
raw volume access, sd card

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:17 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22