列出系统中安装的光盘驱动器的步骤
/** enum-cdrom.c - 列出系统中的光盘驱动器
*
* 作者: xstring@csdn
* 日期: 2004.04.16
*
* 说明:
* 使用GetLogicalDrives也可以列出系统中的光驱, 但无法知道其接口
* 性质; 而且无法列出没有分配盘符的光驱。
* 如果你要得到每个光驱的更多硬件信息, 请自己查看程序中所涉及到
* 的注册表位置。
*/
#define _WIN32_WINNT 0x400
#define STRICT
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <windows.h>
#pragma comment (lib, "advapi32.lib")
#pragma comment (lib, "user32.lib")
void print_msg (LPCTSTR api, DWORD error)
{
LPTSTR str = NULL;
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0, (LPTSTR)&str, 0, 0);
_tprintf (TEXT ("%s 调用失败,错误代码: %u/n %s/n"),
api, error, str);
LocalFree (str);
};
#define CHECK_ERROR(apiname) /
if (error != ERROR_SUCCESS) /
{ /
api = TEXT (apiname); /
goto failed; /
}
int main (void)
{
HKEY keyServ = NULL;
HKEY keyEnum = NULL;
HKEY key = NULL;
DWORD error = 0;
LPCTSTR api = NULL;
DWORD type, count, size, i;
setlocale (LC_ALL, ".ACP");
error = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
TEXT ("SYSTEM//CurrentControlSet//Services//Cdrom//Enum"),
0, KEY_READ, &keyServ);
CHECK_ERROR ("RegOpenKeyEx");
error = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
TEXT ("SYSTEM//CurrentControlSet//Enum"),
0, KEY_READ, &keyEnum);
CHECK_ERROR ("RegOpenKeyEx");
size = sizeof (count);
error = RegQueryValueEx (keyServ, TEXT ("Count"),
0, &type, (LPBYTE)&count, &size);
CHECK_ERROR ("RegQueryValueEx");
_tprintf (TEXT ("您的系统上共有 %u 个光盘驱动器: /n/n"), count);
for (i=0;i<count;++i)
{
TCHAR sz [0x200];
TCHAR name [0x40];
static TCHAR dtname [] [16] =
{ TEXT ("USB"), TEXT("IDE"), TEXT ("SCSI"), TEXT ("Unknown") };
int dt;
wsprintf (name, TEXT ("%d"), i);
size = sizeof (sz);
error = RegQueryValueEx (keyServ, name,
0, &type, (LPBYTE)sz, &size);
CHECK_ERROR ("RegQueryValueEx");
error = RegOpenKeyEx (keyEnum, sz,
0, KEY_READ, &key);
CHECK_ERROR ("RegOpenKeyEx");
for (dt=0;dt<3;++dt)
{
if (_tcsncmp (sz, dtname [dt], _tcslen(dtname [dt])) == 0)
break;
}
size = sizeof (sz);
error = RegQueryValueEx (key, TEXT ("FriendlyName"),
0, &type, (LPBYTE)sz, &size);
CHECK_ERROR ("RegQueryValueEx");
_tprintf (TEXT (" CDROM %d - %s [%s]/n"),
i, sz, dtname [dt]);
RegCloseKey (key);
key = NULL;
};
RegCloseKey (keyEnum);
RegCloseKey (keyServ);
return 0;
failed:
if (keyServ)
RegCloseKey (keyServ);
if (keyEnum)
RegCloseKey (keyEnum);
if (key)
RegCloseKey (key);
print_msg (api, error);
return error;
};
本文地址:http://www.45fan.com/a/question/70208.html