简体   繁体   English

如何获取硬盘的序列号

[英]How to get serial number for Hard Drive

如何使用WMI和asm代码以外的C ++程序获取处理器(CPU),SCSI,显示和IDE的唯一编号(序列号/ ID)?

Since you mention WMI, I assume you are working on Windows. 由于您提到WMI,因此我假设您正在Windows上工作。 Lookup GetVolumeInformation() . 查找GetVolumeInformation()

On Windows you can get CPU info from the environment variable *PROCESSOR_** , you can parse the volume serial number from vol , the MAC address from route print 在Windows上,您可以从环境变量* PROCESSOR _ **获取CPU信息,可以从vol解析卷序列号,从路由打印解析MAC地址。

If you want to make it cross-platform (and if this is for software licensing) then an open source platform like Linux raises the problem to a whole new level anyway and requires a different approach. 如果您想使其跨平台(并且这是为了软件许可),那么像Linux这样的开源平台无论如何都会将问题提升到一个全新的水平,并且需要使用不同的方法。 However you can still get a lot of the info by parsing the output from standard tools. 但是,您仍然可以通过分析标准工具的输出来获取很多信息。

You really should consider WMI. 您确实应该考虑WMI。 In the old days, the BIOS would have been helpful but its all been replaced by the HAL. 在过去,BIOS会有所帮助,但全部由HAL取代。

CodeProject is always worth searching in cases like this. 在这种情况下,CodeProject 始终值得搜索。

How To Get Hardware Information 如何获取硬件信息

The below is the code I use to retrieve the hard drive serial for a game, so that cheaters are permanently banned (and they can't get back in without getting a new drive!): 以下是我用来检索游戏硬盘序列的代码,以便永久禁止作弊者(如果没有新的驱动器,他们就不能回来!):

string GetMachineID()
{
    // LPCTSTR szHD = "C:\\";  // ERROR
    string ss;
    ss = "Err_StringIsNull";
    UCHAR szFileSys[255],
        szVolNameBuff[255];
    DWORD dwSerial;
    DWORD dwMFL;
    DWORD dwSysFlags;
    int error = 0;

    bool success = GetVolumeInformation(LPCTSTR("C:\\"), (LPTSTR)szVolNameBuff,
        255, &dwSerial,
        &dwMFL, &dwSysFlags,
        (LPTSTR)szFileSys,
        255);
    if (!success) {
        ss = "Err_Not_Elevated";
    }
    std::stringstream errorStream;
    errorStream << dwSerial;
    return string(errorStream.str().c_str());
}

Although there is a potential bug whereupon if Windows is installed onto a drive other than C:\\ , this is an easy fix. 虽然如果安装在比其它驱动器的Windows存在的潜在问题,于是C:\\ ,这是一个容易解决。

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

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