SCSI指令使用之弹带和加载磁带指令使用(实例演示)

SCSI指令:小型计算机系统接口(英语:Small Computer System Interface; 简写:SCSI),一种用于计算机和智能设备之间(硬盘、软驱、光驱、打印机、扫描仪等)系统级接口的独立处理器标准。 SCSI是一种智能的通用接口标准。

这里演示两个命令分别为:
弹出磁带IOCTL_STORAGE_EJECT_MEDIA 和 加载磁带IOCTL_STORAGE_LOAD_MEDIA命令。演示如下:

//弹带指令演示  IOCTL_STORAGE_EJECT_MEDIA
void CtestDlg::OnBnClickedEject()
{
    CString strMsg;
    CString m_strTapeDev = _T("\\\\.\\tape0");//这里指的是驱动器设备
    DWORD accessMode = 0, shareMode = 0;
    //使用CreateFile打开设备,获取句柄信息
    shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;  // default
    accessMode = GENERIC_WRITE | GENERIC_READ;       // default
    HANDLE m_devHandle =   CreateFile(m_strTapeDev,
                            accessMode,
                            shareMode,
                            NULL,
                            OPEN_EXISTING,
                            0,
                            NULL);

    if (m_devHandle == INVALID_HANDLE_VALUE) 
    {
        int err= GetLastError();
        strMsg.Format("Error opening %s. error = %d", m_strTapeDev, err);
        OutPutDebugString(strMsg);
        return;
    }
    else
    {
        strMsg.Format("Device \"%s\" Open OK!",m_strTapeDev);
        OutPutDebugString(strMsg);
    }

    BOOL status = 0;
    DWORD dwRet;
    if(m_devHandle == INVALID_HANDLE_VALUE || m_devHandle == NULL)
    {
        return;
    }
    status = DeviceIoControl(m_devHandle,
        IOCTL_STORAGE_EJECT_MEDIA,//弹带指令
        NULL,
        0,
        NULL,
        0,
        &dwRet,
        FALSE); 
    if(!status)
    {
        int err = GetLastError();//获取最后一条失败信息
        strMsg.Format( "Eject Media Error ; error = %d", syserr);
        OutPutDebugString(strMsg);//打印日志信息
    }
    else
    {
        strMsg.Format( "Eject Media OK!");
        OutPutDebugString(strMsg);
    }
    CloseHandle(m_devHandle );
    m_devHandle = NULL;
}
//加载磁带演示IOCTL_STORAGE_LOAD_MEDIA
void CtestDlg::OnBnClickedLoad()
{
    CString strMsg;
    CString m_strTapeDev = _T("\\\\.\\tape0");//这里指的是驱动器设备
    DWORD accessMode = 0, shareMode = 0;
    //使用CreateFile打开设备,获取句柄信息
    shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;  // default
    accessMode = GENERIC_WRITE | GENERIC_READ;       // default
    HANDLE m_devHandle =   CreateFile(m_strTapeDev,
                            accessMode,
                            shareMode,
                            NULL,
                            OPEN_EXISTING,
                            0,
                            NULL);

    if (m_devHandle == INVALID_HANDLE_VALUE) 
    {
        int err= GetLastError();
        strMsg.Format("Error opening %s. error = %d", m_strTapeDev, err);
        OutPutDebugString(strMsg);
        return;
    }
    else
    {
        strMsg.Format("Device \"%s\" Open OK!",m_strTapeDev);
        OutPutDebugString(strMsg);
    }
    BOOL status = 0;
    DWORD dwRet;
    if(m_devHandle == INVALID_HANDLE_VALUE || m_devHandle == NULL)
    {
        return;
    }
    status = DeviceIoControl(m_devHandle,
        IOCTL_STORAGE_LOAD_MEDIA,//加载磁带指令
        NULL,
        0,
        NULL,
        0,
        &dwRet,
        FALSE); 

    if(!status)
    {
        int syserr = GetLastError();
        strMsg.Format( "Load Media Error ; error = %d", syserr);
        OutPutDebugString(strMsg);
    }
    else
    {
        strMsg.Format( "Load Media OK!");
        OutPutDebugString(strMsg);
    }   
    CloseHandle(m_devHandle );
    m_devHandle = NULL;
}

以下是我的个人公众号,主要作为C/C++语言技术分享使用,该公众号里干货满满,如果您有对此博文的疑问或者java方面的问题也可以添加公众号交流讨论。最后,再次希望您能添加关注,互相交流互相学习共同进步:
SCSI指令使用之弹带和加载磁带指令使用(实例演示)_第1张图片

MSDN函数DeviceIOControl地址:
https://msdn.microsoft.com/en-us/library/aa363216
MSDN函数CreateFile地址:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

你可能感兴趣的:(c++)