我在我的 Win32 C++ 应用程序上创建了一个“弹出”按钮,当我按下它时,它会打开 CD ROM 托盘。现在我已经硬编码了,知道我电脑上的 D 驱动器是 CD ROM 驱动器。我的问题是,如果我不知道光驱是哪个驱动器,或者在光驱不是D盘的计算机上,如何确定光驱是我计算机上的哪个驱动器并打开光盘托盘?我的代码如下所示。
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#define IDC_BUTTON 3456
static TCHAR szWindowClass[] = _T("CD_ROM_READER_APP");
static TCHAR szTitle[] = _T("CD Rom Reader App");
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
TCHAR CSCI_NO[60];
HWND TextBox;
DWORD dwBytes;
HANDLE hCdRom = CreateFile(_T("\\\\.\\D:"), //This is where I set it so that the D drive is the drive ejected
GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR heading[] = _T("CD ROM READER");
TCHAR CSCI_No_Inst[] = _T("Please enter the CSCI No below:");
switch (message)
{
case WM_CREATE:
{
HWND hwndButton = CreateWindow(
L"BUTTON", L"EJECT",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
150, 200, 100, 100,
hWnd, (HMENU)IDC_BUTTON, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
return 0;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BUTTON:
//Open CD Rom Tray when the Eject button is pressed
if (hCdRom == INVALID_HANDLE_VALUE)
{
_tprintf(_T("Error: %x"), GetLastError());
return 1;
}
// Open the door:
DeviceIoControl(hCdRom, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL);
MessageBox(NULL, L"Please insert a CD ROM in the CD tray.", L"CD ROM Drive", 0);
break;
}
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}