HDD serial number flipped every 2 bytes in Windows XP, Vista and 7 but not in Windows 8

后端 未结 5 1464
一生所求
一生所求 2021-02-19 05:25

I need to get HDD serial number to use it as a key for licensing a software. I used diskid32 code in this url: http://www.winsim.com/d

相关标签:
5条回答
  • 2021-02-19 05:46

    Basically you're relying on data that's not strictly reliable to begin with. Drives may change over the lifetime of a computer; all the while getting exactly the right serial number isn't even important to your product.

    One easy hack I can think of is to normalize the serial number, e.g. by sorting the digits; this will make more drives look alike, but I doubt it will become an issue.

    Another way of looking at the problem is that the application should provision for serial number changes; the user could be informed about the licensing issue (for whatever reasons) and asked to contact the support department with a generated code (not necessarily the serial number itself). Given this code, support can then create a new license for the customer.

    0 讨论(0)
  • 2021-02-19 05:53

    Just turn off the flip using the "flip" flag of the flibAndCodeBytes function when windows 8 or greater.

    bool shoulFlipBytes = true;
    if(IsWin8OrLater()) shouldFlipBytes = false;
    
    flipAndCodeBytes(buffer,
                     descrip->SerialNumberOffset,
                     shouldFlipBytes,
                     serialNumber);
    

    You can use this to check for windows version:

    #include <windows.h>
    
    bool IsWin8OrLater() {
        DWORD version = GetVersion();
        DWORD major = (DWORD) (LOBYTE(LOWORD(version)));
        DWORD minor = (DWORD) (HIBYTE(LOWORD(version)));
    
        return (major > 6) || ((major == 6) && (minor >= 2));
    }
    

    According to http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx (Thanks to ChrisV Determine if O/S is Windows 7)

    0 讨论(0)
  • 2021-02-19 06:00

    I use the same approach (and same code) in my software licensing. Yes, Windows 8 for some reason is returning flipped values for this method, I can't say why (so I can't answer your question).

    My solution is the one that you pointed out: Flip the values again. So, after calling the "flipAndCodeBytes", you could test if is a Windows 8 OS, and flip the values.

    In my case, it's working now (I got the same values for Windows XP/Vista/7 and Windows 8).

    Good luck!

    0 讨论(0)
  • 2021-02-19 06:04

    Seem you have to check for windows version through API and add if() branch to your code if version is high enough

    My devs found other problem with this method - that kind of IOCtl code relies on program is being run with administrator rights on Win7\Win8+. In cause of our software, it's service that is run with system rights and clients software that is strictly run with user rights

    0 讨论(0)
  • 2021-02-19 06:04

    For licensing-checking purposes, you really don't care. All you need to know is that some configurations result in flipping, some don't, and it can change during the lifetime of a license.

    So accept both variants:

    string serial = get_serial();
    if (license_check(serial)) {
        licensed = true;
        return;
    }
    serial = swap_bytes(serial);
    if (license_check(serial)) {
        licensed = true;
        return;
    }
    

    (I see Raymond suggested this in a comment)

    No fragile OS check, no worries about whether it failed to flip right when the user applied for the license. Just happy users.

    0 讨论(0)
提交回复
热议问题