Rolf Magnus wrote:
Benjamin de Waal wrote:
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"?
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.
--
"When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl."