1. 程式人生 > >獲取硬體UUID方法(windows、linux)

獲取硬體UUID方法(windows、linux)

1、命令獲取uuid
1.1、windows系統中獲取裝置的UUID的方法:
在命令提示符下輸入wmic 再輸入csproduct 或 csproduct list full
wmic:rootcli>csproduct list full
Description=計算機系統產品
IdentifyingNumber=*****
Name=*****
SKUNumber=
UUID=81AC7607-1E47-CB11-BC6A-8C7EF84170A7
Vendor=IBM
Version=ThinkPad T42

1.2、Linux系統中獲取裝置的UUID的方法:
1)# blkid /dev/sda1
/dev/sda1: LABEL="/axs3" UUID="298d198d-aa60-48af-a9f4-638f8f274afa" SEC_TYPE="ext2" TYPE="ext3"
2) # tune2fs -l /dev/sda1 |grep 'UUID'
298d198d-aa60-48af-a9f4-638f8f274afa
3)# ls -l /dev/disk/by-uuid/ |grep sda1 |awk '{print $8}'
298d198d-aa60-48af-a9f4-638f8f274afa
4)#scsi_id -p 0x80/0x83 -s /block/sda1   應該只對SCSI裝置有效。
5)# dumpe2fs /dev/sda1 |grep 'UUID'
dumpe2fs 1.39 (29-May-2006)
Filesystem UUID:       298d198d-aa60-48af-a9f4-638f8f274afa
這個命令不建議使用,要是分割槽比較大,耗時還是比較長的
6)# vol_id /dev/sda1 |grep 'UUID'
ID_FS_UUID=298d198d-aa60-48af-a9f4-638f8f274afa
ID_FS_UUID_ENC=298d198d-aa60-48af-a9f4-638f8f274afa

2、程式碼生成UUID/GUID(inux & Windows)
請見如下相關文章:
boost uuid: http://blog.csdn.net/ghlfllz/article/details/16881179
uuid in linux vs uuid in window[Parser]: http://http//blog.csdn.net/ghlfllz/article/details/6876181
各種OS中生成UUID的方法: http://linfan.info/blog/2012/04/20/uuid/

GUID是微軟對Distributed coumputing environment (DCE) universally unique identifier 的實現,而在Linux下則稱作UUID。

通用的GUID的結構如下
typedef struct _GUID {
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} GUID;
舉一個例子:
假設一個GUID的格式是這樣的 6B29FC40-CA47-1067-B31D-00DD010662DA
其中Data1 是32位,可以看做8個四位十六進位制數,對應於上面的6B29FC40
其中Data2 是16位,可以看做4個四位十六進位制數,對應於上面的CA47
其中Data3 是16位,可以看做4個四位十六進位制數,對應於上面的1067
其中Data4 比較特殊,是8個位元組也就可以看做16個四位十六進位制數
      取其Data4[0],Data4[1]來組成4個四位十六進位制數,對應於上面的B31D
      取其Data4[2],Data4[3]來組成4個四位十六進位制數,對應於上面的00DD
      取其Data4[4],Data4[5]來組成4個四位十六進位制數,對應於上面的0106
      取其Data4[6],Data4[7]來組成4個四位十六進位制數,對應於上面的62DA
*注意:四位十六進位制數對應一個GUID字元。
具體程式碼:
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;

#ifdef WIN32
#include <objbase.h>
#else
#include <uuid/uuid.h>
#endif

GUID CreateGuid()
{
    GUID guid;
#ifdef WIN32
    CoCreateGuid(&guid);
#else
    uuid_generate(reinterpret_cast<unsigned char *>(&guid));
#endif
    return guid;
}

std::string GuidToString(const GUID &guid)
{
    char buf[64] = {0};
#ifdef __GNUC__
    snprintf(
#else // MSVC
    _snprintf_s(
#endif
                buf,
                sizeof(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]);
        return std::string(buf);
}
請注意:
windows下需要引用 ole32.lib
在linux下編譯時需要連結uuid庫(-luuid)

3、參考文章

硬碟分割槽的UUID(推薦先看該文章)
http://blog.csdn.net/smstong/article/details/46417213
windows 7/2008 檢視uuid
http://blog.sina.com.cn/s/blog_483180590100zhn5.html
裝置的UUID詳解
http://www.51testing.com/html/38/225738-241247.html
Linux & Windows上生成UUID/GUID
http://blog.csdn.net/ghlfllz/article/details/17128393
UUID詳解
http://blog.chinaunix.net/uid-26495963-id-3150576.html