c++winapidiskhardware-id

Different disk serial number result from GetVolumeInformation()


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.


Solution

  • These two lines will give you undefined behavior:

    char* disk_serialANSI;
    sprintf(disk_serialANSI, "%d", disk_serialINT);
    

    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 sprintf call will write.


    Since you're programming in C++ there are a couple of solutions.