用C++语言怎么c语言 获取cpu使用率的序列号

linux c 获取cpu序列号
我的图书馆
linux c 获取cpu序列号
#include &stdio.h&1234567891011121314151617181920212223242526272829303132333435363738394041424344unsigned int unsigned int unsigned int unsigned int void cpuid(unsigned int veax1){&&&&asm("cpuid"&&&&&&&&:"=a"(veax),&&&&&&&&"=b"(vebx),&&&&&&&&"=c"(vecx),&&&&&&&&"=d"(vedx)&&&&&&&&:"a"(veax));}void LM(unsigned int var,uint32_t *vx){&&int i;&&for(i=0;i&3;i++)&&{&&&&&&var=(var&&i);&&&&&&vx[i]=&&}}&static voidgetcpuid (char *id){&&&&uint32_t ax[3],cx[3],dx[3];&&&&cpuid(1);&&&&LM(veax,ax);&&&&cpuid(3);&&&&LM(vecx,cx);&&&&LM(vedx,dx);&&&&sprintf(id,"%u%u%u%u%u%u%u%u%u",ax[0],ax[1],ax[2],cx[0],cx[1],cx[2],dx[0],dx[1],dx[2]);}&int main(void){&&&&&&&&char cpuid[100];&&&&&&&&getcpuid(cpuid);&&&&&&&&printf("cpuid is %s\\n",cpuid);&&&&&&&&return 0;}
TA的最新馆藏[转]&[转]&| Copyright &
. All Rights Reserved .查看: 5110|回复: 1
VC++如何获取机器码?硬盘序列号、CPU编号、BIOS编号等~
我知道那个用GetVolumeInformation得到的不是硬盘的序列号,这个大家可以去百度一下。得到的结果是分区的编号,这个东西是可以用软件刷洗更改的!
那么到底该用什么函数来获取,我找了半天没有找到,举个例子最好!麻烦大家了~~
我需要用这个加入到自己的程序中~~所以是要源代码~~~
可以试试使用WMI(在msdn上有详细的信息):
硬盘序列号: 用Win32_PhysicalMedia class.
CPU编号: 用Win32_Processor class.
BIOS编号: 用Win32_BIOS class.
下面例子取得硬盘的序列号,其他的用法也类似(msdn上的例子,把Win32_OperatingSystem改成了Win32_PhysicalMedia):
#define _WIN32_DCOM
#include &iostream&
#include &comdef.h&
#include &Wbemidl.h&
# pragma comment(lib, &wbemuuid.lib&)
int main(int argc, char **argv)
& & HRESULT
& & // Step 1: --------------------------------------------------
& & // Initialize COM. ------------------------------------------
& & hres =&&CoInitializeEx(0, COINIT_MULTITHREADED);
& & if (FAILED(hres))
& && &&&cout && &Failed to initialize COM library. Error code = 0x&
& && && && &&& hex && hres &&
& && &&&return 1;& && && && && && &// Program has failed.
& & // Step 2: --------------------------------------------------
& & // Set general COM security levels --------------------------
& & // Note: If you are using Windows 2000, you need to specify -
& & // the default authentication credentials for a user by using
& & // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
& & // parameter of CoInitializeSecurity ------------------------
& & hres =&&CoInitializeSecurity(
& && &&&NULL,
& && &&&-1,& && && && && && && && &&&// COM authentication
& && &&&NULL,& && && && && && && && &// Authentication services
& && &&&NULL,& && && && && && && && &// Reserved
& && &&&RPC_C_AUTHN_LEVEL_DEFAULT,& &// Default authentication
& && &&&RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation&&
& && &&&NULL,& && && && && && && && &// Authentication info
& && &&&EOAC_NONE,& && && && && && & // Additional capabilities
& && &&&NULL& && && && && && && && & // Reserved
& && &&&);
if (FAILED(hres))
& && &&&cout && &Failed to initialize security. Error code = 0x&
& && && && &&& hex && hres &&
& && &&&CoUninitialize();
& && &&&return 1;& && && && && && &&&// Program has failed.
& & // Step 3: ---------------------------------------------------
& & // Obtain the initial locator to WMI -------------------------
& & IWbemLocator *pLoc = NULL;
& & hres = CoCreateInstance(
& && &&&CLSID_WbemLocator,& && && && &
& && &&&0,
& && &&&CLSCTX_INPROC_SERVER,
& && &&&IID_IWbemLocator, (LPVOID *) &pLoc);
& & if (FAILED(hres))
& && &&&cout && &Failed to create IWbemLocator object.&
& && && && &&& & Err code = 0x&
& && && && &&& hex && hres &&
& && &&&CoUninitialize();
& && &&&return 1;& && && && && &&&// Program has failed.
& & // Step 4: -----------------------------------------------------
& & // Connect to WMI through the IWbemLocator::ConnectServer method
& & IWbemServices *pSvc = NULL;
& & // Connect to the root\cimv2 namespace with
& & // the current user and obtain pointer pSvc
& & // to make IWbemServices calls.
& & hres = pLoc-&ConnectServer(
& && && &_bstr_t(L&ROOT\\CIMV2&), // Object path of WMI namespace
& && && &NULL,& && && && && && &&&// User name. NULL = current user
& && && &NULL,& && && && && && &&&// User password. NULL = current
& && && &0,& && && && && && && &&&// Locale. NULL indicates current
& && && &NULL,& && && && && && &&&// Security flags.
& && && &0,& && && && && && && &&&// Authority (e.g. Kerberos)
& && && &0,& && && && && && && &&&// Context object
& && && &&pSvc& && && && && && &&&// pointer to IWbemServices proxy
& && && &);
& & if (FAILED(hres))
& && &&&cout && &Could not connect. Error code = 0x&
& && && && & && hex && hres &&
& && &&&pLoc-&Release();& &&&
& && &&&CoUninitialize();
& && &&&return 1;& && && && && & // Program has failed.
& & cout && &Connected to ROOT\\CIMV2 WMI namespace& &&
// Step 5: --------------------------------------------------
& & // Set security levels on the proxy -------------------------
& & hres = CoSetProxyBlanket(
& && & pSvc,& && && && && && && && &// Indicates the proxy to set
& && & RPC_C_AUTHN_WINNT,& && && &&&// RPC_C_AUTHN_xxx
& && & RPC_C_AUTHZ_NONE,& && && && &// RPC_C_AUTHZ_xxx
& && & NULL,& && && && && && && && &// Server principal name
& && & RPC_C_AUTHN_LEVEL_CALL,& && &// RPC_C_AUTHN_LEVEL_xxx
& && & RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
& && & NULL,& && && && && && && && &// client identity
& && & EOAC_NONE& && && && && && &&&// proxy capabilities
& & if (FAILED(hres))
& && &&&cout && &Could not set proxy blanket. Error code = 0x&
& && && && &&& hex && hres &&
& && &&&pSvc-&Release();
& && &&&pLoc-&Release();& &&&
& && &&&CoUninitialize();
& && &&&return 1;& && && && && &// Program has failed.
& & // Step 6: --------------------------------------------------
& & // Use the IWbemServices pointer to make requests of WMI ----
& & // For example, get the name of the operating system
& & IEnumWbemClassObject* pEnumerator = NULL;
& & hres = pSvc-&ExecQuery(
& && &&&bstr_t(&WQL&),
& && &&&bstr_t(&SELECT * FROM Win32_PhysicalMedia&),
& && &&&WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
& && &&&NULL,
& && &&&&pEnumerator);
& & if (FAILED(hres))
& && &&&cout && &Query for physical media failed.&
& && && && &&& & Error code = 0x&
& && && && &&& hex && hres &&
& && &&&pSvc-&Release();
& && &&&pLoc-&Release();
& && &&&CoUninitialize();
& && &&&return 1;& && && && && &// Program has failed.
& & // Step 7: -------------------------------------------------
& & // Get the data from the query in step 6 -------------------
& & IWbemClassObject *pclsO
& & ULONG uReturn = 0;
& & while (pEnumerator)
& && &&&HRESULT hr = pEnumerator-&Next(WBEM_INFINITE, 1,
& && && && &&pclsObj, &uReturn);
& && &&&if(0 == uReturn)
& && && && &
& && &&&VARIANT vtP
& && &&&// Get the value of the Name property
& && &&&hr = pclsObj-&Get(L&SerialNumber&, 0, &vtProp, 0, 0);
& &&&wcout && &Serial Number : & && vtProp.bstrVal &&
& && &&&VariantClear(&vtProp);
& & // Cleanup
& & // ========
& & pSvc-&Release();
& & pLoc-&Release();
& & pEnumerator-&Release();
& & pclsObj-&Release();
& & CoUninitialize();
& & return 0;& &// Program successfully completed.
Powered by
& 最好的辅助编程技术论坛今天看啥 热点:
ASM 取CPU序列号 / CPUID,asmcpuid
获取CPU序列号我知道大概有两种方式一种为ASM另一种为WMI
如果要好点的话肯定是首选汇编了、没什么好解释的哇 不过今天
我们只在C#、C++、E三种语言上内嵌汇编实现获取CPUID的办法
首先我们先看看下面各种语言代码运行后的效果图、
上面是C#嵌入汇编运行后获取到的CPUID 我们在看看易语言上的
上面是E语言嵌入汇编后运行的结果 两者输出的值是相同、
可以证明嵌入的汇编运行上没有问题、
上面是C++嵌入汇编后运行的结果 两者输出的值是相同、
我特定找了两台不同的电脑用于测试本代码是否有效
效验图一:
效验图二:
从上图中你可以清楚看到、两款不同的CPU那么它们的CPUID也不应一致
但是你从上图已经明确看到序列号不一的结果、所以不必担心代码有问题
#include &stdafx.h&
#include &stdio.h&
int* GetGPUID()
mov eax,00h
xor edx,edx
mov dword ptr [ebp-4],edx
mov dword ptr [ebp-8],eax
mov eax,01h
xor ecx,ecx
xor edx,edx
mov dword ptr [ebp-12],edx
mov dword ptr [ebp-16],eax
eax,dword ptr [ebp-4]
dword ptr [ebp-20],eax
eax,dword ptr [ebp-8]
dword ptr [ebp-24],eax
eax,dword ptr [ebp-12]
dword ptr [ebp-28],eax
eax,dword ptr [ebp-16]
dword ptr [ebp-32],eax
eax, dword ptr[ebp-20]
int _tmain(int argc, _TCHAR* argv[])
int* ptr = GetGPUID();
int s1 = *ptr++;
int s2 = *ptr++;
int s3 = *ptr++;
int s4 = *
printf(&%X%X%X%X&, s1, s2, s3, s4);
getchar();
上面是在C++中内嵌的汇编代码部分、功能与C#与E语言的相同
.支持库 eAPI
.子程序 __启动窗口_创建完毕
.局部变量 ptr, 整数型
.局部变量 s1, 整数型
.局部变量 s2, 整数型
.局部变量 s3, 整数型
.局部变量 s4, 整数型
ptr = CallWindowProc ({ 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64, 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171, 184, 0, 0, 0, 0, 51, 210, 15, 162, 137, 85, 252, 137, 69, 248, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 137, 85, 244, 137, 69, 240, 139, 69, 252, 137, 69, 236, 139, 69, 248, 137, 69, 232, 139, 69, 244, 137, 69, 228, 139, 69, 240, 137, 69, 224, 141, 69, 236, 95, 94, 91, 139, 229, 93, 195 }, #NULL, #NULL, #NULL, #NULL)
s1 = 指针到整数 (ptr)
s2 = 指针到整数 (ptr + 4)
s3 = 指针到整数 (ptr + 8)
s4 = 指针到整数 (ptr + 12)
信息框 (格式化文本 (“%X%X%X%X”, s1, s2, s3, s4), #信息图标, , )
上面是E语言内嵌汇编的调用的代码、不过我倒是很建议大家下载打包的源代码
里面包含C++、E语言、C#三种不同的代码 我不是很喜欢Java你们可以看不起我、
我不否认我是一名C#屌丝程序员、虽然我一直很菜菜啦、但是我相当快乐、咔咔
上面的汇编是在x86上进行的、所以你需要在C#项目属性中把 目标平台 / 开发环境
修改为x86不然等会你把代码全部搞定运行不了、才搞笑的心慌 你说是不是?
现在我们开始编写代码,我们需要把常量以及需要的API全部声明出来
不然等会怎么用、嘻嘻 的吧
static partial class Program
[DllImport(&kernel32.dll&, SetLastError = true)]
private static extern bool VirtualProtect(byte[] lpAddress, int dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport(&user32.dll&, SetLastError = true)]
private static extern IntPtr CallWindowProc(byte[] lpPrevWndFunc, int hWnd, int Msg, int wParam, int lParam);
private const int NULL = 0;
private const int PAGE_EXECUTE_READWRITE = 64;
现在我们必须要把嵌入到C#里面的汇编以机器码的形式写出来
static partial class Program
private static readonly byte[] buf_asm = { 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64, 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171, 184, 0, 0, 0, 0, 51, 210, 15, 162, 137, 85, 252, 137, 69, 248, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 137, 85, 244, 137, 69, 240, 139, 69, 252, 137, 69, 236, 139, 69, 248, 137, 69, 232, 139, 69, 244, 137, 69, 228, 139, 69, 240, 137, 69, 224, 141, 69, 236, 95, 94, 91, 139, 229, 93, 195 };
现在我们开始进入正规了、编写修改内存保护的代码 否则等会
一调用就GameOver上次我在写另一片文章中专门提到过这个问
题,如果有需要可以看看 http://blog.csdn.net/u/article/details/
static partial class Program
private static void VirtualProtect(byte[] address)
uint lpflOldP
VirtualProtect(address, address.Length, PAGE_EXECUTE_READWRITE, out lpflOldProtect);
&好的、热血澎湃的时刻到了 准备好编写下面的代码 快快快 呵呵
static partial class Program
private static string GetCPUID()
VirtualProtect(buf_asm);
IntPtr ptr = CallWindowProc(buf_asm, NULL, NULL, NULL, NULL);
int s1 = Marshal.ReadInt32(ptr);
int s2 = Marshal.ReadInt32(ptr, 4);
int s3 = Marshal.ReadInt32(ptr, 8);
int s4 = Marshal.ReadInt32(ptr, 12);
return s1.ToString(&X&) + s2.ToString(&X&) + s3.ToString(&X&) + s4.ToString(&X&);
我的废话一下,因为在汇编中返回的是一个整型数组的指针、而
整形数组只有4个值,所以你会看到上面我一直在调用Marshal.ReadInt32
当然你也可以使用C#中的原生指针、那感觉很酸爽 哈哈、
然后在把s1-s4的所有值转换成“%X十六进制格式文本”链接在一起
返回给调用方,OK解决问题、上次我是使用委托调用汇编本次换为
CallWindowProc去呼叫函数地址、两个差不多 我只是不想在写委托了、
static partial class Program
[STAThread]
static void Main()
Console.WriteLine(GetCPUID());
Console.ReadKey(false);
上面是在C#滴Main函数中调用GetCPUID函数的部分,本次的意义
并不大、反正逼格已经完美 再也不担心妈妈说我不懂编程了、呵呵
源代码:/s/1o602yXs&// 含C++、E语言、C#三种语言的源代码、
C#的源代码添加到你项目中时记得先把开发环境切换为x86不然运行不了 别怪我、
版权声明:本文为博主原创文章,未经博主允许不得转载。
暂无相关文章
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&
WEB编程教程最近更新

我要回帖

更多关于 c语言获取硬盘序列号 的文章

 

随机推荐