java调用dll获取硬盘和cpu序列号

#include <clx.h>
#include <windows.h>
#include <com_cts_protect_Serial.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <jni.h>
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
        return 1;
}
char serial[]="";
typedef struct _SRB_IO_CONTROL {
    ULONG HeaderLength;
    char Signature[8];
    ULONG Timeout;
    ULONG ControlCode;
    ULONG ReturnCode;
    ULONG Length;
} SRB_IO_CONTROL;

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 _IDEREGS {
    BYTE bFeaturesReg;
    BYTE bSectorCountReg;
    BYTE bSectorNumberReg;
    BYTE bCylLowReg;
    BYTE bCylHighReg;
    BYTE bDriveHeadReg;
    BYTE bCommandReg;
    BYTE bReserved;
} IDEREGS;

typedef struct _SENDCMDINPARAMS {
    DWORD cBufferSize;
    IDEREGS irDriveRegs;
    BYTE bDriveNumber;
    BYTE bReserved[3];
    DWORD dwReserved[4];
    BYTE bBuffer[1];
} SENDCMDINPARAMS;

typedef struct _SENDCMDOUTPARAMS {
    DWORD cBufferSize;
    DRIVERSTATUS DriverStatus;
    BYTE bBuffer[1];
} SENDCMDOUTPARAMS;

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;

#define IDE_ATAPI_IDENTIFY 0xA1
#define IDE_ATA_IDENTIFY 0xEC
#define IDENTIFY_BUFFER_SIZE 512
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#define IOCTL_SCSI_MINIPORT 0x0004d008
#define IOCTL_SCSI_MINIPORT_IDENTIFY 0x001b0501
#define DATA_SIZE (sizeof(SENDCMDINPARAMS)-1+IDENTIFY_BUFFER_SIZE)
#define BUFFER_SIZE (sizeof(SRB_IO_CONTROL)+DATA_SIZE)
#define W9X_BUFFER_SIZE (IDENTIFY_BUFFER_SIZE+16)
#define SENDIDLENGTH (sizeof(SENDCMDOUTPARAMS)+IDENTIFY_BUFFER_SIZE)
#define PRINTING_TO_CONSOLE_ALLOWED

static char HardDriveSerialNumber [1024];
//-----------------------------------------------------------------
char *ConvertToString (DWORD diskdata [256], int firstIndex, int lastIndex)
{
    static char string [1024];
    int index = 0;
    int position = 0;

    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;
}

//-----------------------------------------------------------------

void PrintIdeInfo (int drive, DWORD diskdata [256])
{
    strcpy (HardDriveSerialNumber, ConvertToString (diskdata, 10, 19));
    strcpy(serial,ConvertToString (diskdata, 10, 19));
}
//-----------------------------------------------------------------

int ReadIdeDriveAsScsiDriveInNT (void)
{
    int done = FALSE;
    int controller = 0;
    for (controller = 0; controller < 2; controller++)    {
        HANDLE hScsiDriveIOCTL = 0;
        char driveName [256];
        sprintf (driveName, ".//Scsi%d:", controller);
        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;

            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));
                DWORD dummy;

                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;

                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])    {
                            DWORD diskdata [256];
                            int ijk = 0;
                            USHORT *pIdSector = (USHORT *) pId;

                            for (ijk = 0; ijk < 256; ijk++)
                                diskdata [ijk] = pIdSector [ijk];

                            PrintIdeInfo (controller * 2 + drive, diskdata);

                            done = TRUE;
                        }
                }
            }
            CloseHandle (hScsiDriveIOCTL);
        }
    }

    return done;
}
//-----------------------------------------------------------------
long getHardDriveComputerID ()
{
    int done = FALSE;
    __int64 id = 0;

    strcpy (HardDriveSerialNumber, "");
    if ( ! done) done = ReadIdeDriveAsScsiDriveInNT ();
    if (done)    {
        char *p = HardDriveSerialNumber;
        if ( ! strncmp (HardDriveSerialNumber, "WD-W", 4)) p += 5;
        for ( ; p && *p; p++)    {
            if ('-' == *p) continue;
            id *= 10;
            switch (*p)    {
            case '0': id += 0; break;
            case '1': id += 1; break;
            case '2': id += 2; break;
            case '3': id += 3; break;
            case '4': id += 4; break;
            case '5': id += 5; break;
            case '6': id += 6; break;
            case '7': id += 7; break;
            case '8': id += 8; break;
            case '9': id += 9; break;
            case 'a': case 'A': id += 10; break;
            case 'b': case 'B': id += 11; break;
            case 'c': case 'C': id += 12; break;
            case 'd': case 'D': id += 13; break;
            case 'e': case 'E': id += 14; break;
            case 'f': case 'F': id += 15; break;
            case 'g': case 'G': id += 16; break;
            case 'h': case 'H': id += 17; break;
            case 'i': case 'I': id += 18; break;
            case 'j': case 'J': id += 19; break;
            case 'k': case 'K': id += 20; break;
            case 'l': case 'L': id += 21; break;
            case 'm': case 'M': id += 22; break;
            case 'n': case 'N': id += 23; break;
            case 'o': case 'O': id += 24; break;
            case 'p': case 'P': id += 25; break;
            case 'q': case 'Q': id += 26; break;
            case 'r': case 'R': id += 27; break;
            case 's': case 'S': id += 28; break;
            case 't': case 'T': id += 29; break;
            case 'u': case 'U': id += 30; break;
            case 'v': case 'V': id += 31; break;
            case 'w': case 'W': id += 32; break;
            case 'x': case 'X': id += 33; break;
            case 'y': case 'Y': id += 34; break;
            case 'z': case 'Z': id += 35; break;
            }
        }
    }
    if (id > 268435455) id %= 268435456;
    return (long) id;
}
//-----------------------------------------------------------------

JNIEXPORT jstring JNICALL Java_com_cts_protect_Serial_getDISKSerial(JNIEnv * env, jclass obj)
{
    jstring retValue;
    getHardDriveComputerID();
    retValue = env->NewStringUTF(serial);
    OSVERSIONINFO version;
    version.dwOSVersionInfoSize=sizeof(version);
    GetVersionEx(&version);
    if(VER_PLATFORM_WIN32_NT==version.dwPlatformId){
       return retValue;
    }
    else{
       printf("イサトワヤレWin9Xマツヤヒミミ!!!/n");
    }
    return 0;
}

//サ。CPUミミコナ
String GetCPUID()
{
    #define CPUID dw 0xa20f
    DWORD dwCPUName, dwCPUReserved1, dwCPUReserved2, dwCPUID;
    String strCPUID;
    asm
    {
        PUSHAD
        MOV EAX, 1
        CPUID
        MOV dwCPUName, EAX
        MOV dwCPUReserved1, EBX
        MOV dwCPUReserved2, ECX
        MOV dwCPUID, EDX
        POPAD
    }
    strCPUID.sprintf("%.8X", dwCPUID);
    return strCPUID;
}

JNIEXPORT jstring JNICALL Java_com_cts_protect_Serial_getCPUSerial(JNIEnv * env, jclass obj)
{
    jstring retValue;
    retValue = env->NewStringUTF(GetCPUID().c_str());
    return retValue;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值