您的位置:首页 > 产品设计 > UI/UE

获取本机MAC地址,创建GUID,硬盘序列号,vs2013代码通过验证

2015-11-25 15:40 736 查看
  本文章中的代码段均经过验证。本人才拙,欢迎大家提意见。

一、获取本机MAC地址

#include <winsock2.h>

#include <Iphlpapi.h>

#include <stdio.h>

#pragma comment(lib,"Iphlpapi")

//#pragma warning(disable: 4996)  //提示C4996错误时用到

void byte2Hex(unsigned char bData, unsigned char hex[])

{

    int high = bData / 16, low = bData % 16;

    hex[0] = (high < 10) ? ('0' + high) : ('A' + high - 10);

    hex[1] = (low < 10) ? ('0' + low) : ('A' + low - 10);

}

int getLocalMacInfo(unsigned char *mac) //获取本机MAC址

{

    ULONG ulSize = 0;

    PIP_ADAPTER_INFO pInfo = NULL;

    int temp = 0;

    temp = GetAdaptersInfo(pInfo, &ulSize);//第一处调用,获取缓冲区大小

    pInfo = (PIP_ADAPTER_INFO)malloc(ulSize);

    temp = GetAdaptersInfo(pInfo, &ulSize);

    int iCount = 0;

    while (pInfo)//遍历每一张网卡

    {

        //  pInfo->Address MAC址

        for (int i = 0; i < (int)pInfo->AddressLength; i++)

        {

            byte2Hex(pInfo->Address[i], &mac[iCount]);

            iCount += 2;

            if (i < (int)pInfo->AddressLength - 1)

            {

                mac[iCount++] = '-';

            }

            else

            {

                mac[iCount++] = '#';

            }

        }

        pInfo = pInfo->Next;

    }

    if (iCount > 0)

    {

        mac[--iCount] = '\0';

        return iCount;

    }

    else return -1;

}

二、创建GUID

#include <winsock2.h>

#include <stdio.h>
void GetGUIDInfo(char* guidStr){

    static char buf[64] = { 0 };

    GUID guid;

    if (S_OK == ::CoCreateGuid(&guid))

    {

        sprintf(buf,

            "{%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}"

            ,guid.Data1

            ,guid.Data2

            ,guid.Data3

            ,guid.Data4[0], guid.Data4[1]

            ,guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5],guid.Data4[6], guid.Data4[7]

            );

    }

    strcpy(guidStr, buf);

}

三、获取电脑硬盘序列号

校验方法:对比QQ电脑管家 -工具箱-硬件检测-硬盘信息右侧的硬盘序列号

头文件:GetDiskSN.h

#pragma once

#include <windows.h>

#include <tchar.h>  

#include <stdio.h>  

#include <Iphlpapi.h>

#pragma comment(lib,"Iphlpapi")

class CGetDiskSN

{

public:

    CGetDiskSN();

    ~CGetDiskSN();

public:

#define DFP_GET_VERSION            0x00074080

#define DFP_SEND_DRIVE_COMMAND    0x0007c084

#define DFP_RECEIVE_DRIVE_DATA    0x0007c088

    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 _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.

    } SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;

    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[512]; // Buffer of arbitrary length

        // in which to store the data read from the drive.

    } SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;

    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:

    void ChangeByteOrder(PCHAR szString, USHORT uscStrSize);

    BOOL CGetDiskSN::GetHDDSerial(char *lpszHD, int len=128);

private:

};

GetDiskSN.cpp文件

#include"GetDiskSN.h"

CGetDiskSN::CGetDiskSN(){

}

CGetDiskSN::~CGetDiskSN(){

}

BOOL CGetDiskSN::GetHDDSerial(char *lpszHD, int len/*=128*/)

{

    BOOL        bRtn = FALSE;

    DWORD        bytesRtn = 0;

    char        szhd[80] = { 0 };

    PIDSECTOR    phdinfo;

    HANDLE        hDrive = NULL;

    GETVERSIONOUTPARAMS vers;

    SENDCMDINPARAMS        in;

    SENDCMDOUTPARAMS    out;

    ZeroMemory(&vers, sizeof(vers));

    ZeroMemory(&in, sizeof(in));

    ZeroMemory(&out, sizeof(out));

    //搜索四个物理硬盘,取第一个有数据的物理硬盘

    for (int j = 0; j < 4; j++)

    {

        sprintf(szhd, "\\\\.\\PhysicalDrive%d", j);

        hDrive = CreateFileA(szhd,

            GENERIC_READ | GENERIC_WRITE,

            FILE_SHARE_READ | FILE_SHARE_WRITE,

            0,

            OPEN_EXISTING,

            0,

            0);

        if (NULL == hDrive)
abfd

        {

            continue;

        }

        if (!DeviceIoControl(hDrive, DFP_GET_VERSION, 0, 0, &vers, sizeof(vers), &bytesRtn, 0))

        {

            goto FOREND;

        }

        //If IDE identify command not supported, fails

        if (!(vers.fCapabilities & 1))

        {

            goto FOREND;

        }

        //Identify the IDE drives

        if (j & 1)

        {

            in.irDriveRegs.bDriveHeadReg = 0xb0;

        }

        else

        {

            in.irDriveRegs.bDriveHeadReg = 0xa0;

        }

        if (vers.fCapabilities&(16 >> j))

        {

            //We don't detect a ATAPI device.

            goto FOREND;

        }

        else

        {

            in.irDriveRegs.bCommandReg = 0xec;

        }

        in.bDriveNumber = j;

        in.irDriveRegs.bSectorCountReg = 1;

        in.irDriveRegs.bSectorNumberReg = 1;

        in.cBufferSize = 512;

        if (!DeviceIoControl(hDrive, DFP_RECEIVE_DRIVE_DATA, &in, sizeof(in), &out, sizeof(out), &bytesRtn, 0))

        {

            //"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;

            goto FOREND;

        }

        phdinfo = (PIDSECTOR)out.bBuffer;

        char    s[21] = { 0 };

        memcpy(s, phdinfo->sSerialNumber, 20);

        s[20] = 0;

        ChangeByteOrder(s, 20);

        //删除空格字符

        int ix = 0;

        for (ix = 0; ix < 20; ix++)

        {

            if (s[ix] == ' ')

            {

                continue;

            }

            break;

        }

        memcpy(lpszHD, s + ix, 20);

        bRtn = TRUE;

        break;

    FOREND:

        CloseHandle(hDrive);

        hDrive = NULL;

    }

    CloseHandle(hDrive);

    hDrive = NULL;

    return(bRtn);

}

void CGetDiskSN::ChangeByteOrder(PCHAR szString, USHORT uscStrSize)

{

    USHORT  i = 0;

    CHAR    temp = '\0';

    for (i = 0; i < uscStrSize; i += 2)

    {

        temp = szString[i];

        szString[i] = szString[i + 1];

        szString[i + 1] = temp;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息