⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.h

📁 通用注册机源程序
💻 H
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TLabel *Label1;
        TEdit *Edit1;
        TLabel *Label2;
        TEdit *Edit2;
        TButton *Button1;
        TButton *Button2;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
//////////转换成数字
char ConvertTable[]="0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+";
int GetIndex(char n)
{
 int i = -1;
 ConvertTable[64] = n;
 while(ConvertTable[++i]!=n);
 return i>63 ? 63 : i;
}
//---------------------------------------------------------------------------
#define f_rnd(i,a,b,c,d)                \
        u = _lrotl(d*(d+d+1), 5);       \
        t = _lrotl(b*(b+b+1), 5);       \
        a = _lrotl(a^t, u)+l_key[i];    \
        c = _lrotl(c^u, t)+l_key[i+1]

unsigned long l_key[44];
//---------------------------------------------------------------------------
//////////设置注册序列
void set_key(const unsigned char *key)
{
 unsigned long i, j, k, a, b, l[8];

 unsigned long in_key[256];
 j = 0;
 k = 32;
 for(i=0; i<256; i++)
    in_key[i] = 0;
 for(i=strlen(key); i>0; i--) {
    if(k >= 32) {
       k = 0;
       in_key[j] = 0;
       j++;
       }
    in_key[j-1] |= (key[i-1]<<k);
    k += 8;
    }

 l_key[0] = 0xb7e15163;
 for(k=1; k<44; k++)
    l_key[k] = l_key[k-1]+0x9e3779b9;
 for(k=0; k<8; k++)
    l[k] = in_key[k];
 a = b = i = j = 0;
 for(k=0; k<132; k++) {
    a = _lrotl(l_key[i]+a+b, 3); b += a;
    b = _lrotl(l[j]+b, b);
    l_key[i] = a; l[j] = b;
    i = (i==43 ? 0 : i+1);
    j = (j==7 ? 0 : j+1);
    }
}
//---------------------------------------------------------------------------
#include <WinIOCtl.h>
#include <stdio.h>

#pragma inline
//---------------------------------------------------------------------------
// IDE NT/2000/XP专用变量
#define GETVERSIONOUTPARAMS     GETVERSIONINPARAMS
#define DFP_GET_VERSION         SMART_GET_VERSION
#define DFP_SEND_DRIVE_COMMAND  SMART_SEND_DRIVE_COMMAND
#define DFP_RCV_DRIVE_DATA      SMART_RCV_DRIVE_DATA

const WORD IDE_ATAPI_IDENTIFY = 0xA1;   // 读取ATAPI设备的命令
const WORD IDE_ATA_IDENTIFY   = 0xEC;   // 读取ATA设备的命令

const int MAX_IDE_DRIVES = 4;

// SCSI专用变量
const DWORD FILE_DEVICE_SCSI             = 0x0000001B;
const DWORD IOCTL_SCSI_MINIPORT_IDENTIFY = ((FILE_DEVICE_SCSI << 16) + 0x0501);
const DWORD IOCTL_SCSI_MINIPORT          = 0x0004D008; // see NTDDSCSI.H for definition
const DWORD SENDIDLENGTH  = sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE;

typedef struct _SRB_IO_CONTROL
{
    ULONG HeaderLength;
    UCHAR Signature[8];
    ULONG Timeout;
    ULONG ControlCode;
    ULONG ReturnCode;
    ULONG Length;
}SRB_IO_CONTROL, *PSRB_IO_CONTROL;
//---------------------------------------------------------------------------
// 读取的主函数
void __fastcall ReadPhysicalDrive(TStrings *pSerList, TStrings *pModeList);
// 辅助函数
char *__fastcall ConvertToString(DWORD dwDiskData[256], int nFirstIndex, int nLastIndex);
// NT/2000/XP函数
void __fastcall ReadPhysicalDriveOnNT(TStrings *pSerList, TStrings *pModeList);
bool __fastcall DoIdentify(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP, PSENDCMDOUTPARAMS pSCOP, BYTE btIDCmd, BYTE btDriveNum, PDWORD lpcbBYTEsReturned);
// Windows 9X函数
void __fastcall ReadPhysicalDriveOnW9X(TStrings *pSerList, TStrings *pModeList);
void __fastcall ReadPhysicalDriveOnW9X_Ring0(bool IsFirst, WORD BaseAddress, BYTE MoS, bool &IsIDEExist, bool &IsDiskExist, WORD *OutData);
// SCSI读取函数(for NT/2000/XP)
String __fastcall ReadIDEDriveAsScsiDriveOnNT();
//---------------------------------------------------------------------------
// ReadPhysicalDrive
void __fastcall ReadPhysicalDrive(TStrings *pSerList, TStrings *pModeList)
{
    switch(Win32Platform)
    {
        case VER_PLATFORM_WIN32_WINDOWS:
            ReadPhysicalDriveOnW9X(pSerList, pModeList);
            break;
        case VER_PLATFORM_WIN32_NT:
            ReadPhysicalDriveOnNT(pSerList, pModeList);
            break;
        default:
            break;
    }
}
//---------------------------------------------------------------------------
// ConvertToString
char *__fastcall ConvertToString(DWORD dwDiskData[256], int nFirstIndex, int nLastIndex)
{
    static char szResBuf[1024];
    int nIndex;
    int nPosition = 0;

    // Each integer has two characters stored in it backwards
    for(nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex++)
    {
        // Get high BYTE for 1st character
        szResBuf[nPosition] = (char)(dwDiskData[nIndex] / 256);
        nPosition++;

        // Get low BYTE for 2nd character
        szResBuf[nPosition] = (char)(dwDiskData[nIndex] % 256);
        nPosition++;
    }

    // End the string
    szResBuf[nPosition] = '\0';

    // Cut off the trailing blanks
    for(nIndex = nPosition - 1; nIndex > 0 && ' ' == szResBuf[nIndex]; nIndex--)
        szResBuf[nIndex] = '\0';

    return szResBuf;
}
//---------------------------------------------------------------------------
// Winndows NT4/2000/XP 代码
void __fastcall ReadPhysicalDriveOnNT(TStrings *pSerList, TStrings *pModeList)
{
    // 输出参数
    BYTE btIDOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];

    for(int nDrive=0; nDrive < MAX_IDE_DRIVES; nDrive++)
    {
        HANDLE hPhysicalDriveIOCTL;
        char szDriveName[32];

        sprintf(szDriveName, "\\\\.\\PhysicalDrive%d", nDrive);
        hPhysicalDriveIOCTL = CreateFile(szDriveName,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                        OPEN_EXISTING, 0, NULL);

        if(hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
        {
            DWORD dwBytesReturned = 0;
            GETVERSIONOUTPARAMS gvopVersionParams;

            // Get the version, etc of PhysicalDrive IOCTL
            ZeroMemory(&gvopVersionParams, sizeof(GETVERSIONOUTPARAMS));

            if(!DeviceIoControl(hPhysicalDriveIOCTL, DFP_GET_VERSION,
                    NULL, 0, &gvopVersionParams, sizeof(gvopVersionParams),
                    &dwBytesReturned, NULL))
            {
                continue;
            }

            if(gvopVersionParams.bIDEDeviceMap > 0)
            {
                // IDE or ATAPI IDENTIFY cmd
                BYTE btIDCmd;
                SENDCMDINPARAMS InParams;
                // 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
                // 具体所得结果请参考头文件中的说明
                btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ?
                        IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
                ZeroMemory(&InParams, sizeof(SENDCMDINPARAMS));
                ZeroMemory(btIDOutCmd, sizeof(btIDOutCmd));

                if(DoIdentify(hPhysicalDriveIOCTL,
                     &InParams, (PSENDCMDOUTPARAMS)btIDOutCmd,
                     (BYTE)btIDCmd, (BYTE)nDrive, &dwBytesReturned))
                {
                    DWORD dwDiskData[256];
                    USHORT *pIDSector; // 对应结构IDSECTOR,见头文件
                    char szSerialNumber[21];
                    char szModelNumber[41];

                    pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer;
                    for(int i=0; i < 256; i++)
                        dwDiskData[i] = pIDSector[i];
                    // 取系列号
                    ZeroMemory(szSerialNumber, sizeof(szSerialNumber));
                    strcpy(szSerialNumber, ConvertToString(dwDiskData, 10, 19));

                    // 取模型号
                    ZeroMemory(szModelNumber, sizeof(szModelNumber));
                    strcpy(szModelNumber, ConvertToString(dwDiskData, 27, 46));

                    pSerList->Add(szSerialNumber);
                    pModeList->Add(szModelNumber);
                }
            }
            CloseHandle (hPhysicalDriveIOCTL);
        }
    }
}
//---------------------------------------------------------------------------
// DoIdentify
bool __fastcall DoIdentify(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,
              PSENDCMDOUTPARAMS pSCOP, BYTE btIDCmd, BYTE btDriveNum,
              PDWORD pdwBytesReturned)
{
    // 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;

    // Compute the drive number.(主盘和从盘所对应的值是不一样的)
    pSCIP->irDriveRegs.bDriveHeadReg = (btDriveNum & 1) ? 0xB0 : 0xA0;

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

    return DeviceIoControl(hPhysicalDriveIOCTL, DFP_RCV_DRIVE_DATA,
           (LPVOID)pSCIP,
           sizeof(SENDCMDINPARAMS) - 1,
           (LPVOID)pSCOP,
           sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
           pdwBytesReturned, NULL);
}
//---------------------------------------------------------------------------
// Windows 95/98/ME 代码
//---------------------------------------------------------------------------
// ReadPhysicalDriveOnW9X
void __fastcall ReadPhysicalDriveOnW9X(TStrings *pSerList, TStrings *pModeList)
{
    WORD wOutData[256];
    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);

    // 经过测试,发现第一次调用而且Drive >= 2时会在Ring0代码中出现错误,导致蓝屏。
    // 经过N(N > 15)次的蓝屏后仍找不到原因:(不得不在这里增加一段无用代码以
    // 避免蓝屏的出现。(期待高人能指出原因)
    for(int nDrive = 0; nDrive < 8; nDrive++)
    {
        WORD dwBaseAddress;
        BYTE btMasterSlave;         // Master Or Slave
        bool bIsIDEExist;
        bool IsDiskExist;

        switch(nDrive / 2)
        {
            case 0: dwBaseAddress = 0x01F0; break;
            case 1: dwBaseAddress = 0x0170; break;
            case 2: dwBaseAddress = 0x01E8; break;
            case 3: dwBaseAddress = 0x0168; break;
        }

        btMasterSlave = (BYTE)(((nDrive % 2) == 0) ? 0xA0 : 0xB0);

        // 进入Ring0
        ReadPhysicalDriveOnW9X_Ring0(true, dwBaseAddress, btMasterSlave,
                bIsIDEExist, IsDiskExist, wOutData);
    }

    // 开始读取
    for(int nDrive = 0; nDrive < 8; nDrive++)
    {
        WORD dwBaseAddress;
        BYTE btMasterSlave;         // Master Or Slave
        bool bIsIDEExist;
        bool bIsDiskExist;
        switch(nDrive / 2)
        {
            case 0: dwBaseAddress = 0x01F0; break;
            case 1: dwBaseAddress = 0x0170; break;
            case 2: dwBaseAddress = 0x01E8; break;
            case 3: dwBaseAddress = 0x0168; break;
        }

        btMasterSlave = (BYTE)(((nDrive % 2) == 0) ? 0xA0 : 0xB0);

        // 进入Ring0
        bIsIDEExist  = false;
        bIsDiskExist = false;
        ZeroMemory(wOutData, sizeof(wOutData));

        ReadPhysicalDriveOnW9X_Ring0(false, dwBaseAddress, btMasterSlave,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -