获取外部媒体类型

时间:2014-01-22 21:53:26

标签: c++ windows winapi visual-c++

我想找到一种方法,在Windows上使用C ++中的简单功能检测光驱中的媒体类型(例如DVD + R,DVD-R,DVD-RW,CD + R等)。

该功能不应要求管理员权限。

修改

我实现了以下代码:

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <imapi2.h>
#include <imapi2fs.h>
#include <imapi2error.h>
#include <imapi2fserror.h>

int main(int argc, char *argv[])
{
IDiscFormat2Data*   discFormatData = NULL;
HRESULT hr;

       CoInitialize ( NULL );

hr = CoCreateInstance(  __uuidof(MsftDiscFormat2Data),
    NULL,
    CLSCTX_ALL,
    __uuidof(IDiscFormat2Data),
    (void**)&discFormatData);

if ( SUCCEEDED(hr) )
{        
    IMAPI_MEDIA_PHYSICAL_TYPE mediaType = IMAPI_MEDIA_TYPE_UNKNOWN; 

    hr = discFormatData->get_CurrentPhysicalMediaType(&mediaType);

    if ( SUCCEEDED(hr) )
    {
        std::cout << "MediaPhysicalType: " << mediaType << std::endl;
    }
    else
    {
        std::stringstream str;
        str << "get_CurrentPhysicalMediaType() failed with the error: 0x";

        str << std::hex << hr << ".";

        std::cout << str.str() << std::endl;


    }

    // Release the interface.
    // Tell the COM object that we're done with it.
    discFormatData->Release();
}
else
{
    std::stringstream str;
    str << "CoCreateInstance() failed with the error: 0x" << std::hex << hr;


    std::cout << str.str() << std::endl;
}

cin.get();

return 0;

}

目前我的问题是我收到以下错误:E_IMAPI_RECORDER_REQUIRED这意味着 “该请求要求选择当前的光盘记录器。”

假设我有至少两个光学驱动器,我怎么能区别呢?

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在Windows 2000及更高版本中,您可以使用IOCTL_CDROM_GET_CONFIGURATIONSCSI_GET_CONFIGURATION_REQUEST_TYPE_CURRENT标志来查询光学设备的当前配置文件,这将告诉您哪种类型的光盘(CD,DVD + -R / W,HDDVD,BluRay等已插入,如果有的话。在早期版本中,您必须手动将SCSI MMC命令直接发送到设备以查询相同的信息。