Solved

CreateFile() keeps returning INVALID_HANDLE_VALUE.

Posted on 1998-06-10
7
327 Views
Last Modified: 2013-12-03
I am writing a disk copier in MS Visual C++ V. 4 which copies entire disks sector by sector and run Windows '95.  I am using CreateFile() to get a handle to a floppy disk drive, then using ReadFile() to read the contents of the entire disk into memory.  However, when I try to get a handle to drive A, I keep getting an INVALID_HANDLE_VALUE.  I tried it with drive C (my HD) and got the same result as well.  I enclose my code fragment below.  Do you know why it's not working and if so, what can I do to get it working?

HANDLE pFile;

pFile = CreateFile("\\.\A:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);

// pFile now contains INVALID_HANDLE_VALUE, rather than a handle to drive A!!

Thanks for your time

Richard_Thompson
0
Comment
Question by:Richard_Thompson
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:kinkajou
Comment Utility
I cut you code fragment and had to change "\\.\A:" to "A:\\"

     pFile = CreateFile("A:\\",
     GENERIC_READ | GENERIC_WRITE,
     FILE_SHARE_READ | FILE_SHARE_WRITE,
     NULL,
     OPEN_EXISTING,
     0,
     NULL
     );

If this fixes your problem, let me post the answer.
0
 
LVL 6

Accepted Solution

by:
alamo earned 50 total points
Comment Utility
The problem is that you aren't escaping your device string:

"\\.\A:" should be "\\\\.\\A:".

Good luck!
0
 
LVL 2

Expert Comment

by:kinkajou
Comment Utility
I cut you code fragment and had to change "\\.\A:" to "A:\\"

     pFile = CreateFile("A:\\",
     GENERIC_READ | GENERIC_WRITE,
     FILE_SHARE_READ | FILE_SHARE_WRITE,
     NULL,
     OPEN_EXISTING,
     0,
     NULL
     );

If this fixes your problem, let me post the answer.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:alamo
Comment Utility
Sorry,  kinkajou, I hadn't seen your answer before I posted mine. However, "A:\\" won't work - Richard_Thompson had the right string format, just didn't escape it.
0
 

Author Comment

by:Richard_Thompson
Comment Utility
Thanks for your comments/answers.  Unfortunately, I tried using the device string "\\\\.\\A:", but got exactly the same result, INVALID_HANDLE_VALUE.  GetLastError() returns 2, if this is any help to you.  I also tried "A:\\", but that failed as well with GetLastError() returning 5.  Could anything else be wrong with the code fragment? (is it Windows '95 compatible?)
0
 
LVL 2

Expert Comment

by:kinkajou
Comment Utility
I created a test program using the code fragment with the example fixes and got the same results as Richard_Thompson. My Windows information suggest that the code fragment with alamo's suggestion should work.
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
Sorry about the confusion... the misquoted string seemed so obvious a problem, I misinterpreted a note in MS's doc about it not working in Win95, I thought that was overcome by the other flags you set. I have much more NT experience than 95 experience, and most of the time it makes no difference, but with direct device access it turns out Win95 device access is quite different than NT (much more like Win3.1 access).

In Win95 you need to open the handle not to the logical device, but instead to a VXD. Therefore, instead of "\\\\.\\A:" you need to specify "\\\\.\\vwin32", to specify the system vxd, VWIN32.VXD, which supports the input and output control (IOCTL) functions originally provided by MS-DOS Interrupt 21h. At that point you can either call DeviceIOControl or call int21 directly.

The article http://support.microsoft.com/support/kb/articles/q163/9/20.asp gives an example of how to open the floppy and query its device characteristics under Win95, which is a good starting point and something you'll need to do anyway.

Sorry for the confusion, this should clear it up. Good luck!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now