Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CreateFile() keeps returning INVALID_HANDLE_VALUE.

Posted on 1998-06-10
7
Medium Priority
?
340 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 200 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

597 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