C++ 中的 CPUID 实现
我想知道这里是否有人有一些可以从任何托管 .net 语言引用的 C++ CPUID 实现的好示例。
另外,如果情况并非如此,我是否应该注意 X86 和 X64 之间的某些实现差异?
我想使用 CPUID 来获取运行我的软件的机器上的信息(崩溃报告等...),并且我希望保持所有内容尽可能广泛兼容。
我问这个问题的主要原因是,尽管我有关于 CPU 寄存器等的基本知识,但在编写可能是所有机器指令的内容时,我完全是个菜鸟……
在人们开始告诉我 Google 之前:我在网上找到了一些例子,但通常它们并不意味着允许与托管代码进行交互,并且没有一个示例同时针对 X86 和 X64。大多数示例似乎都是 X86 特定的。
I would like to know if somebody around here has some good examples of a C++ CPUID implementation that can be referenced from any of the managed .net languages.
Also, should this not be the case, should I be aware of certain implementation differences between X86 and X64?
I would like to use CPUID to get info on the machine my software is running on (crashreporting etc...) and I want to keep everything as widely compatible as possible.
Primary reason I ask is because I am a total noob when it comes to writing what will probably be all machine instructions though I have basic knowledge about CPU registers and so on...
Before people start telling me to Google: I found some examples online, but usually they were not meant to allow interaction from managed code and none of the examples were aimed at both X86 and X64. Most examples appeared to be X86 specific.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
访问原始 CPUID 信息实际上非常容易,这里有一个适用于 Windows、Linux 和 OSX 的 C++ 类:
要使用它,只需实例化该类的实例,加载您感兴趣的 CPUID 指令并检查寄存器。例如:
此维基百科页面告诉您如何使用 CPUID:http://en.wikipedia.org/wiki/CPUID
编辑:根据评论添加了适用于 Windows 的
#include
。Accessing raw CPUID information is actually very easy, here is a C++ class for that which works in Windows, Linux and OSX:
To use it just instantiate an instance of the class, load the CPUID instruction you are interested in and examine the registers. For example:
This Wikipedia page tells you how to use CPUID: http://en.wikipedia.org/wiki/CPUID
EDIT: Added
#include <intrin.h>
for Windows, per comments.请参阅这篇关于 __cpuid 的 MSDN 文章。
有一个使用 Visual Studio 2005 或更高版本进行编译的综合示例。对于 Visual Studio 6,您可以使用它代替编译器固有 __cpuid:
对于 Visual Studio 2005,您可以使用它代替编译器固有 __cpuidex:
See this MSDN article about __cpuid.
There is a comprehensive sample that compiles with Visual Studio 2005 or better. For Visual Studio 6, you can use this instead of the compiler instrinsic __cpuid:
For Visual Studio 2005, you can use this instead of the compiler instrinsic __cpuidex:
可能不完全是您正在寻找的,但英特尔有一个 用于枚举 Intel 64 位平台架构(处理器、缓存等)的好文章和示例代码,它似乎也涵盖了 32 位 x86 处理器。
Might not be exactly what you are looking for, but Intel have a good article and sample code for enumerating Intel 64 bit platform architectures (processor, cache, etc.) which also seems to cover 32 bit x86 processors.