iDiscMaster2::get_Item 方法 (imapi2.h)

检索指定光盘设备的唯一标识符。

语法

HRESULT get_Item(
  [in]  LONG index,
  [out] BSTR *value
);

parameters

[in] index

要检索其唯一标识符的设备从零开始的索引。

当从计算机或跨启动会话添加或删除设备时,在 PNP 活动期间,索引值可能会更改。

[out] value

包含与指定索引关联的光盘设备的唯一标识符的字符串。

返回值

成功后返回S_OK,但实现后可能会返回其他成功代码。 以下错误代码通常在操作失败时返回,但不表示唯一可能的错误值:

返回代码 说明
E_INVALIDARG
一个或多个参数无效。

值:0x80070057

E_POINTER
指针无效。

值:0x80004003

E_OUTOFMEMORY
未能分配所需的内存。

值:0x8007000E

注解

若要枚举所有标识符,请调用 IDiscMaster2::get__NewEnum 方法。

以下示例演示了如何重新枚举光驱,以便准确说明在初始创建 IDiscMaster2 对象后添加或删除的驱动器。 这是通过 IDiscMaster2::get_ItemIDiscMaster2::get_Count 方法实现的:

#include <windows.h>
#include <tchar.h>
#include <imapi2.h>
#include <objbase.h>
#include <stdio.h>

#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "user32.lib")

int __cdecl _tmain(int argc, TCHAR* argv[])   
{   
    BSTR           bstrDeviceName;   
    HRESULT        hr = S_OK;   
    BOOL           bComInitialised;       
    IDiscMaster2*  discMaster;   
    UINT           iCounter = 0; 
    LONG           lValue = 0; 

    bComInitialised = SUCCEEDED(CoInitializeEx(0, COINIT_MULTITHREADED));

    // Create an object of IDiscMaster2  
    if (SUCCEEDED(hr)){
        CoCreateInstance(
            CLSID_MsftDiscMaster2,
            NULL, CLSCTX_ALL,
            IID_PPV_ARGS(&discMaster)
        );   

        if(FAILED(hr)){
            _tprintf(TEXT("\nUnsuccessful in creating an instance of CLSID_MsftDiscMaster2.\n\nError returned: 0x%x\n"), hr);
            return 0;
        }
    }
    //
    // Loop twice and get the optical drives attached to the system,
    // first time just get the current configuration and second time 
    // prompt the user to change the configuration and then get the 
    // altered configuration.   
    //
    do{
        // Get the number of drives 
        if (SUCCEEDED(hr)){
            hr = discMaster->get_Count(&lValue);
            if (SUCCEEDED(hr)){
                _tprintf(TEXT("\nTotal number of drives = %d\n"), lValue);
            }
        }

        // Print all the optical drives attached to the system 
        if (SUCCEEDED(hr)){
            for(LONG iCount = 0; iCount < lValue; iCount++) {
                hr = discMaster->get_Item(iCount, &bstrDeviceName);
                _tprintf(TEXT("\nUnique identifier of the disc device associated with index %d is: %s\n"), iCount, bstrDeviceName);
            }            
        }

        // Prompt the user to unhook or add drives
        if (iCounter < 1){
            MessageBox(NULL,TEXT("Please un-hook or add drives and hit OK"), TEXT("Manual Action"), MB_OK);
            _tprintf(TEXT("\nGetting the altered configuration ... \n"));
        }
        iCounter++;
    }while(iCounter < 2);

    discMaster->Release();
    CoUninitialize();   
    bComInitialised = FALSE;   

    return 0;

要求

   
最低受支持的客户端 Windows Vista、Windows XP SP2 [仅限桌面应用]
最低受支持的服务器 Windows Server 2003 [仅限桌面应用]
目标平台 Windows
标头 imapi2.h

另请参阅

IDiscMaster2

IDiscMaster2::get_Count

IDiscRecorder2::InitializeDiscRecorder