Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get volume serial number

Tags:

c++

c

winapi

I am trying to get volume serial number with winapi in c++

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:

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

how do i convert 571477456 to 2210-0DD0 ?

like image 275
user1438233 Avatar asked May 21 '14 07:05

user1438233


1 Answers

You just need to print the value in hex instead of decimal, using the %X format specifier:

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:

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

Which will output:

Volume Serial Number is 2210-0DD0
like image 200
Jonathon Reinhart Avatar answered Oct 18 '22 03:10

Jonathon Reinhart