用C++节制DVD/CD驱动器的开关
在Windows资源欣赏器中,可在DVD/CD光驱图标上单击鼠标右键,选择"弹出"来打开光驱仓门,你大概也发明白,菜单中并没有"封锁"呼吁来封锁光驱。下面,就让我们用措施来节制打开、封锁光驱。
措施的主要事情部门为CD_OpenClose(BOOL bOpen, TCHAR cDrive)函数:
//cDrive是光驱盘符,可能0x01为默认驱动器。
//譬喻:
//CD_OpenCloseDrive(TRUE, 'G'); //打开光驱G:
//CD_OpenCloseDrive(FALSE, 'G'); //封锁光驱G:
//CD_OpenCloseDrive(TRUE, 1); //打开第一个逻辑光驱
void CD_OpenCloseDrive(BOOL bOpenDrive, TCHAR cDrive)
{
MCI_OPEN_PARMS op;
MCI_STATUS_PARMS st;
DWORD flags;
TCHAR szDriveName[4];
strcpy(szDriveName, "X:");
::ZeroMemory(&op, sizeof(MCI_OPEN_PARMS));
op.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
if(cDrive > 1)
{
szDriveName[0] = cDrive;
op.lpstrElementName = szDriveName;
flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT| MCI_OPEN_SHAREABLE;
}
else flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE;
if (!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&op))
{
st.dwItem = MCI_STATUS_READY;
if(bOpenDrive)
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
else
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
}
}
为了利便对多个光驱举办操纵,添加了下面这个函数,其会挪用上面的CD_OpenCloseDrive()函数:
void CD_OpenCloseAllDrives(BOOL bOpenDrives)
{
//鉴定所有光驱,并逐个打开或封锁。
int nPos = 0;
UINT nCount = 0;
TCHAR szDrive[4];
strcpy(szDrive, "?:\\");
DWORD dwDriveList = ::GetLogicalDrives ();
while (dwDriveList) {
if (dwDriveList & 1)
{
szDrive[0] = 0x41 + nPos;
if(::GetDriveType(szDrive) == DRIVE_CDROM)
CD_OpenCloseDrive(bOpenDrives, szDrive[0]);
}
dwDriveList >>= 1;
nPos++;
}
}
最后一点,别忘了在措施开头包罗Mmsystem.h头文件,及在链接选项里写上Winmm.lib。