Devs.site

Get the optical drives using the Windows API

With the help of this simple function you will be able to retrieve a std::vector of all the optical drives available in the system at the moment, both physical and virtual.

A struct will be created for each one, with the name, path, serial number and file system type. You can replace it with a class and extend it according to your needs, of course

Wide chars are used, to support special characters


In order to show all the info in a console application, you can use this code:

int main()
{
    std::vector<driveinfo> drives_list = GetOpticalDiscDrives();

    std::wcout << "Found optical drives:" << std::endl << std::endl;
    for (std::vector<driveinfo>::iterator it = drives_list.begin(); it != drives_list.end(); ++it) {
        std::wcout << "Disc name: " << ((driveinfo)*it).name << std::endl;
        std::wcout << "Disc path: " << ((driveinfo)*it).path << std::endl;
        std::wcout << "SN: " << ((driveinfo)*it).serial_number << std::endl;
        std::wcout << "File System: " << ((driveinfo)*it).file_system << std::endl;
        std::wcout << "---" << std::endl;
    }

    std::wcout << "Press any key to close";
    std::cin.get();

    return 0;
}

Code

#include <windows.h>

#include <stdio.h>
#include <tchar.h>

#include <string>
#include <vector>
#include <iostream>
#include <sstream>

/**
* Maximum amount of drives to read
*/
#define MAX_DRIVES      100

/**
* Structure for drive information
*/
struct driveinfo
{
    std::wstring path;
    std::wstring name;
    std::wstring file_system;
    LPDWORD serial_number;

    driveinfo() :
        serial_number(0)
    {}
};

/**
* Returns all the optical disc drives (name and letter)
*/
std::vector<driveinfo> GetOpticalDiscDrives()
{
    // The list of drives will be stored here
    std::vector<driveinfo> optical_drives;

    // Allocate space for drives info buffer
    DWORD dw_size = MAX_DRIVES;
    wchar_t sz_logical_drives[MAX_DRIVES] = { 0 };

    // Get the drives with the help of the Windows API
    DWORD dw_result = GetLogicalDriveStrings(dw_size, sz_logical_drives);

    // If amount of drives within accepted range
    if (dw_result > 0 && dw_result <= MAX_DRIVES)
    {
        wchar_t* sz_single_drive = sz_logical_drives;
        // Loop through all optical discs
        while (*sz_single_drive)
        {
            // If this is an optical disc
            if (GetDriveType(sz_single_drive) == DRIVE_CDROM)
            {
                wchar_t sz_logical_drives[MAX_PATH + 1], sz_fs_name[MAX_PATH + 1] = { 0 };

                LPDWORD lp_max_component = 0, lp_fs_flags = 0;

                driveinfo new_drive;
                new_drive.path = sz_single_drive;

                // Get details about the unit
                if (GetVolumeInformation(sz_single_drive, sz_logical_drives, MAX_PATH + 1, new_drive.serial_number, lp_max_component, lp_fs_flags, sz_fs_name, MAX_PATH + 1))
                {
                    // Save name, file system and serial number
                    new_drive.name = sz_logical_drives;
                    new_drive.file_system = sz_fs_name;

                    std::wostringstream stream;
                    stream << new_drive.serial_number;
                    std::wstring str = stream.str();

                    // Store in list
                    optical_drives.push_back(new_drive);
                }
            }

            sz_single_drive += wcslen(sz_single_drive) + 1;
        }
    }

    return optical_drives;
}

Requirements


Instructions

0 comments

Specify your e-mail if you want to receive notifications about new comments and replies