电脑 硬件特征码读取 C++(序列号加密)

    本文地址:http://tongxinmao.com/Article/Detail/id/279

    /**
    
      The MAC addresses of ethernet network cards present on computer
    
      @param[out] vMAC vector of strings containing MAC addresses in XX-XX-XX-XX-XX-XX format
    
      returns empty vector on error
    
      See discussion of this 
      http://stackoverflow.com/questions/6131123/generating-hardware-based-computerid/6131231#6131231
    
    */
    
    void cLicenser::GetMac( vector < string >& vMac )
    {
        vMac.clear();
        char data[4096];
        ZeroMemory( data, 4096 );
         unsigned long  len = 4000;
        PIP_ADAPTER_INFO pinfo = ( PIP_ADAPTER_INFO ) data;
    
        DWORD ret = GetAdaptersInfo( pinfo, &len );
        if( ret != ERROR_SUCCESS )
            return;
    
        while ( pinfo ) {
        if( pinfo->Type == MIB_IF_TYPE_ETHERNET ) (
                // ignore software loopbacks and other strange things that might be present
            continue;
            char sbuf[20];
            string sret;
            for(int k = 0; k < 5; k++ ) {
                sprintf(sbuf,"%02X-",pinfo->Address[k]);
                sret += sbuf;
            }
            sprintf(sbuf,"%02X",pinfo->Address[5]);
                sret += sbuf;
            vMac.push_back( sret );
                }
            pinfo = pinfo->Next;
        }
    
    }


    //读般硬盘序列号函数

    char* CGetHDSerial::GetHDSerial()

    {

    m_buffer[0]='\n';

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

    OSVERSIONINFO OSVersionInfo;

    OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 

    GetVersionEx(&OSVersionInfo);

    if(OSVersionInfo.dwPlatformld != VER_PLATFORM_WIN32_NT)

    {

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

    WORD m_wWin9xl'HDSerial[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;

    }

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

    BOOL CGetHDScriaI::WinNTReadIDEHDSerial(DWORD* buffer)

    BYTE JdOutCmd [sizeof(SENDCMDOUTPARAMS)+IDFNTIFY_BUFFER_SIZE - 1];

    BOOL bFlag = FALSE;

    int drive = 0;

    Char driveName[256];

    HANDLE hPhysicalDrivelOCTL=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;

    //得到驱动器的I/O控制器版本

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

    if(DeviceIoControl(hPhysicalDriveIOCTL,IOCTL_GET_VERSION,NULL,0,&VersionPaiams,sizeof(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(hPhysicalDrivelOCTL,&scip,(PSENDCMDOUTPARMS)&IdOutCmd,(BYTE)bIDCmd,(BYTE)drive,&cbBytesReturned))

    {

    int m=0;

    USHORT*pldSector=(USHORT*)((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer;

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

    buffer[m]=pIdSector[m];

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

    }

    }

    }

    CloseHandle(hPhysicalDriyeIOCTL); //关闭句柄

    }

    Return bFlag;

    }


    上一篇:USB打印机识别区分
    下一篇:LINUX硬件相关命令工具