Posts Tagged ‘硬盘序列号’

C++获取硬盘信息(序列号)源码

星期五, 十二月 14th, 2012 519 views

代码是老外的,项目中需要用到所以临时找了很多版本,这是目前测试可用性最高的版本,所有代码都写进了一个CPP里
代码有点长,限于篇幅只传一部分吧,其他后面给压缩包。此处为获取硬盘信息的入口函数,感兴趣的朋友可以试着剥离出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
long getHardDriveComputerID ()
{
    int done = FALSE;
    // char string [1024];
    __int64 id = 0;
    OSVERSIONINFO version;

    strcpy (HardDriveSerialNumber, "");

    memset (&version, 0, sizeof (version));
    version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
    GetVersionEx (&version);
    if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        //  this works under WinNT4 or Win2K if you have admin rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using physical access with admin rights\n");
#endif
        done = ReadPhysicalDriveInNTWithAdminRights ();

        //  this should work in WinNT or Win2K if previous did not work
        //  this is kind of a backdoor via the SCSI mini port driver into
        //     the IDE drives
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using the SCSI back door\n");
#endif
        // if ( ! done)
        done = ReadIdeDriveAsScsiDriveInNT ();

        //  this works under WinNT4 or Win2K or WinXP if you have any rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using physical access with zero rights\n");
#endif
        //if ( ! done)
        done = ReadPhysicalDriveInNTWithZeroRights ();

        //  this works under WinNT4 or Win2K or WinXP or Windows Server 2003 or Vista if you have any rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using Smart\n");
#endif
        //if ( ! done)
        done = ReadPhysicalDriveInNTUsingSmart ();
    }
    else
    {
        //  this works under Win9X and calls a VXD
        int attempt = 0;

        //  try this up to 10 times to get a hard drive serial number
        for (attempt = 0;
            attempt < 10 && ! done && 0 == HardDriveSerialNumber [0];
            attempt++)
            done = ReadDrivePortsInWin9X ();
    }

    if (HardDriveSerialNumber [0] > 0)
    {
        char *p = HardDriveSerialNumber;

        WriteConstantString ("HardDriveSerialNumber", HardDriveSerialNumber);

        //  ignore first 5 characters from western digital hard drives if
        //  the first four characters are WD-W
        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;
            }                            
        }
    }

    id %= 100000000;
    if (strstr (HardDriveModelNumber, "IBM-"))
        id += 300000000;
    else if (strstr (HardDriveModelNumber, "MAXTOR") ||
        strstr (HardDriveModelNumber, "Maxtor"))
        id += 400000000;
    else if (strstr (HardDriveModelNumber, "WDC "))
        id += 500000000;
    else
        id += 600000000;

#ifdef PRINTING_TO_CONSOLE_ALLOWED

    printf ("\nHard Drive Serial Number__________: %s\n",
        HardDriveSerialNumber);
    printf ("\nHard Drive Model Number___________: %s\n",
        HardDriveModelNumber);
    printf ("\nComputer ID_______________________: %I64d\n", id);

#endif

    return (long) id;
}

具体就不分析了,有问题欢迎留言谈论,以下是效果截图

源码下载:
【Diskid32源码及演示下载】

C#获取硬盘序列号

星期二, 十二月 11th, 2012 108 views

本来准备直接用在U3D里的,但是无奈游戏程序一加载到该模块就完全崩溃,目前原因不明,有可能是该部分功能已经超越Unity的权限范围,不过独立的C#版本是可用的,这里先把代码发上来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Management;//导入此命名空间
/**
* 该函数返回存有硬盘序列号的字符串
**/

private string GetDiskSerial()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                String strHardDiskID = "nothing found!";
                foreach (ManagementObject mo in searcher.Get())
                {
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();
                    break;
                }
                return strHardDiskID;
            }
            catch
            {
                return "nothing found!";
            }
        }

基本原理就是调用API去与硬件底层交互
不过测试之后发现有些时候并不能获取到硬盘序列号
所以又有了第二种方法,代码如下
(更多…)