获取指定类型的设备,符号链接



头文件:DeviceInterfaceClassGUID.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//keyBoard 
DEFINE_GUID(GUID_CLASS_KEYBOARD, 0x884b96c3, 0x56ef, 0x11d1, 0xbc, 0x8c, 0x00, 0xa0, 0xc9, 0x14, 0x05, 0xdd); 
//Mouse 
DEFINE_GUID(GUID_CLASS_MOUSE, 0x378de44c, 0x56ef, 0x11d1, 0xbc, 0x8c, 0x00, 0xa0, 0xc9, 0x14, 0x05, 0xdd ); 
//Parallel 
DEFINE_GUID(GUID_PARALLEL_DEVICE, 0x97F76EF0, 0xF883, 0x11D0, 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C); 
DEFINE_GUID(GUID_PARCLASS_DEVICE, 0x811FC6A5, 0xF728, 0x11D0, 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1); 
//PCMCIA 
DEFINE_GUID(GUID_PCMCIA_INTERFACE_STANDARD, 0xbed5dadfL, 0x38fb, 0x11d1, 0x94, 0x62, 0x00, 0xc0, 0x4f, 0xb9, 0x60, 0xee); 
//SCSI 
DEFINE_GUID(ScsiRawInterfaceGuid, 0x53f56309L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(WmiScsiAddressGuid, 0x53f5630fL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
//Serial 
DEFINE_GUID(GUID_CLASS_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73); 
DEFINE_GUID(GUID_SERENUM_BUS_ENUMERATOR, 0x4D36E978L, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18); 
//Storage 
DEFINE_GUID(DiskClassGuid, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(CdRomClassGuid, 0x53f56308L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(PartitionClassGuid, 0x53f5630aL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(TapeClassGuid, 0x53f5630bL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(WriteOnceDiskClassGuid, 0x53f5630cL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(VolumeClassGuid, 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(MediumChangerClassGuid, 0x53f56310L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(FloppyClassGuid, 0x53f56311L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(CdChangerClassGuid, 0x53f56312L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); 
DEFINE_GUID(StoragePortClassGuid, 0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);

程序代码:
//设备类型判断,获取当前计算机所有的光盘驱动器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//by obaby tested on 2010-04-27
#include "windows.h"
#include "tchar.h"
#include "stdio.h"
#include "DeviceInterfaceClassGUID.h"
#ifdef __cplusplus
extern "C"
{
#endif
#include "winioctl.h"
#include "initguid.h"
#include "setupapi.h"
#ifdef __cplusplus
}
#endif
 
#pragma comment(lib,"setupapi.lib")
#define INTERFACE_DETAIL_SIZE    (1024)
 
int GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath);
 
int _tmain(int argc, _TCHAR* argv[])
{
	GUID tmpGuid;
	LPTSTR lpPath[10];
	for (int i=0 ;i <10;i++)
		lpPath[i]=new TCHAR[100];
	char c;
	int cdcount;
	memcpy(&tmpGuid,&CdRomClassGuid,sizeof(CdRomClassGuid));
	cdcount = GetDevicePath(&tmpGuid,lpPath);
	_tprintf(_T("All Cdroms on this computer:\n")); //打印本机的所有cd驱动器
	_tprintf(_T("============================================\n"));
	for (int j =0 ;j <cdcount ;j++)
	{
	_tprintf(_T("%s\n"),lpPath[j]);
	}
	_tprintf(_T("Cdrom Numbers:%d\n"),cdcount);		//本机cd驱动器数量
 
	HANDLE hDeviceHandle = CreateFile(lpPath[0],		
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	if (hDeviceHandle != INVALID_HANDLE_VALUE)
	{
		printf("Create file success. \n");
		DWORD bytes;
		scanf("%c",&c);
		DeviceIoControl(
			(HANDLE) hDeviceHandle,            // handle to device
			IOCTL_STORAGE_EJECT_MEDIA,   // dwIoControlCode
			NULL,                        // lpInBuffer
			0,                           // nInBufferSize
			NULL,                        // lpOutBuffer
			0,                           // nOutBufferSize
			&bytes,   // number of bytes returned
			NULL  // OVERLAPPED structure
			);
	}
 
	scanf("%c",&c);
 
	return 0;
}
 
int GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
{
	HDEVINFO hDevInfoSet;    //设备信息集句柄;
	SP_DEVICE_INTERFACE_DATA ifdata;
	PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
	int nCount;
	BOOL bResult;
 
	// 取得一个该GUID相关的设备信息集句柄
	hDevInfoSet = ::SetupDiGetClassDevs(lpGuid,     // class GUID 
		NULL,                    // 无关键字
		NULL,                    // 不指定父窗口句柄
		DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);    // 目前存在的设备
 
	// 失败...
	if (hDevInfoSet == INVALID_HANDLE_VALUE)
	{
		return 0;
	}
 
	// 申请设备接口数据空间
	pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
 
	pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
 
	nCount = 0;
	bResult = TRUE;
 
	// 设备序号=0,1,2... 逐一测试设备接口,到失败为止
	while (bResult)
	{
		ifdata.cbSize = sizeof(ifdata);
 
		// 枚举符合该GUID的设备接口
		bResult = ::SetupDiEnumDeviceInterfaces(
			hDevInfoSet,     // 设备信息集句柄
			NULL,            // 不需额外的设备描述
			lpGuid,          // GUID
			(ULONG)nCount,   // 设备信息集里的设备序号
			&ifdata);        // 设备接口信息
 
		if (bResult)
		{
			// 取得该设备接口的细节(设备路径)
			bResult = SetupDiGetInterfaceDeviceDetail(
				hDevInfoSet,    // 设备信息集句柄
				&ifdata,        // 设备接口信息
				pDetail,        // 设备接口细节(设备路径)
				INTERFACE_DETAIL_SIZE,   // 输出缓冲区大小
				NULL,           // 不需计算输出缓冲区大小(直接用设定值)
				NULL);          // 不需额外的设备描述
			if (bResult)
			{
				// 复制设备路径到输出缓冲区
				::_tcscpy(pszDevicePath[nCount], pDetail->DevicePath);
				// 调整计数值
				nCount++;
			}
		}
	}
	// 释放设备接口数据空间
	::GlobalFree(pDetail);
	// 关闭设备信息集句柄
	::SetupDiDestroyDeviceInfoList(hDevInfoSet);
	return nCount;
}</cdcount>

原创文章,转载请注明: 转载自 obaby@mars

本文标题: 《获取指定类型的设备,符号链接》

本文链接地址: http://www.h4ck.org.cn/2010/04/get-device-type/

You may also like

发表评论

电子邮件地址不会被公开。 必填项已用*标注