DWORD disk_serialINT[MAX_PATH + 1];
GetVolumeInformationA(NULL, NULL, NULL, disk_serialINT, NULL, NULL, NULL, NULL);
char* disk_serialANSI;
sprintf(disk_serialANSI, "%d", disk_serialINT);
std::string HDDserial = disk_serialANSI;
This is my piece of code where I get the hdd serial number, but the problem is that every time when the program executes the value is different. Can someone explain that?
SOLVED:
DWORD disk_serialINT;
GetVolumeInformationA(NULL, NULL, NULL, &disk_serialINT, NULL, NULL, NULL, NULL);
std::string HDDserial = std::to_string(disk_serialINT);
Thanks.
These two lines will give you undefined behavior:
You declare a pointer variable, but you don't actually make it point anywhere. Uninitialized local variables have an indeterminate value (in practice it will be seemingly random), and by using that uninitialized pointer you don't know where the
sprintfcall will write.Since you're programming in C++ there are a couple of solutions.
The old-fashioned is to make
disk_serialANSIan array of characters, big enough to hold the number (including string terminator). An alternative is to manually allocate memory for the pointer, and then free that memory again when you're done with it.Using
std::ostringstreamto format the data and get astd::string.Using
std::to_stringto convert to a string directly.Use Boost Lexical cast.