Reading/Writing directly to/from a device in MSWindows

B

Benjamin de Waal

Hey all,
I'm trying to figure out how to directly write to a device in Windows.
Basically, what I'm wanting to do is create an image of a device
(specifically, a CompactFlash card that uses a filesystem Windows doesn't
recognise), store it as a file, modify it and dump it back to the card.
Currently, in version 0.001 of my program, I'm just calling "dd for
Windows" using ShellExecute - this works perfectly, but obviously this isn't
exactly acceptable for the final product! (and in the kludgy way I've done
it, it locks the whole program and I can't implement a progress bar etc etc)

I'm HOPING it's as easy as using fopen and just treating it like a file
from then on (that'll my Unix background speaking)...

If it makes a difference, I'm using Visual C++ 6 and this is an MFC app,
but whether the solution is MFC or otherwise, I'm really not fussy!

Any help (be they code snippets or even just pointers to documentation)
would be much appreciated.

Regards,
Ben de Waal.
 
R

Rolf Magnus

Benjamin said:
Hey all,
I'm trying to figure out how to directly write to a device in Windows.
Basically, what I'm wanting to do is create an image of a device
(specifically, a CompactFlash card that uses a filesystem Windows doesn't
recognise), store it as a file, modify it and dump it back to the card.
Currently, in version 0.001 of my program, I'm just calling "dd for
Windows" using ShellExecute - this works perfectly, but obviously this
isn't exactly acceptable for the final product! (and in the kludgy way
I've done it, it locks the whole program and I can't implement a progress
bar etc etc)

I'm HOPING it's as easy as using fopen and just treating it like a file
from then on (that'll my Unix background speaking)...

How about looking into the source code of "dd for Windows"?
 
B

Benjamin de Waal

Rolf said:
How about looking into the source code of "dd for Windows"?

Okay, I've gotten a bit further, but am still a bit stuck. I'm using
CreateFile, ReadFile and WriteFile to do the work, but am now stuck just a
bit further in...
I took your suggestion and looked at the source for "dd for Windows", read
it, and unfortunately failed to understand what's fundamentally different.
It too is using CreateFile, ReadFile and WriteFile (however being written in
Pascal I'm not 100% certain if it's doing anything else particularly
interesting).
Specifically, my current code looks something like this (error checking,
variable names for drives/files etc and so forth removed for brevity):

------------code begins here------------
//Step 1: Open the drive for reading
HANDLE hCardRead;
hCardRead = CreateFile("\\.\a:", GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

//Step 2: Open the file for writing
HANDLE hFileWrite;
hFileWrite = CreateFile("c:\\floppy.img", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

//Step 3: Read the drive and write to the card
DWORD dwImageBuffer[75000];
DWORD nBytesToCopy=75000;
DWORD nBytesRead;
DWORD nBytesWritten;
ReadFile(hCardRead, dwImageBuffer, nBytesToCopy, &nBytesRead, NULL);
WriteFile(hFileWrite, dwImageBuffer, nBytesToCopy, &nBytesWritten,
NULL);

CloseHandle(hFileWrite);
CloseHandle(hCardRead);
-------------code ends here-------------

75000 is just some arbritrary number of no significance that I'm using
during testing. Once I get it working, it will be designed to read the
entire device.
What I'd EXPECT this snippet to do is read the first 75000bytes of the
floppy drive and dump them to a file. It seems to give me fairly random
looking garbage. In one instance, when I was doing it from a CompactFlash
card (the intended medium to be reading from), it gave me 75000 empty bytes
(Hex 00) - this was from a FAT formatted card that even had files on it, so
I KNOW there's something in the first 75000 bytes.
All this was leading me to believe I fundamentally misunderstood how
CreateFile, ReadFile and WriteFile are supposed to work. So I tried a
little experiment and found that if I replace "\\.\a:" with
"c:\\somefile.txt" it works perfectly. This makes me think what I'm doing
really SHOULD work, just doesn't.

Any help?

Regards,
Ben de Waal.
 
K

Karl Heinz Buchegger

Lesson one:
When dealing with files, always include code for error checking.
Especially you always want a check if the open succeeded.
hCardRead = CreateFile("\\.\a:", GENERIC_READ, 0, NULL, OPEN_EXISTING,

********

What's that?
Certainly it is not a valid filename. I guess when you look at hCardRead
it will not have a valid file handle in it.
little experiment and found that if I replace "\\.\a:" with
"c:\\somefile.txt" it works perfectly. This makes me think what I'm doing
really SHOULD work, just doesn't.

What makes you think, that "\\.\a:" is correct. For one the sequence
"\a" is an escape character followed by 'a', just like "\n" denotes
escape character followed by 'n', which generally means 'carriage return',
or "\t" is an escape character followed by 't', which means: tabulator.
Any help?

Did you forget that in order to include a single '\' in a string, you have
to write 2 of them?
 
B

Benjamin de Waal

Karl said:
Lesson one:
When dealing with files, always include code for error checking.
Especially you always want a check if the open succeeded.

Actually, as stated, the real code does have error checking - I just cut it
out for brevity in the example I posted... What I posted isn't exactly
what's in my program, but demonstrates the problem.
********

What's that?
Certainly it is not a valid filename. I guess when you look at
hCardRead it will not have a valid file handle in it.

Ooops... my fault - the code actually reads "hCardRead =
CreateFile(TEXT(devicename)"... where "devicename" is a valid drive
preceeded by "\\.\" - the example should have read "\\\\.\\a:" - my bad.
Did you forget that in order to include a single '\' in a string, you
have to write 2 of them?

Only when typing my example - not in the real code!

So... any ideas why it's not working?

Regards,
Ben de Waal.
 
B

Benjamin de Waal

Benjamin said:
Actually, as stated, the real code does have error checking - I just
cut it out for brevity in the example I posted... What I posted
isn't exactly what's in my program, but demonstrates the problem.

Err... and to clarify further - I KNOW the open succeeded - both through
error checking and the fact that when I use the floppy disk, I hear it spin!

Regards,
Ben de Waal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top