Link to home
Start Free TrialLog in
Avatar of Cronic
Cronic

asked on

Using CreateFile to get the CD ROM Handle

I'm trying to get the handle of the CD ROM device on my system with CreateFile, and I then want to Open / close the tray, and other stuff with my new handle. Unfortunatly for the life of me I can't get the damn thing to work. Every time I call the function, CreateFile errors with a rc of 2 (Which from the web looks like ERROR_FILE_NOT_FOUND). I know that my CD Rom is D, and GetLogicalDriveString and GetDriveType confirm it, yet I still get the error. My Code looks like:

         CDLetter = 'D'; // HardCode for time being
      // Create the handle to our CD Rom
      sprintf(DriveString, "\\\\.\\%c:", CDLetter);
      TRACE1("Drive String [%s]", DriveString);
      cdHandle = CreateFile((LPCWSTR)DriveString, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

      if (cdHandle == INVALID_HANDLE_VALUE)
      {
        rc = GetLastError();
          TRACE1("CreateFile error %d\n", rc);
        return rc;
      }

Output of Trace:

j:\winddk\src\cd_ripper\driveinfo.cpp 93: Drive String [\\.\D:]
j:\winddk\src\cd_ripper\driveinfo.cpp 101: CreateFile error 2

My Trace function shows me that the string is "\\.\D:", which is what the documentation says it should look like, but I still can't get it to work. This can't be that hard... what am I doing wrong??

(If it makes a difference this is in a function of a DLL written with the Win XP SP1 DDK)
ASKER CERTIFIED SOLUTION
Avatar of _ys_
_ys_

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
Avatar of jkr
Id'd fuse UNICODE in general instead of converting:

     wchat_t   DriveString [ 16];
     wchar_t    CDLetter = L'D'; // HardCode for time being
    // Create the handle to our CD Rom
    wsprintf(DriveString, L"\\\\.\\%c:", CDLetter);
     TRACE1("Drive String [%S]", DriveString);
    cdHandle = CreateFile(DriveString, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (cdHandle == INVALID_HANDLE_VALUE)
    {
      rc = GetLastError();
         TRACE1("CreateFile error %d\n", rc);
      return rc;
    }
Avatar of _ys_
_ys_

Agreed ... but notepad doesn't verify C++ syntax very well.

Doesn't TRACE1 simply use printf. So I'm not sure what it would output.

Not only does notepad not check syntax, it doesn't compile code either. I really should get me a compiler.
>>Doesn't TRACE1 simply use printf. So I'm not sure what it would output.

Thus the '%S' - the Windows CRT routines will do a UNICODE->ANSI conversion when a capital letter is used as a format specifier (that works vice versa, also)
Avatar of Cronic

ASKER

Hmm, I'm a clown... in my cut and paste frenzy I somehow was compiling my dll with unicode support, which is not something I actually wanted. I only cast DriveString there because it wouldn't compile otherwise ;-) That will teach me for not trying to fix error messages properly.

So now it works properly... yay... Thanks for the input guys...

Oh BTW TRACE was something I wrote, and no it doesn't use printf...