<b>用C++掌握DVD/CD驱动器的开关</b>[VC/C++编程]
本文“<b>用C++掌握DVD/CD驱动器的开关</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在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.
以上是“<b>用C++掌握DVD/CD驱动器的开关</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |