c语言 查看磁盘信息,Windows获取磁盘信息

Windows下磁盘相关操作:

1. 使用FindFirstVolume, FindNextVolume查询所有磁盘;

2. QueryDosDevice获取设备名称;

3. GetVolumePathNamesForVolumeName获取路径信息,如C:, D:;

4. GetDriveType获取磁盘类型,不同返回值代表的类型如下图:

ada03a5fb5e68a3920b703468e159650.png

#include

#include

#include

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值