// “获得Intel CPU ID”按钮消息处理函数

void CIntelCPUIDDlg::OnBtnCPUID()

{

unsigned long s1,s2;

unsigned char vendor_id[]="------------";//CPU提供商ID

CString str1,str2,str3;

// 以下为获得CPU ID的汇编语言指令

_asm // 得到CPU提供商信息

{

xor eax,eax // 将eax清0

cpuid // 获取CPUID的指令

mov dword ptr vendor_id,ebx

mov dword ptr vendor_id[+4],edx

mov dword ptr vendor_id[+8],ecx

}

str1.Format("%s",vendor_id);

_asm // 得到CPU ID的高32位

{

mov eax,01h

xor edx,edx

cpuid

mov s2,eax

}

str2.Format("%08X-",s2);

_asm // 得到CPU ID的低64位

{

mov eax,03h

xor ecx,ecx

xor edx,edx

cpuid

mov s1,edx

mov s2,ecx

}

str3.Format("%08X-%08X\n",s1,s2);

str2=str2+str3;

m_editVendor.SetWindowText(str1);

m_editCPUID.SetWindowText(str2);

}

// GetHDSerial.cpp: implementation of the CGetHDSerial class.

//

//

#include "stdafx.h"

#include "GetHDSerial.h"

char m_buffer[256];

WORD m_serial[256];

DWORD m_OldInterruptAddress;

DWORDLONG m_IDTR;

// 等待硬盘空闲

static unsigned int WaitHardDiskIdle()

{

BYTE byTemp;

Waiting:

_asm

{

mov dx, 0x1f7

in al, dx

cmp al, 0x80

jb Endwaiting

jmp Waiting

}

Endwaiting:

_asm

{

mov byTemp, al

}

return byTemp;

}

//中断服务程序

void _declspec( naked )InterruptProcess(void)

{

int byTemp;

int i;

WORD temp;

//保存寄存器值

_asm

{

push eax

push ebx

push ecx

push edx

push esi

}

WaitHardDiskIdle();//等待硬盘空闲状态

_asm

{

mov dx, 0x1f6

mov al, 0xa0

out dx, al

}

byTemp = WaitHardDiskIdle(); //若直接在Ring3级执行等待命令,会进入死循环

if ((byTemp&0x50)!=0x50)

{

_asm // 恢复中断现场并退出中断服务程序

{

pop esi

pop edx

pop ecx

pop ebx

pop eax

iretd

}

}

_asm

{

mov dx, 0x1f6 //命令端口1f6,选择驱动器0

mov al, 0xa0

out dx, al

inc dx

mov al, 0xec

out dx, al //发送读驱动器参数命令

}

byTemp = WaitHardDiskIdle();

if ((byTemp&0x58)!=0x58)

{

_asm // 恢复中断现场并退出中断服务程序

{

pop esi

pop edx

pop ecx

pop ebx

pop eax

iretd

}

}

//读取硬盘控制器的全部信息

for (i=0;i<256;i++)

{

_asm

{

mov dx, 0x1f0

in ax, dx

mov temp, ax

}

m_serial[i] = temp;

}

_asm

{

pop esi

pop edx

pop ecx

pop ebx

pop eax

iretd

}

}

//

// Construction/Destruction

//

CGetHDSerial::CGetHDSerial()

{

}

CGetHDSerial::~CGetHDSerial()

{

}

// 读取硬盘序列号函数

char* CGetHDSerial::GetHDSerial()

{

m_buffer[0]='\n';

// 得到当前操作系统版本

OSVERSIONINFO OSVersionInfo;

OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

GetVersionEx( &OSVersionInfo);

if (OSVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)

{

// Windows 9x/ME下读取硬盘序列号

WORD m_wWin9xHDSerial[256];

Win9xReadHDSerial(m_wWin9xHDSerial);

strcpy (m_buffer, WORDToChar (m_wWin9xHDSerial, 10, 19));

}

else

{

// Windows NT/2000/XP下读取硬盘序列号

DWORD m_wWinNTHDSerial[256];

// 判断是否有SCSI硬盘

if ( ! WinNTReadIDEHDSerial(m_wWinNTHDSerial))

WinNTReadSCSIHDSerial(m_wWinNTHDSerial);

strcpy (m_buffer, DWORDToChar (m_wWinNTHDSerial, 10, 19));

}

return m_buffer;

}

// Windows9X/ME系统下读取硬盘序列号

void _stdcall CGetHDSerial::Win9xReadHDSerial(WORD * buffer)

{

int i;

for(i=0;i<256;i++)

buffer[i]=0;

_asm

{

push eax

//获取修改的中断的中断描述符(中断门)地址

sidt m_IDTR

mov eax,dword ptr [m_IDTR+02h]

add eax,3*08h+04h

cli

//保存原先的中断入口地址

push ecx

mov ecx,dword ptr [eax]

mov cx,word ptr [eax-04h]

mov dword ptr m_OldInterruptAddress,ecx

pop ecx

//设置修改的中断入口地址为新的中断处理程序入口地址

push ebx

lea ebx,InterruptProcess

mov word ptr [eax-04h],bx

shr ebx,10h

mov word ptr [eax+02h],bx

pop ebx

//执行中断,转到Ring 0(类似CIH病毒原理)

int 3h

//恢复原先的中断入口地址

push ecx

mov ecx,dword ptr m_OldInterruptAddress

mov word ptr [eax-04h],cx

shr ecx,10h

mov word ptr [eax+02h],cx

pop ecx

sti

pop eax

}

for(i=0;i<256;i++)

buffer[i]=m_serial[i];

}

// Windows 9x/ME系统下,将字类型(WORD)的硬盘信息转换为字符类型(char)

char * CGetHDSerial::WORDToChar (WORD diskdata [256], int firstIndex, int lastIndex)

{

static char string [1024];

int index = 0;

int position = 0;

// 按照高字节在前,低字节在后的顺序将字数组diskdata 中内容存入到字符串string中

for (index = firstIndex; index <= lastIndex; index++)

{

// 存入字中的高字节

string [position] = (char) (diskdata [index] / 256);

position++;

// 存入字中的低字节

string [position] = (char) (diskdata [index] % 256);

position++;

}

// 添加字符串结束标志

string [position] = '\0';

// 删除字符串中空格

for (index = position - 1; index > 0 && ' ' == string [index]; index--)

string [index] = '\0';

return string;

}

// Windows NT/2000/XP系统下,将双字类型(DWORD)的硬盘信息转换为字符类型(char)

char* CGetHDSerial::DWORDToChar (DWORD diskdata [256], int firstIndex, int lastIndex)

{

static char string [1024];

int index = 0;

int position = 0;

// 按照高字节在前,低字节在后的顺序将双字中的低字存入到字符串string中

for (index = firstIndex; index <= lastIndex; index++)

{

// 存入低字中的高字节

string [position] = (char) (diskdata [index] / 256);

position++;

// 存入低字中的低字节

string [position] = (char) (diskdata [index] % 256);

position++;

}

// 添加字符串结束标志

string [position] = '\0';

// 删除字符串中空格

for (index = position - 1; index > 0 && ' ' == string [index]; index--)

string [index] = '\0';

return string;

}

// Windows NT/2000/XP下读取IDE硬盘序列号

BOOL CGetHDSerial::WinNTReadIDEHDSerial(DWORD * buffer)

{

BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];

BOOL bFlag = FALSE;

int drive = 0;

char driveName [256];

HANDLE hPhysicalDriveIOCTL = 0;

sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);

// Windows NT/2000/XP下创建文件需要管理员权限

hPhysicalDriveIOCTL = CreateFile (driveName,

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,

OPEN_EXISTING, 0, NULL);

if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)

{

GETVERSIONOUTPARAMS VersionParams;

DWORD cbBytesReturned = 0;

// 得到驱动器的IO控制器版本

memset ((void*) &VersionParams, 0, sizeof(VersionParams));

if(DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_GET_VERSION,

NULL, 0, &VersionParams,

sizeof(VersionParams),

&cbBytesReturned, NULL) )

{

if (VersionParams.bIDEDeviceMap > 0)

{

BYTE bIDCmd = 0; // IDE或者ATAPI识别命令

SENDCMDINPARAMS scip;

// 如果驱动器是光驱,采用命令IDE_ATAPI_IDENTIFY, command,

// 否则采用命令IDE_ATA_IDENTIFY读取驱动器信息

bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10)?

IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;

memset (&scip, 0, sizeof(scip));

memset (IdOutCmd, 0, sizeof(IdOutCmd));

// 获取驱动器信息

if (WinNTGetIDEHDInfo (hPhysicalDriveIOCTL,

&scip,

(PSENDCMDOUTPARAMS)&IdOutCmd,

(BYTE) bIDCmd,

(BYTE) drive,

&cbBytesReturned))

{

int m = 0;

USHORT *pIdSector = (USHORT *)

((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;

for (m = 0; m < 256; m++)

buffer[m] = pIdSector [m];

bFlag = TRUE; // 读取硬盘信息成功

}

}

}

CloseHandle (hPhysicalDriveIOCTL); // 关闭句柄

}

return bFlag;

}

// WindowsNT/2000/XP系统下读取SCSI硬盘序列号

BOOL CGetHDSerial::WinNTReadSCSIHDSerial (DWORD * buffer)

{

buffer[0]='\n';

int controller = 0;

HANDLE hScsiDriveIOCTL = 0;

char driveName [256];

sprintf (driveName, "\\\\.\\Scsi%d:", controller);

// Windows NT/2000/XP下任何权限都可以进行

hScsiDriveIOCTL = CreateFile (driveName,

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,

OPEN_EXISTING, 0, NULL);

if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE)

{

int drive = 0;

DWORD dummy;

for (drive = 0; drive < 2; drive++)

{

char buffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];

SRB_IO_CONTROL *p = (SRB_IO_CONTROL *) buffer;

SENDCMDINPARAMS *pin =

(SENDCMDINPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));

// 准备参数

memset (buffer, 0, sizeof (buffer));

p -> HeaderLength = sizeof (SRB_IO_CONTROL);

p -> Timeout = 10000;

p -> Length = SENDIDLENGTH;

p -> ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;

strncpy ((char *) p -> Signature, "SCSIDISK", 8);

pin -> irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;

pin -> bDriveNumber = drive;

// 得到SCSI硬盘信息

if (DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,

buffer,

sizeof (SRB_IO_CONTROL) +

sizeof (SENDCMDINPARAMS) - 1,

buffer,

sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,

&dummy, NULL))

{

SENDCMDOUTPARAMS *pOut =

(SENDCMDOUTPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));

IDSECTOR *pId = (IDSECTOR *) (pOut -> bBuffer);

if (pId -> sModelNumber [0])

{

int n = 0;

USHORT *pIdSector = (USHORT *) pId;

for (n = 0; n < 256; n++)

buffer[n] =pIdSector [n];

return TRUE; // 读取成功

}

}

}

CloseHandle (hScsiDriveIOCTL); // 关闭句柄

}

return FALSE; // 读取失败

}

// Windows NT/2000/XP下读取IDE设备信息

BOOL CGetHDSerial::WinNTGetIDEHDInfo (HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,

PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,

PDWORD lpcbBytesReturned)

{

// 为读取设备信息准备参数

pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;

pSCIP -> irDriveRegs.bFeaturesReg = 0;

pSCIP -> irDriveRegs.bSectorCountReg = 1;

pSCIP -> irDriveRegs.bSectorNumberReg = 1;

pSCIP -> irDriveRegs.bCylLowReg = 0;

pSCIP -> irDriveRegs.bCylHighReg = 0;

// 计算驱动器位置

pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);

// 设置读取命令

pSCIP -> irDriveRegs.bCommandReg = bIDCmd;

pSCIP -> bDriveNumber = bDriveNum;

pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;

// 读取驱动器信息

return ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_GET_DRIVE_INFO,

(LPVOID) pSCIP,

sizeof(SENDCMDINPARAMS) - 1,

(LPVOID) pSCOP,

sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,

lpcbBytesReturned, NULL) );

}

取消

评论

java 硬盘序列号_java肿么获取硬盘序列号 iteye相关推荐

  1. java正则表达式 中括号_Java正则表达式获取中括号之间的内容

    不包含中括号 正则表达式如下: \\[(.*?)] 注: .匹配除换行符\n之外的任何单字符: *匹配前面的子表达式零次或多次: ?匹配前面的子表达式零次或一次: ()标记一个子表达式的开始和结束位置 ...

  2. linux c取网卡名称,在Linux下用c编程肿么获取网卡序列号和硬盘序列号

    system(执行shell命令)相关函数fork,execve,waitpid,popen表头文件#include定义函数intsystem(constchar*string);函数说明system ...

  3. java正则 括号_java正则表达式获取大括号小括号内容并判断数字和小数亲测可用...

    获取大括号小括号内容 项目开发用到了,暂做个简单记录 private static String regex = "\\{([^}]*)\\}";//匹配大括号 private s ...

  4. java 不知道类名_Java 中获取类名的三种方法,你知道几种?

    之前我们分享过Java高级篇--深入浅出Java类加载机制这篇文章,今天来带你了解获取类名的三种方法. 获取类名的方法 Java 中获取类名的方式主要有以下三种. getName() 返回的是虚拟机里 ...

  5. java 反射 速度_Java反射获取实例的速度对比分析

    之前代码有一个逻辑,是在初始化时读取某个包下的所有class文件,放入到一个HashMap里.代码运行过程中,通过Key获取到对应class的全路径名,最后通过Class.forName(classN ...

  6. java 字体名字_JAVA:获取系统中可用的字体的名字

    import java.awt.*; public class GetLocalFontFamily { public static void main(String[] agrs) { //获取系统 ...

  7. java 反射 速度_Java 反射获取类方法速率和实现方式

    今天突然想到Java反射.然后测试了一下1.8.0_191版本的反射性能,没想到反射竟然比直接调用性能还高. 反射介绍 Java反射是指在程序运行状态中,能检查任意对象的内容并调用任意方法.对于任何一 ...

  8. java exe 路径_Java程序获取执行自己的java.exe路径

    首先通过:java.lang.management包提供的接口获取当前进程ID:接下来利用wmic命令根据进程ID获取进程exe的目录.代码如下: /** * Windows下获取运行当前程序的jav ...

  9. java取当前日期_java如何获取系统的当前时间

    java获取系统当前时间的方法:获取当前时间和当前时间的前30秒,代码为[simple date format df = new simple date format(" yyyy-MM-D ...

  10. 获取java时间 小时_Java 如何获取当前时间前一个小时的时间

    在网上找好多类似文章或者帖子,绝大部分都是不正确和不准确的,都是互相转来转去的.下面是我自己通过研究Java API得出正确获取当前时间前一小时的时间正确方法. public static void ...

最新文章

  1. 镜像和linux关系,Docker中容器和镜像的关系【通俗易懂】
  2. C语言:一个数组中只有两个数字是出现一次
  3. 广东省计算机应用考试试题,2015广东省计算机等级考试试题 二级C试题最新考试试题库...
  4. mybatis批量更新及其效率问题
  5. werkzeug Request
  6. Python的一些单行代码(摘抄)
  7. 全球唯一顶级车展来袭,云徙科技成为造车新势力?
  8. Linux 内存管理:DAX(Direct Access)机制的作用及实现原理
  9. 光伏工程cad计算机软件,4.23MWp光伏发电示范工程全套电气施工cad图纸
  10. linux 有道笔记,Linux笔记:manjaro_折腾笔记
  11. 甲骨文公司老板埃里森在耶如大学的…
  12. adb检测不到模拟器的解决方法
  13. QT小游戏(五子棋)
  14. 从零开始自制实现WebServer(十九)---- 正式系统的学习一下Git 捣鼓捣鼓github以及一些其他的小组件
  15. kodu_Kodu教您的孩子直观地编程自己的视频游戏
  16. 在web网页中打开word文档
  17. cc2530期末试卷_ZigBee期末试题.docx
  18. ubuntu 内存占用过高导致卡死 解决办法
  19. saas 测试_为什么SaaS公司需要进行安全测试?
  20. jquery通过name,id名称获取当前value值

热门文章

  1. linux安装文泉驿字体,centos 安装文泉驿字体相关教程
  2. python3.9.5安装教程加汉化
  3. 计算机桌面怎么分区域,怎样设置电脑桌面的区域分割?
  4. 十三肝了2晚的《IDEA操作手册-终极秘籍》终于来了...
  5. Cannot resolve com.oracle:ojdbc14:11.2.0.1.0
  6. 公司用的非标普通自动化用单片机还是plc_高级电气自动化工程师必备十大技能...
  7. 虹科和ELPRO推出符合GxP标准的自助式温度分布验证套装
  8. 方管图纸标注_图样中型材的标注方法
  9. 机器人机电液控制一体化联合仿真
  10. SAP BAPI_SALESORDER_CREATEFROMDAT2 创建销售订单