【 tulaoshi.com - 编程语言 】
在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。