윈도우 uuid(guid) 생성

C++ 2012. 5. 20. 15:47



#pragma once


#include <string>

#include <iomanip>

#include <sstream>

using namespace std;


#include <strsafe.h>


// ex) E66B83DC-F191-406A-9F39-57FA19250F4E

string createUuidUsePrintf()

{

GUID guid;

if (CoCreateGuid(&guid) != S_OK) return "";


string guid_str(40, 0); // size : 40

StringCchPrintfA(&guid_str.at(0), guid_str.size(), 

"%.8X-%.4X-%.4X-%.2X%.2X-%.2X%.2X%.2X%.2X%.2X%.2X", 

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

}


// ex) E66B83DCF191406A9F3957FA19250F4E

string createUuid()

{

GUID guid;

if (CoCreateGuid(&guid) != S_OK) return "";


stringstream ss;

ss << hex << uppercase << setfill('0')

<< setw(8) << guid.Data1

<< setw(4) << guid.Data2

<< setw(4) << guid.Data3

<< setw(2) << (int)guid.Data4[0]

<< setw(2) << (int)guid.Data4[1]

<< setw(2) << (int)guid.Data4[2]

<< setw(2) << (int)guid.Data4[3]

<< setw(2) << (int)guid.Data4[4]

<< setw(2) << (int)guid.Data4[5]

<< setw(2) << (int)guid.Data4[6]

<< setw(2) << (int)guid.Data4[7];

return ss.str();

}

// ex) E66B83DC-F191-406A-9F39-57FA19250F4E

string createUuidDash()

{

GUID guid;

if (CoCreateGuid(&guid) != S_OK) return "";


stringstream ss;

ss << hex << uppercase << setfill('0')

<< setw(8) << guid.Data1 << "-"

<< setw(4) << guid.Data2 << "-"

<< setw(4) << guid.Data3 << "-"

<< setw(2) << (int)guid.Data4[0]

<< setw(2) << (int)guid.Data4[1] << "-"

<< setw(2) << (int)guid.Data4[2]

<< setw(2) << (int)guid.Data4[3]

<< setw(2) << (int)guid.Data4[4]

<< setw(2) << (int)guid.Data4[5]

<< setw(2) << (int)guid.Data4[6]

<< setw(2) << (int)guid.Data4[7];

return ss.str();

}

// ex) {E66B83DC-F191-406A-9F39-57FA19250F4E}

string createUuidBraceDash()

{

return "{" + createUuidDash() + "}";

}


'C++' 카테고리의 다른 글

간단한 sync 소켓 사용  (0) 2012.05.20
WinINet, WinHTTP, post 전송  (0) 2012.05.20
간단한 유틸(util) 함수들  (0) 2012.05.20
간단한 file 조작  (0) 2012.05.20
std::string 문자열 조작  (0) 2012.05.20
url encode  (0) 2012.05.04
컴파일시 해야할 일 표시하기 #pragma message 사용  (0) 2012.04.15
ado 하위 OS 호환 컴파일 - E_NOINTERFACE (0x80004002)  (0) 2012.04.13