Windows获取磁盘信息
2016-05-27 14:36
344 查看
Windows下磁盘相关操作:
1. 使用FindFirstVolume, FindNextVolume查询所有磁盘;
2. QueryDosDevice获取设备名称;
3. GetVolumePathNamesForVolumeName获取路径信息,如C:, D:;
4. GetDriveType获取磁盘类型,不同返回值代表的类型如下图:
![](https://img-blog.csdn.net/20160527143930489?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
void GetVolumePaths(
__in PWCHAR VolumeName,
__out PWCHAR VolumaPath
)
{
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 = GetVolumePathNamesForVolumeName(
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)
{
memcpy_s(VolumaPath, MAX_PATH, NameIdx, MAX_PATH);
//wprintf(L" %s", NameIdx);
}
//wprintf(L"\n");
}
if (Names != NULL)
{
delete[] Names;
Names = NULL;
}
return;
}
void EmurateVolume()
{
HANDLE FindHandle = INVALID_HANDLE_VALUE;
DWORD CharCount = 0;
WCHAR DeviceName[MAX_PATH] = L"";
WCHAR VolumeName[MAX_PATH] = L"";
WCHAR VolumePath[MAX_PATH] = L"";
UINT VolumeType = 0;
DWORD Error = ERROR_SUCCESS;
BOOL NextVolume = FALSE;
size_t Index = 0;
BOOL Success = FALSE;
FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
if (FindHandle == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
wprintf(L"FindFirstVolumeW failed with error code %d\n", Error);
return;
}
while (TRUE)
{
//
// 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 does not 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"Found a device: %s\n", DeviceName);
wprintf(L"Volume name: %s\n", VolumeName);
GetVolumePaths(VolumeName, VolumePath);
wprintf(L"Volume path: %s\n", VolumePath);
VolumeType = GetDriveTypeW(VolumePath);
wprintf(L"Volume type: %d\n", VolumeType);
wprintf(L"\n");
//
// 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;
}
}
}
int main()
{
EmurateVolume();
return 0;
}
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/cc542456%28v=vs.85%29.aspx
1. 使用FindFirstVolume, FindNextVolume查询所有磁盘;
2. QueryDosDevice获取设备名称;
3. GetVolumePathNamesForVolumeName获取路径信息,如C:, D:;
4. GetDriveType获取磁盘类型,不同返回值代表的类型如下图:
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
void GetVolumePaths(
__in PWCHAR VolumeName,
__out PWCHAR VolumaPath
)
{
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 = GetVolumePathNamesForVolumeName(
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)
{
memcpy_s(VolumaPath, MAX_PATH, NameIdx, MAX_PATH);
//wprintf(L" %s", NameIdx);
}
//wprintf(L"\n");
}
if (Names != NULL)
{
delete[] Names;
Names = NULL;
}
return;
}
void EmurateVolume()
{
HANDLE FindHandle = INVALID_HANDLE_VALUE;
DWORD CharCount = 0;
WCHAR DeviceName[MAX_PATH] = L"";
WCHAR VolumeName[MAX_PATH] = L"";
WCHAR VolumePath[MAX_PATH] = L"";
UINT VolumeType = 0;
DWORD Error = ERROR_SUCCESS;
BOOL NextVolume = FALSE;
size_t Index = 0;
BOOL Success = FALSE;
FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
if (FindHandle == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
wprintf(L"FindFirstVolumeW failed with error code %d\n", Error);
return;
}
while (TRUE)
{
//
// 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 does not 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"Found a device: %s\n", DeviceName);
wprintf(L"Volume name: %s\n", VolumeName);
GetVolumePaths(VolumeName, VolumePath);
wprintf(L"Volume path: %s\n", VolumePath);
VolumeType = GetDriveTypeW(VolumePath);
wprintf(L"Volume type: %d\n", VolumeType);
wprintf(L"\n");
//
// 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;
}
}
}
int main()
{
EmurateVolume();
return 0;
}
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/cc542456%28v=vs.85%29.aspx
相关文章推荐
- flex布局语法
- flume 拦截器
- TCP/IP——Internet 协议(IP)功能
- Android 5.0+ 解析(一)RecyclerView
- jequry_rotate.js用来写旋转类的东西的插件(如:抽奖转盘)
- Redis配置和常用命令
- Microsoft Azure系列之八 实现Azure虚拟机的高可用
- 如何在matlab里安装libsvm包
- SpringMVC登录实例
- AUTOLEARNSKILLS.LUA --升级自动学习技能
- 在 PowerPC 下安装 K8S
- Android PackageManagerService分析二:安装APK
- Android如何实现压缩和解压缩文件
- 折半插入排序
- ASP.NET MVC
- 虹膜灰度极坐标直方图
- NRPE: Unable to read output
- 解决UMEditor与bootstrap不兼容导致图片不能正常拖拽缩放的问题
- 15. 3Sum
- 淘宝的一些性能监控工具试用