• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

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)
0
Cronic
Asked:
Cronic
  • 2
  • 2
1 Solution
 
_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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now