简体   繁体   English

C++ 获取处理器 ID

[英]C++ get Processor ID

This thread is ok.这个线程没问题。 How to get Processor and Motherboard Id? 如何获取处理器和主板 ID?

I wanted to get processor ID using C++ code not using WMI or any third party lib.我想使用 C++ 代码而不使用 WMI 或任何第三方库来获取处理器 ID。

OR anything on a computer that turns out to be unique.或者计算机上的任何东西都是独一无二的。 One thing is Ethernet ID but which is again removable on some machines.一件事是以太网 ID,但它在某些机器上又是可移动的。 This I want to use mostly for licensing purpose.我想主要用于许可目的。

Is processor ID unique and available on all major processors?处理器 ID 是唯一的并且在所有主要处理器上都可用吗?

I had a similar problem lately and I did the following.我最近遇到了类似的问题,我做了以下事情。 First I gained some unique system identification values:首先,我获得了一些独特的系统识别值:

I took these values and combined them in an arbitrary but deterministic way (read update below!) (adding, xoring, dividing and keeping the remainder etc.).我将这些值以任意但确定性的方式组合起来(阅读下面的更新!) (添加、异或、除和保留余数等)。 Iterate over the values as if they were strings and be creative.像字符串一样迭代这些值并具有创造性。 In the end, you will get a byte literal which you can transform to the ASCII range of letters and numbers to get a unique, "readable" code that doesn't look like noise.最后,您将获得一个字节文字,您可以将其转换为字母和数字的 ASCII 范围,以获得一个独特的、“可读”的、看起来不像噪音的代码。

Another approach can be simply concatenating these values and then "cover them up" with xoring something over them (and maybe transforming to letters again).另一种方法可以简单地连接这些值,然后通过对它们进行异或运算(并且可能再次转换为字母)来“掩盖它们”。

I'm saying it's unique, because at least one of the inputs is supposed to be unique (the MAC address).我是说它是唯一的,因为至少有一个输入应该是唯一的(MAC 地址)。 Of course you need some understanding of number theory to not blew away this uniqueness, but it should be good enough anyway.当然,你需要对数论有一定的了解才能不破坏这种独特性,但无论如何它应该足够好。

Important update : Since this post I learned a few things about cryptography, and I'm on the opinion that making up an arbitrary combination (essentially your own hash) is almost certainly a bad idea.重要更新:自从这篇文章以来,我学到了一些关于密码学的知识,我认为组成一个任意组合(基本上是你自己的哈希)几乎肯定是一个坏主意。 Hash functions used in practice are constructed to be well-behaved (as in low probability of collisions) and to be hard to break (the ability construct a value that has the same hash value as another).实践中使用的散列函数被构造为表现良好(如碰撞概率低)并且难以破坏(能够构造一个与另一个具有相同散列值的值)。 Constructing such a function is a very hard computer science problem and unless you are qualified, you shouldn't attempt.构建这样一个函数是一个非常困难的计算机科学问题,除非你有资格,否则你不应该尝试。 The correct approach for this is to concatenate whatever information you can collect about the hardware (ie the ones I listed in the post) and use a cryptographic hash or digital signature to get a verifiable and secure output.正确的方法是连接您可以收集的有关硬件的任何信息(即我在帖子中列出的信息),并使用加密哈希或数字签名来获得可验证且安全的输出。 Do not implement the cryptographic algorithms yourself either;也不要自己实现加密算法; there are lots of vulnerability pitfalls that take lots of knowledge to avoid.有许多漏洞陷阱需要大量知识才能避免。 Use a well-known and trusted library for the implementation of the algorithms.使用知名且值得信赖的库来实现算法。

If you're using Visual Studio, Microsoft provides the __cpuid instrinsic in the <intrin.h> header.如果您使用的是 Visual Studio,Microsoft 会在<intrin.h>标头中提供__cpuid内在参数。 Example on the linked msdn site.链接的 msdn 站点上的示例。

Hm...嗯...

There are special libraries to generate unique ID based on the hardware installed (so for the specified computer this ID always be the same).有特殊的库可以根据安装的硬件生成唯一 ID(因此对于指定的计算机,此 ID 始终相同)。 Most of them takes motherboard ID + HDD ID + CPU ID and mix these values.它们中的大多数采用主板 ID + HDD ID + CPU ID 并混合这些值。

Whe reinvent the wheel?谁在重新发明轮子? Why not to use these libraries?为什么不使用这些库? Any serious reason?有什么严重的原因吗?

You can use command line.您可以使用命令行。

wmic cpu list full

wmic baseboard list full

Or WMI interface或者WMI接口

#include <wmi.hpp>
#include <wmiexception.hpp>
#include <wmiresult.hpp>

#include <../src/wmi.cpp>
#include <../src/wmiresult.cpp> // used

#pragma comment(lib, "wbemuuid.lib")

struct Win32_WmiCpu
{

    void setProperties(const WmiResult& result, std::size_t index)
    {
        //EXAMPLE EXTRACTING PROPERTY TO CLASS
        result.extract(index, "ProcessorId", (*this).m_cpuID);
    }

    static std::string getWmiClassName()
    {
        return "Win32_Processor";
    }

    string m_cpuID;
    //All the other properties you wish to read from WMI

}; //end struct Win32_ComputerSystem
   
struct Win32_WmiMotherBoard
{

    void setProperties(const WmiResult& result, std::size_t index)
    {
        //EXAMPLE EXTRACTING PROPERTY TO CLASS
        result.extract(index, "SerialNumber", (*this).m_mBId);
    }

    static std::string getWmiClassName()
    {
        return "Win32_BaseBoard";
    }

    string m_mBId;

}; //end struct Win32_ComputerSystem


try
{
    const Win32_WmiCpu cpu = Wmi::retrieveWmi<Win32_WmiCpu>();
    strncpy_s(ret.m_cpu, cpu.m_cpuID.c_str(), _TRUNCATE);
}
catch (const Wmi::WmiException& )
{
}
try
{
    const Win32_WmiMotherBoard mb = Wmi::retrieveWmi<Win32_WmiMotherBoard>();
    strncpy_s(ret.m_mb, mb.m_mBId.c_str(), _TRUNCATE);
}
catch (const Wmi::WmiException& )
{
}

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

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