forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateUUIDString.hpp
123 lines (106 loc) · 3.21 KB
/
GenerateUUIDString.hpp
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
#pragma once
//---------------------------------------------------------------------------//
//
// レジストリ形式の UUID 文字列を生成するユーティリティ関数
// GenerateUUIDString.hpp
// Copyright (C) 2013-2016 tapetums
//
//---------------------------------------------------------------------------//
#include <windows.h>
#include <strsafe.h>
//---------------------------------------------------------------------------//
namespace tapetums
{
inline void GenerateUUIDStringA(char* buf, size_t cch_buf);
inline void GenerateUUIDStringW(wchar_t* buf, size_t cch_buf);
}
#if defined(_UNICODE) || defined(UNICODE)
#define GenerateUUIDString GenerateUUIDStringW
#else
#define GenerateUUIDString GenerateUUIDStringA
#endif
//---------------------------------------------------------------------------//
inline void tapetums::GenerateUUIDStringA(char* buf, size_t cch_buf)
{
if ( cch_buf >= 38 + 1 )
{
struct REG_UUID
{
UINT32 data1;
UINT16 data2;
UINT16 data3;
UINT16 data4;
UINT8 data5[6];
} uuid;
::CoCreateGuid((GUID*)&uuid);
::StringCchPrintfA
(
buf, cch_buf,
"{%08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X}",
uuid.data1, uuid.data2, uuid.data3, uuid.data4,
uuid.data5[0], uuid.data5[1], uuid.data5[2],
uuid.data5[3], uuid.data5[4], uuid.data5[5]
);
}
else if ( cch_buf >= 32 + 1 )
{
GUID guid;
::CoCreateGuid((GUID*)&guid);
::StringCchPrintfA
(
buf, cch_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]
);
}
else
{
buf[0] = L'\0';
}
}
//---------------------------------------------------------------------------//
inline void tapetums::GenerateUUIDStringW(wchar_t* buf, size_t cch_buf)
{
if ( cch_buf >= 38 + 1 )
{
struct REG_UUID
{
UINT32 data1;
UINT16 data2;
UINT16 data3;
UINT16 data4;
UINT8 data5[6];
} uuid;
::CoCreateGuid((GUID*)&uuid);
::StringCchPrintfW
(
buf, cch_buf,
L"{%08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X}",
uuid.data1, uuid.data2, uuid.data3, uuid.data4,
uuid.data5[0], uuid.data5[1], uuid.data5[2],
uuid.data5[3], uuid.data5[4], uuid.data5[5]
);
}
else if ( cch_buf >= 32 + 1 )
{
GUID guid;
::CoCreateGuid((GUID*)&guid);
::StringCchPrintfW
(
buf, cch_buf,
L"%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]
);
}
else
{
buf[0] = L'\0';
}
}
//---------------------------------------------------------------------------//
// GenerateUUIDString.hpp