Link to home
Start Free TrialLog in
Avatar of Richard_Thompson
Richard_Thompson

asked on

CreateFile() keeps returning INVALID_HANDLE_VALUE.

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
Avatar of kinkajou
kinkajou

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.
ASKER CERTIFIED SOLUTION
Avatar of alamo
alamo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
Avatar of Richard_Thompson

ASKER

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?)
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.
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!