473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reading/Writing directly to/from a device in MSWindows

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."
Jul 23 '05 #1
5 2814
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"?

Jul 23 '05 #2
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."
Jul 23 '05 #3
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
kb******@gascad.at
Jul 23 '05 #4
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."
Jul 23 '05 #5
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."
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
18
by: shakeel-ur-rehman | last post by:
I am wrtiting programm in C++ to read/take complete image of the partiton. can any one let me know the internet resources and web sites having tutorials relating to this and tell any sequence ...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Ldaled | last post by:
Okay, I had a previous post called reading an XML document. Since this post I have revised my code and got it to work. Now, Like Derek had mentioned in answer to my previous post, I am getting an...
1
by: Eric Renken | last post by:
We are developing an HID device and I have a couple questions about communicating with it. I need to be able to do a direct Interrupt Endpoint call to the USB device. Is this possible to do from...
0
by: Chris Herring | last post by:
I am writing a WinForms app that will integrate a handheld voting device system into the app. The handhelds communicate to the PC via a USB connection. The USB device has no manufacturer driver,...
12
by: Krunoslav Ostrouska | last post by:
How can i write on specifyed sector on a multimedia card? I must go round file system and write/read direct. C# VS2003 Thanks Kruno
1
by: kameswara | last post by:
Hi folks , Can anyone help me out the problem regarding writing and reading data to/from a serial device like ttyS0 in linux . Writing is being done but unable to read from it . could u plea...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.