// 1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
#include <setupapi.h>
#include <objbase.h>
#include <initguid.h>
#include <stdlib.h>
//#include <winioctl.h>
#include <tchar.h>
#include <cstdlib>
#include <locale>
using namespace std;
#pragma comment(lib,"setupapi.lib")
// 根据GUID获得设备路径
#define MAX_DEVICE 256
#define MAXPATH 1024
// SetupDiGetInterfaceDeviceDetail所需要的输出长度,定义足够大
#define INTERFACE_DETAIL_SIZE 1024
const GUID DiskClassGuid = {0x65E8773D, 0x8F56, 0x11D0, {0xA3, 0xB9, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96}};
// 根据GUID获得设备路径
// lpGuid: GUID指针
// pszDevicePath: 设备路径指针的指针
// 返回: 成功得到的设备路径个数,可能不止1个
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)
{
// 复制设备路径到输出缓冲区
//LPTSTR chr = pszDevicePath[nCount];
wprintf_s(L"%s\n", pDetail->DevicePath);
//char *ch = new char[MAXPATH];
std::cout<<ch<<std::endl;
//int length = wcstombs( ch, chr, MAXPATH);
//MultiByteToWideChar( CP_ACP, 0, ch, MAXPATH, pDetail->DevicePath, pDetail->DevicePath[0]);
// 调整计数值
nCount++;
}
}
}
// 释放设备接口数据空间
::GlobalFree(pDetail);
// 关闭设备信息集句柄
::SetupDiDestroyDeviceInfoList(hDevInfoSet);
return nCount;
}
//调用GetDevicePath函数时要注意,pszDevicePath是个指向字符串指针的指针,例如可以这样
int main()
{
/*
char *t = new char[16];
t = "this is a string";
std::cout<<t<<std::endl;
LPTSTR c = new WCHAR[16];
MultiByteToWideChar( CP_ACP, 0, t, strlen(t) + 1, c, strlen(t) + 1);
wprintf_s(L"%s\n", c);
std::cout<<sizeof(c)<<std::endl;
char *ch = new char[16];
WideCharToMultiByte( CP_ACP, 0, c, -1, ch, strlen(t) + 1, NULL, NULL );
std::cout << ch << std::endl;
int i;
char *pmbbuf = (char *)malloc( 1024 );
wchar_t *pwchello = L"Hello, world.";
printf( "Convert wide-character string:\n" );
i = wcstombs( pmbbuf, pwchello, 1024 );
printf( "\tCharacters converted: %u\n", i );
printf( "\tMultibyte character: %s\n\n", pmbbuf );*/
LPTSTR pszDevicePath[MAX_DEVICE]; // 设备路径
int nDevice; //定义nDevice的类型
// 分配需要的空间
for (int i = 0; i < MAX_DEVICE; i++)
{
pszDevicePath[i] = new wchar_t[256];
}
// 取设备路径
nDevice = ::GetDevicePath((LPGUID)&DiskClassGuid, pszDevicePath);
cout<<nDevice<<endl;
freopen("G:\\设备路径.txt","w",stdout);//写到G盘目录下的设备路径.txt中
// 逐一获取设备信息
for (int i = 0; i < nDevice; i++)
{
// 打开设备
HANDLE hDevice = ::CreateFile(pszDevicePath[i],GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if (hDevice != INVALID_HANDLE_VALUE)
{
// I/O操作
::CloseHandle(hDevice);
}
}
// 释放空间
for (int i = 0; i < MAX_DEVICE; i++)
{
delete []pszDevicePath[i];
}
system("pause");
return 0;
}
从资源上下载的,半转载状态