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)
CronicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_ys_Commented:
> (LPCWSTR)DriveString
Simply casting this won't work. You're trying to move from a single byte string to multi-byte.

Also, forget FILE_ATTRIBUTE_NORMAL. Use 0 instead.

--------x-----------
USES_CONVERSION;
cdHandle = CreateFile(A2CW(DriveString), GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);
--------x-----------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
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;
    }
0
_ys_Commented:
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.
0
jkrCommented:
>>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)
0
CronicAuthor Commented:
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...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.