Solved

CreateFile() keeps returning INVALID_HANDLE_VALUE.

Posted on 1998-06-10
7
331 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
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 60
Access Query (QBE) Using VBA Function for Criteria 13 60
SSIS 2010 Convert to SSIS 2013 On Different Box 3 59
Please explain "Multi-Tenant Services" 5 86
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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