GUID的結構:函數
typedef struct _GUID { unsigned long Data1; unsigned short Data2; unsigned short Data3; unsigned char Data4[ 8 ]; } GUID;
總大小爲:4 + 2 + 2 + 8 = 16字節。ui
一個GUID內容形式爲:{77046E66-C274-42CE-85AD-5DCFA5B4D2A5}spa
就是4+2+2+2+6 = 16字節。code
「85AD」 是 Data4[0]和Data4[1];blog
「5DCFA5B4D2A5」 則是從 Data4[2]~Data4[7]。get
須要包含頭文件objbase.hstring
主要就是函數it
HRESULT CoCreateGuid( GUID * pguid );
示例:class
#include "stdafx.h" #include "objbase.h" #include <string> using namespace std; std::string GuidToString(const GUID &guid) { char buf[64] = {0}; sprintf_s(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); } int _tmain(int argc, _TCHAR* argv[]) { GUID guid; CoCreateGuid(&guid); std::string s = GuidToString(guid); printf(s.c_str()); system("pause"); return 0; }
運行結果:gui