免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 993 | 回复: 0
打印 上一主题 下一主题

获取 CPU 序列号 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-15 14:52 |只看该作者 |倒序浏览

#include stdio.h>

typedef struct {
    unsigned int eax;
    unsigned int edx;
    unsigned int ecx;
} cpusn_t;

int get_cpusn(cpusn_t *sn)
{
    sn->eax = 1;
    __asm__ (
        "cpuid\n\t"
        :"=c"(sn->ecx), "=d"(sn->edx), "=a" (sn->eax)
        :"2" (sn->eax)
    );
    if ((sn->edx & (117)) == 0)
        return 1;

    sn->eax = 3;
    __asm__ (
        "cpuid\n\t"
        :"=c"(sn->ecx), "=d"(sn->edx), "=a" (sn->eax)
        :"2" (sn->eax)
    );
    return 0;
}
void print_cpusn(cpusn_t sn)
{
    char buf[30];
    snprintf(buf, sizeof(buf), "%04X-%04X-%04X-%04X-%04X-%04X",
        sn.eax>> 16, sn.eax & 0xffff,
        sn.edx>> 16, sn.edx & 0xffff,
        sn.ecx>> 16, sn.ecx & 0xffff);
    printf("%s\n", buf);

}

int main(void)
{
    cpusn_t sn = {0, 0, 0};
    if (get_cpusn(&sn) == 0) {
        print_cpusn(sn);
    } else {
        //printf("edx = %x\n", sn.edx);
        printf("Doesn't support this.\n");
    }
    return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/76292/showart_2052929.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP