473,554 Members | 3,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2835
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=75 000;
DWORD nBytesRead;
DWORD nBytesWritten;
ReadFile(hCardR ead, dwImageBuffer, nBytesToCopy, &nBytesRead, NULL);
WriteFile(hFile Write, dwImageBuffer, nBytesToCopy, &nBytesWritt en,
NULL);

CloseHandle(hFi leWrite);
CloseHandle(hCa rdRead);
-------------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.t xt" 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.t xt" 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
9067
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 same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by...
18
3480
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 of how to writing code in order to catch and complete directory structure of a partition with out using windows API i mean only assembly and C or...
0
1735
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 Process=Interim Level=10 Technique=Fletching Knowledge=Woodworking Device=Sawhorse Primary components=Refined Maple
1
3644
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 XmlException. The error message is invalid date SEMICOLON expected at Line 1 Position 380. Derek you had said something about the UTF encoding...
1
5442
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 Windows/C#? I can do a create file and get a handle to the device, the problem is right now I would have to talk directly to an Interrupt and I...
0
4373
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, it simply uses the built in MS HID srv driver. It sends the results as keyboard commands. I have 2 ways to catch this data as I see it: 1) I can...
12
9157
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
3003
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 help me out with good stuff or brief explation what i need to do . Remember i need asynchronous read/write opeartion where i m using aio_read and...
9
14358
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, "That's easy, there's a lot out there, Google it,", 1 is a discussion on it without examples and the other is who knows what. I did find some...
0
7615
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7819
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5165
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2030
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.