多光驱控制的简单功能的简单封装。

博客提供了光驱控制的代码,可实现打开和关闭光驱的功能。代码中定义了 CDiskControl 类,包含判断指定盘符是否为光驱、打开和关闭光驱等方法,还给出了不同情况下的使用示例,适用于单光驱和多光驱场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提供了打开和关闭光驱,如果就一个光驱自然好控制,但是多个的话......我查了点资料参考着,盗窃了一个.希望对大家有用.欢迎指导 不多说,贴代码. 使用如下.

CDiskControl gCDRom;
void CMyDlg::OnBnClickedButton3()
{
    // TODO: 在此添加控件通知处理程序代码
    if(gCDRom.CDRomExists('j'))
    {
        MessageBox("是光驱");
    }
    else
    {
        MessageBox("不是光驱");
    }
}

void CMyDlg::OnBnClickedButton4()
{
    // TODO: 在此添加控件通知处理程序代码
    if(!gCDRom.OpenCDRom('j'))
    {
        MessageBox("open光驱");
    }
    else
    {
        MessageBox("failed open 光驱");
    }
}

void CMyDlg::OnBnClickedButton5()
{
    // TODO: 在此添加控件通知处理程序代码
    if(!gCDRom.CloseCDRom('j'))
    {
        MessageBox("closed光驱");
    }
    else
    {
        MessageBox("failed closed 光驱");
    }
}

/********************************************************************
 created: 2005/08/19
 created: 19:8:2005   16:27
 filename:  CDiskControl.h
 author:  hh
 
 purpose:    也就是获得光盘的一点信息,然后控制它开关的
*********************************************************************/
#ifndef _CDISKCONTROL_H_
#define _CDISKCONTROL_H_

#define CLOSEDCDROM  "Set cdaudio door closed wait"
#define OPENCDROM    "Set cdaudio door open wait"

#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm")

class CDiskControl
{
public:
    CDiskControl(){};
    ~CDiskControl(){};

    /** 如果你就一个光驱就不用输入它的盘符了 成功返回0,否则返回其他值 */
    int OpenCDRom(const char chDisk = 0);

    /** 如果你就一个光驱就不用输入它的盘符了 成功返回0,否则返回其他值*/
    int CloseCDRom(const char chDisk = 0);

    /** 判断指定的盘符是否是光驱 是返回真,否则返回假 */
    BOOL CDRomExists(const char chDisk ) ;

protected:
    /** 针对单光驱用户的 */
    MCIERROR SendMciString(const char* szCommand,
        LPTSTR lpszReturnString = NULL,
        UINT cchReturn = 0,
        HWND hwndCallback = NULL)const;

    /** 把字符整理为F:形式的字符串 */
    const char* FormatDriver(const char chDisk);

    /** 成功返回0,否则返回其他值 */
    MCIERROR  ExecuteCommand(const char chDisk, DWORD dwCommand); 

private:
    static const int DISKVOLUMELEN = 3;
    char m_CDRomVolume[DISKVOLUMELEN];
};

#endif

/********************************************************************
 created: 2005/08/19
 created: 19:8:2005   16:27
 filename:  CDiskControl.cpp
 author:  hh
 
 purpose: 
*********************************************************************/
#include "CDiskControl.h"


/** 如果你就一个光驱就不用输入它的盘符了 */
int CDiskControl::OpenCDRom(const char chDisk)
{
    int iResult = 0;

    if(chDisk == 0)
    {     
        if(!SendMciString(OPENCDROM))
        {
            iResult = -1;
        }      
    }
    else
    {
        if(ExecuteCommand(chDisk, MCI_SET_DOOR_OPEN ))
        {
            iResult = -2;
        }
    }  

    return iResult;
}

/** 如果你就一个光驱就不用输入它的盘符了 */
int CDiskControl::CloseCDRom(const char chDisk)
{
    int iResult = 0;

    if(chDisk == 0)
    {     
        if(SendMciString(CLOSEDCDROM))
        {
            iResult = -1;
        }      
    }
    else
    {
        if(ExecuteCommand(chDisk, MCI_SET_DOOR_CLOSED ))
        {
            iResult = -2;
        }
    }  

    return iResult;
}

/** 针对单光驱用户的 */
MCIERROR CDiskControl::SendMciString(const char* szCommand,
                                     LPTSTR lpszReturnString,
                                     UINT cchReturn ,
                                     HWND hwndCallback)const
{
    return mciSendString(szCommand,lpszReturnString,cchReturn,hwndCallback);//关闭光驱
}

/** 判断指定的盘符是否是光驱 是返回真,否则返回假 */
BOOL CDiskControl::CDRomExists(const char chDisk )
{
    return (GetDriveType(FormatDriver(chDisk)) == DRIVE_CDROM) ;
}

/** 把字符整理为F:形式的字符串 */
const char* CDiskControl::FormatDriver(const char chDisk)
{
    ZeroMemory(m_CDRomVolume, DISKVOLUMELEN);
    m_CDRomVolume[0] = chDisk;
    m_CDRomVolume[1] = ':';

    return m_CDRomVolume;
}

/** 成功返回0,否则返回其他值 */
MCIERROR  CDiskControl::ExecuteCommand(const char chDisk, DWORD dwCommand)
{
    MCIERROR errResult = 0;

    DWORD dwFlags;
    MCI_OPEN_PARMS mciOpen;       
    ZeroMemory(&mciOpen,sizeof(MCI_OPEN_PARMS));
    mciOpen.lpstrDeviceType=(LPCSTR)MCI_DEVTYPE_CD_AUDIO;
    mciOpen.lpstrElementName = FormatDriver(chDisk);
    dwFlags = MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT|MCI_OPEN_SHAREABLE;
    errResult = mciSendCommand(0,MCI_OPEN,dwFlags,(unsigned long)&mciOpen);

    if(!errResult)
    {
        mciSendCommand(mciOpen.wDeviceID,MCI_SET,dwCommand,0);
        mciSendCommand(mciOpen.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
    }

    return errResult;
}

Disk Controls is set of 22+ components that can make your life much easier if you developing software that works with disks (Hard / Floppy / CD / RAM / Network), shell and file system. The DiskControls pack contains: Two advanced search engines, which will find any file or folder by specified criterias (DiskScanner and MultiDiskScanner components); The FolderListView component which let's you to display filtered contents of any directory and looks and feels like Windows Explorer. Supports big number of features such like automatical sorting of the list items with arrow-style sort mark on the header section, individual context menus for every shell object, possibility to hide or show some list columns, automatic drag'n'drop support via OLE and so on…; The FolderMonitor, which will check your folders for any changes; The VirtualDrives engine, which can define, redefine or delete the virtual drives from the file system; The FileTail utility, which works like tail command in Unix, and can monitor the file for its changes; Several components that provide you with detailed information about any shell objects (files, folders or drives), can retrieve the version information from executable files and dynamic-link libraries; Component which can install or uninstall new file types into the shell (dcFileAssociation); Component which performs the operations with files and folders on shell level (dcFileOperations); Several advanced edit controls and dialogs for browsing and selecting the files and folders; much more... see Components Overview for more details. Compatibility DiskControls compatible with Delphi 3/4/5/6/7 and BCB 3/4/5/6, and has been tested on Win95, Win95OSR2, Win98, WinME, NT4, Win2000 and WinXP. Improvements Currently the DiskControls pack contains 22 components. However, we constantly updating the package and adding more useful utilities and new great features in existing components. If you have any suggestions to help us to improve the DiskControls — please contact us and never hesitate to do it for any question. Updates Registering of the DiskControls entitles you to unlimited support (via email or personal panel at AppControls.Com website), minor updates indefinitely and major version updates for 6 month from date of purchase.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值