0

我正在创建一个 win32 C++ 应用程序,其中按“弹出”按钮打开 CD ROM 托盘,按“关闭”按钮关闭 CD ROM 托盘。“弹出”按钮起作用并且托盘打开,但是,按“关闭”按钮不会关闭 CD ROM 托盘。我希望它找到 CD ROM 驱动器(无论是从计算机到计算机的)并关闭托盘。我正在使用“DeviceIoControl”和“IOCTL_STORAGE_LOAD_MEDIA”。我的代码的“弹出”按钮部分如下所示(仅代码的相关部分):

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>

#define IDC_BUTTON                  3456
#define BUF_SIZE                     512

WCHAR buf[BUF_SIZE];
LPWSTR pBuf = buf;
DWORD chrCopied = GetLogicalDriveStrings(BUF_SIZE - 1, buf);

TCHAR DRIVE[200];

DWORD dwBytes;
HANDLE hCdRom;


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        while (chrCopied)
        {
            if (DRIVE_CDROM == GetDriveType(pBuf))
            {
                wsprintf(DRIVE, L"%c%c.%c%s", 0x5c, 0x5c, 0x5c, pBuf);  //  Displays drive name as: \\.\D:\ 
                size_t indexOfNull = _tcslen(DRIVE);
                DRIVE[indexOfNull - 1] = '\0';             // removes the last \ to make the file name \\.\D:
            }
            size_t len = _tcslen(buf);
            chrCopied -= len + 1;
            pBuf += len + 1;
        }

        HWND hwndButton = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed 
            L"EJECT",   // Button text 
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
            150,         // x position 
            200,        // y position 
            100,        // Button width
            100,        // Button height
            hWnd,     // Parent window
            (HMENU)IDC_BUTTON,       // No menu.
            (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
            NULL);      // Pointer not needed.

        HWND hwndButton_Close = CreateWindow(
                L"BUTTON",  // Predefined class; Unicode assumed 
                L"CLOSE",   // Button text 
                WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
                263,         // x position 
                200,        // y position 
                100,        // Button width
                100,        // Button height
                hWnd,     // Parent window
                (HMENU)IDC_BUTTON_CLOSE,       // No menu.
                (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                NULL);      // Pointer not needed.

    }

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDC_BUTTON:

            hCdRom = CreateFile(DRIVE,
                GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

            if (hCdRom == INVALID_HANDLE_VALUE)
            {
                _tprintf(_T("Error: %x"), GetLastError());
                return 1;
            }

            DeviceIoControl(hCdRom, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL);
            if (hCdRom == 0)
            {
                _tprintf(_T("Error: %x"), GetLastError());
                return 1;
            }
            MessageBox(NULL, L"Please insert a CD ROM in the CD tray.", L"CD ROM Drive", 0);

            CloseHandle(hCdRom);

            break;

        case IDC_BUTTON_CLOSE:

            hCdRom = CreateFile(DRIVE,
                GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

            if (hCdRom == INVALID_HANDLE_VALUE)
            {
                _tprintf(_T("Error: %x"), GetLastError());
                return 1;
            }

            //Close CD Rom Tray
            DeviceIoControl(hCdRom, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwBytes_close, NULL);

            if (hCdRom == 0)
            {
                _tprintf(_T("Error: %x"), GetLastError());
                return 1;
            }

            CloseHandle(hCdRom);

            break;
        }
    }
}
4

0 回答 0