Solved

CreateFile() keeps returning INVALID_HANDLE_VALUE.

Posted on 1998-06-10
7
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:kinkajou
ID: 1407408
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
ID: 1407409
The problem is that you aren't escaping your device string:

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

Good luck!
0
 
LVL 2

Expert Comment

by:kinkajou
ID: 1407410
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 6

Expert Comment

by:alamo
ID: 1407411
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
ID: 1407412
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
ID: 1407413
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
ID: 1407414
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

717 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