您的位置:首页 > 其它

Windows获取磁盘信息

2016-05-27 14:36 344 查看
Windows下磁盘相关操作:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: