Sophia
Sophia

Reputation: 67

Pressing Close button should close CD Rom Tray but it is not closing - win32 C++

I am creating a win32 C++ app in which pressing the "Eject" button opens the CD Rom Tray and pressing the "Close" button closes the CD Rom Tray. The "Eject" button works and the tray opens, however, pressing the "Close" button does not close the CD Rom Tray. I want it to find the CD Rom drive (whatever that might be from computer to computer) and close the tray. I am using "DeviceIoControl" and "IOCTL_STORAGE_LOAD_MEDIA". The "Eject" button part of my code is shown below (only the relevant parts of the code):

#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;
        }
    }
}

Upvotes: 0

Views: 116

Answers (0)

Related Questions