要确定驱动器是 DVD 还是 CDROM ,您可以使用带有IOCTL_STORAGE_GET_MEDIA_TYPES_EX控制代码的DeviceIoControl函数,然后检查GET_MEDIA_TYPES结构的 DeviceType 字段的值。
试试这个样本
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define wszDrive L"\\\\.\\D:"
int wmain(int argc, wchar_t *argv[])
{
BOOL bResult;
HANDLE hDevice = INVALID_HANDLE_VALUE; // handle to the drive to be examined
hDevice = CreateFileW(wszDrive, // drive to open
GENERIC_READ,
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
UCHAR lpOutBuffer[2048];
DWORD nOutBufferSize = sizeof(lpOutBuffer);
ULONG lpBytesReturned;
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_STORAGE_GET_MEDIA_TYPES_EX, // operation to perform
NULL, 0, // no input buffer
&lpOutBuffer, nOutBufferSize, &lpBytesReturned,
NULL);
CloseHandle(hDevice);
PGET_MEDIA_TYPES pMediaTypes = (PGET_MEDIA_TYPES) lpOutBuffer;
if (bResult)
{
if (pMediaTypes->DeviceType==FILE_DEVICE_DVD)
{
wprintf(L"DVD\n");
}
else
if (pMediaTypes->DeviceType==FILE_DEVICE_CD_ROM)
{
wprintf(L"CDROM\n");
}
}
else
{
wprintf (L"Failed. Error %ld.\n", GetLastError ());
}
cin.get();
return ((int)bResult);
}