简体   繁体   English

如何获得卷序列号

[英]how to get volume serial number

I am trying to get volume serial number with winapi in c++ 我试图用c ++中的winapi获取卷序列号

I have the following code: 我有以下代码:

DWORD VolumeSerialNumber=0; 
GetVolumeInformation(L"c:\\", NULL, NULL, &VolumeSerialNumber, NULL, NULL, NULL, NULL);

it works fine and returns VolumeSerialNumber=571477456 but in cmd , when i use dir I get: 它工作正常并返回VolumeSerialNumber=571477456但在cmd ,当我使用dir我得到:

C:\Users\User>dir
 Volume in drive C is Windows
 Volume Serial Number is 2210-0DD0

how do i convert 571477456 to 2210-0DD0 ? 我怎么转换571477456到2210-0DD0?

You just need to print the value in hex instead of decimal, using the %X format specifier: 您只需要使用%X格式说明符以十六进制而不是十进制打印值:

printf("VolumeSerialNumber: 0x%X\n", VolumeSerialNumber);

Which will output: 哪个会输出:

0x22100dd0

If you really require the exact same output, you can separate the DWORD into its lower and upper WORDS using the LOWORD and HIWORD macros: 如果你真的需要完全相同的输出,你可以使用LOWORDHIWORD宏将DWORD分成它的低位和高位WORDS

printf("Volume Serial Number is %04X-%04X\n",
    HIWORD(VolumeSerialNumber),
    LOWORD(VolumeSerialNumber));

Which will output: 哪个会输出:

Volume Serial Number is 2210-0DD0

in order to convert int to hex-string, i found this solution: 为了将int转换为十六进制字符串,我找到了这个解决方案:

char Hex_output [500];
itoa (VolumeSerialNumber ,Hex_output,16);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM