Reading/Writing directly to/from a device in MSWindows

Discussion in 'C++' started by Benjamin de Waal, Feb 28, 2005.

  1. 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.

    --
    "When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl."
    Benjamin de Waal, Feb 28, 2005
    #1
    1. Advertising

  2. Benjamin de Waal

    Rolf Magnus Guest

    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"?
    Rolf Magnus, Feb 28, 2005
    #2
    1. Advertising

  3. 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."
    Benjamin de Waal, Mar 3, 2005
    #3
  4. Benjamin de Waal wrote:
    >


    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?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Mar 3, 2005
    #4
  5. Karl Heinz Buchegger wrote:
    > 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.

    >> 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.


    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.
    --
    "When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl."
    Benjamin de Waal, Mar 3, 2005
    #5
  6. Benjamin de Waal wrote:
    > Karl Heinz Buchegger wrote:
    >> 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.


    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
    --
    "When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl."
    Benjamin de Waal, Mar 3, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Dale

    cant install from source on MSWindows

    Darren Dale, Aug 18, 2004, in forum: Python
    Replies:
    1
    Views:
    270
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Aug 19, 2004
  2. Tony Nelson
    Replies:
    4
    Views:
    357
    Tony Nelson
    Nov 18, 2005
  3. Darren Dale
    Replies:
    3
    Views:
    517
    Thomas Bellman
    Mar 19, 2006
  4. jalkadir
    Replies:
    6
    Views:
    329
    Howard
    Oct 12, 2005
  5. Replies:
    0
    Views:
    81
Loading...

Share This Page