How to get a CD-ROM drive letter with programming?

xiaoxiao2021-03-06  36

First, a machine may have more than one CD-ROM drive. Now the CD-ROM is already a standard configuration of the PC, and it is also a very common thing to load an erasable optical disk drive, even DVDs. How many different drives are installed regardless of a machine, how do you find them?

The functions of the drive include getLogicalDrives, getLogicalDriveStrings and getDriveType. The first two are used to get logical drive characters, getLogicalDriveStrings returns to path name strings, such as:

"A: /

c: /

F: /

"

Every path name is separated by NULL (empty or zero) character, and the last end is two empty characters - this is a standard C style handling method. For assembly language users who like operational bits and bytes, getLogicalDrives is a very good API function. It returns a logical drive in a block. That is, in a DWORD type return value, bit 0 (minimum one bit) represents the drive A, bit 1 represents the driver B, and so on. If the state of each bit is ON, the corresponding logical drive exists; otherwise the state is OFF, indicating that the corresponding logical drive does not exist. Everyone knows DWORD is a 32-bit value, which is enough to include all English letters, that is, there are up to 26 disks. To determine the type of a logical drive, you must call the getDriveType function. It uses path names as parameters (such as C: /), returns Drive_Fixed, Drive_removable, or Drive_unkNown. All of the values ​​that may be returned: These values ​​are defined in WinBase.h

#define drive_unknown 0 // Invalid path name

#define drive_no_root_dir 1 // Invalid Road, if you can't find the volume

#define drive_removable 2 // Removable drive (such as disk drive, optical drive, etc.)

#define drive_fixed 3 // fixed drive (such as the usual hard disk)

#define drive_remote 4 // Network Drive

#define drive_cdrom 5 // CD-ROM

#define drive_ramdisk 6 // Random Access (RAM) Disk

In order to easier to explain the problem, I wrote a small program -ListDrive, which lists all the logical drives on a machine. In fact, the current code is as follows:

Listdrives.cpp

#include "stdafx.h"

#include "resource.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

USING NAMESPACE std; // for string class

// below is a getDriveType return code with a mini comparison table of people readable strings

//

Struct {

UINT TYPE; // getDriveType Return Code Type

LPCSTR Name; // ASCII name

DriveTypeflags [] = {

{Drive_Unknown, "unknown"},

{Drive_no_root_dir, "invalid road"}, {drive_removable, "movable"},

{Drive_fixed, "fixed"},

{Drive_Remote, "Network Drive",

{Drive_CDROM, "CD-ROM"},

{Drive_ramdisk, "Random Access Disk"},

{0, NULL},

}

INT_Tmain (int Argc, tchar * argv [], tchar * envp [])

{

IF (! AFXWININIT (:: getModuleHandle (Null), NULL, :: getcommandline (), 0)) {

CERR << _T ("Fatal Error: MFC Initialization Faled") << ENDL;

Return -1;

}

// Get logical drive strings - A: / B: / C: / ..., etc..

/ / You can also use getLogicalDrives to replace strings in a bitmap form to get information.

TCHAR BUF [100];

DWORD LEN = getLogicalDriveStrings (Sizeof (BUF) / Sizeof (Tchar), BUF)

// Show information for each drive

//

String msg = "Logical Drives: / N"; // STL STRING

For (TCHAR * S = BUF; * S; s = _ tcslen (s) 1) {

LPCTSTR SDRIVEPATH = S;

MSG = SDRIVEPATH;

Msg = "";

// GetDriveType Get an enumeration value, such as Drive_unkNown, etc.

//

UINT UDriveType = GetDriveType (SDRIVEPATH);

// Find the drive type. Here I used the table (structural array) to find processing, too cumbersome,

/ / But since the value of UDriveType is continuous.

// I can use DriveTypeflags [UDriveType] instead of linear lookup. Can usually do this in actual programming:

// if (UDriveType & device_cdrom) {

......

//}

//

For (int i = 0; driveTypeflags [i] .name; i ) {

IF (UDriveType == DriveTypeflags [i] .type) {

Msg = drivetypeflags [i] .name;

Break;

}

}

Msg = '' '' '' '' '' '' '' '' '' '' '';

}

COUT << msg.c_str ();

Return 0;

} The program code is very simple, it is an MFC program. Get the root path name of all logical drives with GetLogicalDriveStrings, then call GetDriveType to determine the type of each drive. If you are looking for a CD-ROM, check UDriveType = Drive_CDROM is ok.

转载请注明原文地址:https://www.9cbs.com/read-63421.html

New Post(0)