按关闭按钮应该关闭 CD Rom 托盘,但它没有关闭 - win32 C++

问题描述

我正在创建一个 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,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,NULL);      // Pointer not needed.

    }

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

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

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

            DeviceIoControl(hCdRom,IOCTL_STORAGE_EJECT_MEDIA,&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,GetLastError());
                return 1;
            }

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

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

            CloseHandle(hCdRom);

            break;
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)