我是靠谱客的博主 闪闪巨人,最近开发中收集的这篇文章主要介绍在Win平台得到磁盘、CPU、用户信息,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

// SN.h: interface for the CSN class.
//
//

#if !defined(AFX_SN_H__54CD147E_A7EE_4C0E_A9DA_2AA865F5C62D__INCLUDED_)
#define AFX_SN_H__54CD147E_A7EE_4C0E_A9DA_2AA865F5C62D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//Get HardDisk ID
#define FILE_DEVICE_SCSI              0x0000001b
#define IOCTL_SCSI_MINIPORT_IDENTIFY ( ( FILE_DEVICE_SCSI << 16 ) + 0x0501 )
#define IOCTL_SCSI_MINIPORT 0x0004D008 // see NTDDSCSI.H for definition
#define IDENTIFY_BUFFER_SIZE 512
#define SENDIDLENGTH ( sizeof( SENDCMDOUTPARAMS ) + IDENTIFY_BUFFER_SIZE )
#define IDE_ATAPI_IDENTIFY 0xA1 // Returns ID sector for ATAPI.
#define IDE_ATA_IDENTIFY    0xEC // Returns ID sector for ATA.
#define DFP_RECEIVE_DRIVE_DATA   0x0007c088

class CSN 
{
//struct 
public:
typedef struct _DRIVERSTATUS
{
BYTE bDriverError; // Error code from driver, or 0 if no error.
BYTE bIDEStatus;    // Contents of IDE Error register.
// Only valid when bDriverError is SMART_IDE_ERROR.
BYTE bReserved[2]; // Reserved for future expansion.
DWORD dwReserved[2]; // Reserved for future expansion.
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;

typedef struct _SENDCMDOUTPARAMS
{
DWORD         cBufferSize;   // Size of bBuffer in bytes
DRIVERSTATUS DriverStatus; // Driver status structure.
BYTE          bBuffer[1];    // Buffer of arbitrary length in which to store the data read from the                                                       // drive.
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;

typedef struct _IDEREGS
{
BYTE bFeaturesReg;       // Used for specifying SMART "commands".
BYTE bSectorCountReg;    // IDE sector count register
BYTE bSectorNumberReg;   // IDE sector number register
BYTE bCylLowReg;         // IDE low order cylinder value
BYTE bCylHighReg;        // IDE high order cylinder value
BYTE bDriveHeadReg;      // IDE drive/head register
BYTE bCommandReg;        // Actual IDE command.
BYTE bReserved;          // reserved for future use. Must be zero.
} IDEREGS, *PIDEREGS, *LPIDEREGS;

typedef struct _SENDCMDINPARAMS
{
DWORD     cBufferSize;   // Buffer size in bytes
IDEREGS   irDriveRegs;   // Structure with drive register values.
BYTE bDriveNumber;       // Physical drive number to send 
// command to (0,1,2,3).
BYTE bReserved[3];       // Reserved for future expansion.
DWORD     dwReserved[4]; // For future use.
BYTE      bBuffer[1];    // Input buffer.
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;


typedef struct _GETVERSIONOUTPARAMS
{
BYTE bVersion;      // Binary driver version.
BYTE bRevision;     // Binary driver revision.
BYTE bReserved;     // Not used.
BYTE bIDEDeviceMap; // Bit map of IDE devices.
DWORD fCapabilities; // Bit mask of driver capabilities.
DWORD dwReserved[4]; // For future use.
} GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;

typedef struct _SRB_IO_CONTROL
{
ULONG HeaderLength;
UCHAR Signature[8];
ULONG Timeout;
ULONG ControlCode;
ULONG ReturnCode;
ULONG Length;
} SRB_IO_CONTROL, *PSRB_IO_CONTROL;

typedef struct _IDSECTOR
{
USHORT wGenConfig;
USHORT wNumCyls;
USHORT wReserved;
USHORT wNumHeads;
USHORT wBytesPerTrack;
USHORT wBytesPerSector;
USHORT wSectorsPerTrack;
USHORT wVendorUnique[3];
CHAR    sSerialNumber[20];
USHORT wBufferType;
USHORT wBufferSize;
USHORT wECCSize;
CHAR    sFirmwareRev[8];
CHAR    sModelNumber[40];
USHORT wMoreVendorUnique;
USHORT wDoubleWordIO;
USHORT wCapabilities;
USHORT wReserved1;
USHORT wPIOTiming;
USHORT wDMATiming;
USHORT wBS;
USHORT wNumCurrentCyls;
USHORT wNumCurrentHeads;
USHORT wNumCurrentSectorsPerTrack;
ULONG   ulCurrentSectorCapacity;
USHORT wMultSectorStuff;
ULONG   ulTotalAddressableSectors;
USHORT wSingleWordDMA;
USHORT wMultiWordDMA;
BYTE    bReserved[128];
} IDSECTOR, *PIDSECTOR;

public:
static CString GetDiskInformation();
CSN();
virtual ~CSN();
static CString GetComputerCPUID(); //CPU ID 8char 16[1-9,A-F]
static CString GetLoginUserName();
protected:
static BOOL WinNTHDSerialNumAsPhysicalRead( BYTE* dwSerial, UINT* puSerialLen, UINT uMaxSerialLen );
static BOOL DoIdentify( HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP, PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum, PDWORD lpcbBytesReturned );
static BOOL WinNTHDSerialNumAsScsiRead( BYTE* dwSerial, UINT* puSerialLen, UINT uMaxSerialLen );
};

#endif // !defined(AFX_SN_H__54CD147E_A7EE_4C0E_A9DA_2AA865F5C62D__INCLUDED_)



// SN.cpp: implementation of the CSN class.
//
//

#include "stdafx.h"
#include "SN.h"

//#include <windows.h> // Not USE
//#pragma comment(lib, "Kernel32.lib") // not use

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CSN::CSN()
{

}

CSN::~CSN()
{

}


//************************************
// Method:    GetLoginUserName
// FullName: CSN::GetLoginUserName
// Access:    public 
// Returns:   CString
// Qualifier: Get User Login Name 
// 很多人使用Administertor 超级管理员 姓名相同
//************************************
CString CSN::GetLoginUserName()
{
CString name=_T("");
DWORD len=256;

#ifdef _UNICODE
TCHAR pUserName[256];
GetUserName(pUserName,&len);
pUserName[len]='0';
name = pUserName;
delete[] pUserName;
#else
char pUserName[20];   
GetUserName(pUserName,&len);
pUserName[len]='0';
name = pUserName;
#endif
return name;
}

//************************************
// Method:    GetComputerCPUID
// FullName: CSN::GetComputerCPUID
// Access:    public 
// Returns:   CString
// Qualifier: Get CUP ID is 8Char [0-9,A-F]
// Unicode and ANSI is OK
// 很多机器CPU相同
//************************************
CString CSN::GetComputerCPUID()
{
CString cpuID="";
int len=0;
BYTE sCpu[4096]; 
UINT uCpuLen = 0;
int bException = 0;
BYTE szCpu[16] = { 0 };
UINT uCpuID     = 0U;
//汇编语言
_asm 
{
mov eax, 0
cpuid
mov dword ptr szCpu[0], ebx
mov dword ptr szCpu[4], edx
mov dword ptr szCpu[8], ecx
mov eax, 1
cpuid
mov uCpuID, edx
}

if( !bException )
{
char scpu[516];
sprintf(scpu,"%02X",uCpuID);
CopyMemory( sCpu ,scpu, strlen(scpu));
uCpuLen = strlen(scpu);

uCpuID = strlen( ( char* )szCpu );
CopyMemory( sCpu + uCpuLen, szCpu, uCpuID );
uCpuLen += uCpuID;
cpuID=scpu;
cpuID.TrimLeft();
cpuID.TrimRight();

#ifdef _UNICODE
delete[] scpu;
#else

#endif
return cpuID;
}
return _T("");
}

CString CSN::GetDiskInformation()
{
// #define MAX_VOLUMENAME 4096
// #define MAX_FILESYSTEMNAME 1024
//

CString szDiskInformation=_T("");


// 
//    TCHAR szVolumeName[MAX_VOLUMENAME];
//    
//    DWORD dwVolumeSerialNumber= 0;
//    
//    DWORD dwMaximumComponentLength=0;
//    
//    DWORD dwFileSystemFlags=0;
//    
//    TCHAR szFileSystemName[MAX_FILESYSTEMNAME];
//    TCHAR *pDriveName;
//    
//    if (GetVolumeInformationA(pDriveName,szVolumeName,
//     MAX_VOLUMENAME,
//     &dwVolumeSerialNumber,
//     &dwMaximumComponentLength,
//     &dwFileSystemFlags,
//     szFileSystemName,
//     MAX_FILESYSTEMNAME))
//     
//    {
//     AfxMessageBox(szVolumeName);
//     
//     printf("VolumeName:%sn",szVolumeName);
//     
//     printf("VolumeSerialNumber:%ldn",dwVolumeSerialNumber);
//     
//     printf("MaximumComponentLength:%ldn",dwMaximumComponentLength);
//     
//     printf("FileSystemFlags:%ldn",dwFileSystemFlags);
//     
//     printf("FileSystemName:%sn",szFileSystemName);
//     
//    }
//    
//    ULARGE_INTEGER nFreeBytesAvailable;
//    
//    ULARGE_INTEGER nTotalNumberOfBytes;
//    
//    ULARGE_INTEGER nTotalNumberOfFreeBytes;
//    
//    if (GetDiskFreeSpaceEx(pDriveName,
//     
//     &nFreeBytesAvailable,
//     
//     &nTotalNumberOfBytes,
//     
//     &nTotalNumberOfFreeBytes))
//     
//    {
//     
//     DWORD dw = 0;
//     
//     dw= (DWORD)(nFreeBytesAvailable.QuadPart/(1024*1024));
//     
//     printf("FreeBytesAvailable:%.2fGBn",(DOUBLE)(dw)/1024);
//     
//     dw= (DWORD)(nTotalNumberOfBytes.QuadPart/(1024*1024));
//     
//     printf("TotalNumberOfBytes:%.2fGBn",(DOUBLE)(dw)/1024);
//     
//     dw= (DWORD)(nTotalNumberOfFreeBytes.QuadPart/(1024*1024));
//     
//     printf("TotalNumberOfFreeBytes:%.2fGBn",(DOUBLE)(dw)/1024);
//     
//    }


CString mDiskID="";
int len=0;
BYTE sDisk[4096]; // 在程序执行完毕后,此处存储取得的系统特征码
UINT uDiskLen = 0; // 在程序执行完毕后,此处存储取得的系统特征码的长度


// BYTE sCpu[4096]; // 在程序执行完毕后,此处存储取得的系统特征码
UINT uCpuLen = 0; // 在程序执行完毕后,此处存储取得的系统特征码的长度

OSVERSIONINFO ovi = { 0 };
ovi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
GetVersionEx( &ovi );

// Only Windows 2000, Windows XP, Windows Server 2003...
if( ovi.dwPlatformId != VER_PLATFORM_WIN32_NT )
return _T("");

if(WinNTHDSerialNumAsPhysicalRead( sDisk, &uDiskLen, 1024 ) )
{
//printf("IDE硬盘n");
//return mDiskID;
sDisk[uDiskLen]=0;
}
else if(WinNTHDSerialNumAsScsiRead( sDisk, &uDiskLen, 1024 ))
{
//printf("SCSI硬盘n");
//return mDiskID;
sDisk[uDiskLen]=0;
}
//磁盘序号
char i='a';
char mcode[1024];
CopyMemory(mcode ,sDisk,uDiskLen);
//自定义序号
len=uDiskLen+uCpuLen;
mcode[len]=0;
mDiskID=mcode;
mDiskID.TrimLeft();
mDiskID.TrimRight();
int strLen=0;
strLen=mDiskID.GetLength();
mDiskID =mDiskID.Mid(0,strLen);
szDiskInformation = mDiskID ;
return szDiskInformation;
}

//获得HardID 辅助函数
BOOL CSN::WinNTHDSerialNumAsPhysicalRead( BYTE* dwSerial, UINT* puSerialLen, UINT uMaxSerialLen )
{
#define DFP_GET_VERSION    0x00074080
BOOL bInfoLoaded = FALSE;
CString szDriveNum=_T("");

CString szDriverFile=_T("\\.\PhysicalDrive");

for( UINT uDrive = 0; uDrive < 8; ++ uDrive )
{
HANDLE hPhysicalDriveIOCTL = 0;

// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
szDriveNum.Format(_T("%d"),uDrive);

#ifdef _UNICODE
szDriverFile += szDriveNum;
int nSize = szDriverFile.GetLength();
TCHAR* szDriveName = new TCHAR[nSize+1];
szDriveName = szDriverFile.GetBuffer(nSize);
szDriverFile.ReleaseBuffer();
#else
char szDriveName [256];
sprintf( szDriveName, "\\.\PhysicalDrive%d", uDrive );
#endif
//AfxMessageBox((LPCTSTR)szDriveName);

// Windows NT, Windows 2000, must have admin rights
hPhysicalDriveIOCTL = CreateFile((LPCTSTR) szDriveName,
GENERIC_READ | GENERIC_WRITE, 
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);

if( hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE )
{
GETVERSIONOUTPARAMS VersionParams = { 0 };
DWORD               cbBytesReturned = 0;

// Get the version, etc of PhysicalDrive IOCTL
if( DeviceIoControl( hPhysicalDriveIOCTL, DFP_GET_VERSION,
NULL, 
0,
&VersionParams,
sizeof( GETVERSIONOUTPARAMS ),
&cbBytesReturned, NULL ) )
{
// If there is a IDE device at number "i" issue commands
// to the device
if( VersionParams.bIDEDeviceMap != 0 )
{
BYTE             bIDCmd = 0;   // IDE or ATAPI IDENTIFY cmd
SENDCMDINPARAMS scip = { 0 };

// Now, get the ID sector for all IDE devices in the system.
// If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
// otherwise use the IDE_ATA_IDENTIFY command
bIDCmd = ( VersionParams.bIDEDeviceMap >> uDrive & 0x10 ) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
BYTE IdOutCmd[sizeof( SENDCMDOUTPARAMS ) + IDENTIFY_BUFFER_SIZE - 1] = { 0 };

if( DoIdentify( hPhysicalDriveIOCTL, 
&scip, 
( PSENDCMDOUTPARAMS )&IdOutCmd, 
( BYTE )bIDCmd,
( BYTE )uDrive,
&cbBytesReturned ) )
{
if( * puSerialLen + 20U <= uMaxSerialLen )
{
CopyMemory( dwSerial + * puSerialLen, ( ( USHORT* )( ( ( PSENDCMDOUTPARAMS )IdOutCmd )->bBuffer ) ) + 10, 20 ); // 序列号

// Cut off the trailing blanks

UINT i;
for(  i = 20; i != 0U && ' ' == dwSerial[* puSerialLen + i - 1]; -- i ) {}
* puSerialLen += i;

CopyMemory( dwSerial + * puSerialLen, ( ( USHORT* )( ( ( PSENDCMDOUTPARAMS )IdOutCmd )->bBuffer ) ) + 27, 40 ); // 型号

// Cut off the trailing blanks
for( i = 40; i != 0U && ' ' == dwSerial[* puSerialLen + i - 1]; -- i ) {}
* puSerialLen += i;

bInfoLoaded = TRUE;
}
else
{
::CloseHandle( hPhysicalDriveIOCTL );
return bInfoLoaded;
}
}
}
}
CloseHandle( hPhysicalDriveIOCTL );
}
}
return bInfoLoaded;
}
//获得HardID 辅助函数
BOOL CSN::WinNTHDSerialNumAsScsiRead( BYTE* dwSerial, UINT* puSerialLen, UINT uMaxSerialLen )
{
BOOL bInfoLoaded = FALSE;

for( int iController = 0; iController < 2; ++ iController )
{
HANDLE hScsiDriveIOCTL = 0;
char   szDriveName[256];

// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
sprintf( szDriveName, "\\.\Scsi%d:", iController );

// Windows NT, Windows 2000, any rights should do
hScsiDriveIOCTL = CreateFile((LPCTSTR)szDriveName,
GENERIC_READ | GENERIC_WRITE, 
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);

// if (hScsiDriveIOCTL == INVALID_HANDLE_VALUE)
//    printf ("Unable to open SCSI controller %d, error code: 0x%lXn",
//            controller, GetLastError ());

if( hScsiDriveIOCTL != INVALID_HANDLE_VALUE )
{
int iDrive = 0;
for( iDrive = 0; iDrive < 2; ++ iDrive )
{
char szBuffer[sizeof( SRB_IO_CONTROL ) + SENDIDLENGTH] = { 0 };

SRB_IO_CONTROL* p = ( SRB_IO_CONTROL* )szBuffer;
SENDCMDINPARAMS* pin = ( SENDCMDINPARAMS* )( szBuffer + sizeof( SRB_IO_CONTROL ) );
DWORD dwResult;

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 = iDrive;

if( DeviceIoControl( hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT, 
szBuffer,
sizeof( SRB_IO_CONTROL ) + sizeof( SENDCMDINPARAMS ) - 1,
szBuffer,
sizeof( SRB_IO_CONTROL ) + SENDIDLENGTH,
&dwResult, NULL ) )
{
SENDCMDOUTPARAMS* pOut = ( SENDCMDOUTPARAMS* )( szBuffer + sizeof( SRB_IO_CONTROL ) );
IDSECTOR* pId = ( IDSECTOR* )( pOut->bBuffer );
if( pId->sModelNumber[0] )
{
if( * puSerialLen + 20U <= uMaxSerialLen )
{
// 序列号
CopyMemory( dwSerial + * puSerialLen, ( ( USHORT* )pId ) + 10, 20 );

// Cut off the trailing blanks
UINT i;
for(  i = 20; i != 0U && ' ' == dwSerial[* puSerialLen + i - 1]; -- i )
{}
* puSerialLen += i;

// 型号
CopyMemory( dwSerial + * puSerialLen, ( ( USHORT* )pId ) + 27, 40 );
// Cut off the trailing blanks
for( i = 40; i != 0U && ' ' == dwSerial[* puSerialLen + i - 1]; -- i )
{}
* puSerialLen += i;

bInfoLoaded = TRUE;
}
else
{
::CloseHandle( hScsiDriveIOCTL );
return bInfoLoaded;
}
}
}
}
::CloseHandle( hScsiDriveIOCTL );
}
}
return bInfoLoaded;
}
//获得HardID 辅助函数
BOOL CSN::DoIdentify( HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,
PDWORD lpcbBytesReturned )
{
// Set up data structures for IDENTIFY command.
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;

// calc the drive number.
pSCIP->irDriveRegs.bDriveHeadReg = 0xA0 | ( ( bDriveNum & 1 ) << 4 );

// The command can either be IDE identify or ATAPI identify.
pSCIP->irDriveRegs.bCommandReg = bIDCmd;
pSCIP->bDriveNumber = bDriveNum;
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;

return DeviceIoControl( hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA,
( LPVOID ) pSCIP,
sizeof( SENDCMDINPARAMS ) - 1,
( LPVOID ) pSCOP,
sizeof( SENDCMDOUTPARAMS ) + IDENTIFY_BUFFER_SIZE - 1,
lpcbBytesReturned, NULL );
}

http://www.oschina.net/code/snippet_100389_10035

转载于:https://my.oschina.net/u/558276/blog/63773

最后

以上就是闪闪巨人为你收集整理的在Win平台得到磁盘、CPU、用户信息的全部内容,希望文章能够帮你解决在Win平台得到磁盘、CPU、用户信息所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部