如何枚举windows系统中的所有设备
2010-04-15 11:35
387 查看
对于在linux下,我们可以读取/proc/partitions来获得系统中所有的硬盘和分区的各个设备名,但对于windows下,我们又如何获得系统下的各个设备呢,这里用了windows下的API --- FindFirstVolume、
FindNextVolume
和
FindVolumeClose。
这些API可以枚举windows系统下的各个设备。
以下的程序摘自微软的msdn上的例子程序,用于显示系统中的所有设备和卷的路径名。对于每个卷,例子中的程序将会查找系统中的每个卷,程序将获得卷的设备名和卷的路径名,并显示出了。
下面是程序输出中的一个例子,对于每个卷,输出它的卷设备路径,卷GUID路径和设备字符。
FindNextVolume
和
FindVolumeClose。
这些API可以枚举windows系统下的各个设备。
以下的程序摘自微软的msdn上的例子程序,用于显示系统中的所有设备和卷的路径名。对于每个卷,例子中的程序将会查找系统中的每个卷,程序将获得卷的设备名和卷的路径名,并显示出了。
#include <windows.h> #include <stdio.h> void DisplayVolumePaths( __in PWCHAR VolumeName ) { DWORD CharCount = MAX_PATH + 1; PWCHAR Names = NULL; PWCHAR NameIdx = NULL; BOOL Success = FALSE; for (;;) { // // Allocate a buffer to hold the paths. Names = (PWCHAR) new BYTE [CharCount * sizeof(WCHAR)]; if ( !Names ) { // // If memory can't be allocated, return. return; } // // Obtain all of the paths // for this volume. Success = GetVolumePathNamesForVolumeNameW( VolumeName, Names, CharCount, &CharCount ); if ( Success ) { break; } if ( GetLastError() != ERROR_MORE_DATA ) { break; } // // Try again with the // new suggested size. delete [] Names; Names = NULL; } if ( Success ) { // // Display the various paths. for ( NameIdx = Names; NameIdx[0] != L'/0'; NameIdx += wcslen(NameIdx) + 1 ) { wprintf(L" %s", NameIdx); } wprintf(L"/n"); } if ( Names != NULL ) { delete [] Names; Names = NULL; } return; } void __cdecl wmain(void) { DWORD CharCount = 0; WCHAR DeviceName[MAX_PATH] = L""; DWORD Error = ERROR_SUCCESS; HANDLE FindHandle = INVALID_HANDLE_VALUE; BOOL Found = FALSE; size_t Index = 0; BOOL Success = FALSE; WCHAR VolumeName[MAX_PATH] = L""; // // Enumerate all volumes in the system. FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName)); if (FindHandle == INVALID_HANDLE_VALUE) { Error = GetLastError(); wprintf(L"FindFirstVolumeW failed with error code %d/n", Error); return; } for (;;) { // // Skip the //?/ prefix and remove the trailing backslash. Index = wcslen(VolumeName) - 1; if (VolumeName[0] != L'//' || VolumeName[1] != L'//' || VolumeName[2] != L'?' || VolumeName[3] != L'//' || VolumeName[Index] != L'//') { Error = ERROR_BAD_PATHNAME; wprintf(L"FindFirstVolumeW/FindNextVolumeW returned a bad path: %s/n", VolumeName); break; } // // QueryDosDeviceW doesn't allow a trailing backslash, // so temporarily remove it. VolumeName[Index] = L'/0'; CharCount = QueryDosDeviceW(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName)); VolumeName[Index] = L'//'; if ( CharCount == 0 ) { Error = GetLastError(); wprintf(L"QueryDosDeviceW failed with error code %d/n", Error); break; } wprintf(L"/nFound a device:/n %s", DeviceName); wprintf(L"/nVolume name: %s", VolumeName); wprintf(L"/nPaths:"); DisplayVolumePaths(VolumeName); // // Move on to the next volume. Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName)); if ( !Success ) { Error = GetLastError(); if (Error != ERROR_NO_MORE_FILES) { wprintf(L"FindNextVolumeW failed with error code %d/n", Error); break; } // // Finished iterating // through all the volumes. Error = ERROR_SUCCESS; break; } } FindVolumeClose(FindHandle); FindHandle = INVALID_HANDLE_VALUE; return; }
下面是程序输出中的一个例子,对于每个卷,输出它的卷设备路径,卷GUID路径和设备字符。
Found a device: /Device/HarddiskVolume2 Volume name: //?/Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}/ Paths: C:/ Found a device: /Device/CdRom0 Volume name: //?/Volume{4c1b02c4-d990-11dc-99ae-806e6f6e6963}/ Paths: D:/
相关文章推荐
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
- 应用程序中如何枚举系统所有USB设备
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
- IoEnumerateDeviceObjectList 枚举驱动的所有设备 文件过滤驱动 windows内核开发
- 让OpenCV枚举所有摄像头-附如何建立Windows下的OpenCV工程
- 【API】枚举Windows系统所有进程
- 如何解决Windows 10系统下设备的声音问题
- 【驱动开发】Windows系统下枚举设备的几个函数
- [转]设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识
- 如何利用API获取WINDOWS系统所有用户名和密码(做过的朋友进来看看)
- IoEnumerateDeviceObjectList 枚举驱动的所有设备 文件过滤驱动 windows内核开发
- Windows下如何枚举所有进程
- 【API】枚举Windows系统所有进程
- C++ 实现Windows系统设备枚举
- Windows下如何枚举所有进程
- 1月14日苹果在线商店促销,冲动之下购买了IMAC。以前一直在旧电脑windows下使用itunes,现在有了新的也想借此机会将旧电脑系统重装。于是我需要把现有设备中的所有资料通过itunes全部导
- 1月14日苹果在线商店促销,冲动之下购买了IMAC。以前一直在旧电脑windows下使用itunes,现在有了新的也想借此机会将旧电脑系统重装。于是我需要把现有设备中的所有资料通过itunes全部导
- Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息
- 枚举系统中已经安装的所有硬件设备