[Ask questions]
Recently I was developing a program that reads files from CD-ROM, such as video and audio files. Because the situation on each machine is different. How do I know what the CD-ROM drive is?
Troubleshooting
First, a machine may have more than one CD-ROM drive. Now the CD-ROM is the standard configuration of the PC, in addition to install a erasable CD-ROM drive, even DVDs are very common things. No matter how many different drives are installed on a single machine, how do you find them?
The functions for the drive are getlogicaldrives,getlogicaldrivestrings and GetDriveType. The first two are used to get the logical drive letter, GetLogicalDriveStrings returns the path name string, such as:
"A:\<null>c:\<null>f:\<null><null>"
Here each pathname is delimited by null (empty or 0) characters, and the last ending is two null characters-this is the standard C-style approach. GetLogicalDrives is a very useful API function for assembly language users who prefer to manipulate bits and bytes. It returns the logical drive in the form of a bitmask. That is, in the return value of a DWORD type, the bit 0 (the smallest one) represents drive a, bit 1 indicates drive B, and so on. The state of each bit, if on, indicates that the corresponding logical drive exists, otherwise the state is off, indicating that the corresponding logical drive does not exist. You know that a DWORD is a 32-bit value that includes all the letters in English, that is to say, up to 26 characters.
To determine the type of a logical drive, you must call the GetDriveType function. It returns drive_fixed,drive_removable, or Drive_unknown, with a path name as an argument (such as C:\). All possible values are listed below: These values are defined in the Winbase.h
#define DRIVE_UNKNOWN 0 // 无效路径名
#define DRIVE_NO_ROOT_DIR 1 // 无效路经,如无法找到的卷标
#define DRIVE_REMOVABLE 2 // 可移动驱动器(如磁盘驱动器,光驱等)
#define DRIVE_FIXED 3 // 固定的驱动器 (如 通常的硬盘)
#define DRIVE_REMOTE 4 // 网络驱动器
#define DRIVE_CDROM 5 // CD-ROM
#define DRIVE_RAMDISK 6 // 随机存取(RAM) 磁盘
To make it easier to explain the problem, I wrote a small program,--listdrives, which lists all the logical drives on a single machine. The implementation 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
Here is a getdrivetype return code with a human-readable string of miniature tables
//
struct {
UINT type; GetDriveType Return code type
LPCSTR name; ASCII name
} Drivetypeflags [] = {
{Drive_unknown, "unknown"},
{drive_no_root_dir, "Invalid Path"},
{drive_removable, ' removable '},
{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 Failed") << Endl;
return-1;
}
Get logical drive string-a:\b:\c:\ ... Wait.
You can also use getlogicaldrives to get information in the form of a bitmap instead of a string
TCHAR buf[100];
DWORD len = getlogicaldrivestrings (sizeof (BUF)/sizeof (TCHAR), buf);
Display 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 gets enumerated values, such as Drive_unknown.
//
UINT Udrivetype = GetDriveType (Sdrivepath);
Find the drive type. Here I used the table (array of structures) to do the lookup processing, too cumbersome some,
But since the value of the udrivetype is continuous.
I can use Drivetypeflags[udrivetype to replace the linear lookup. This can usually be done 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. Use GetLogicalDriveStrings to get the root path name of all logical drives, and then call GetDriveType to determine the type of each drive. If you are looking for a CD-ROM, check udrivetype = Drive_cdrom.
This article supporting source code