C++ 生成GUID
1//--生成GUID的代码
2//--相关程序/微软guidgen.exe
3
4#include "stdafx.h"
5#include <objbase.h>
6#include <stdio.h>
7
8//--生成GUID
9const char* newGUID()
10{
11 static char buf[64] = {0};
12 GUID guid;
13 if (S_OK == ::CoCreateGuid(&guid))
14 {
15 _snprintf(buf, sizeof(buf)
16 , "{%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}"
17 , guid.Data1
18 , guid.Data2
19 , guid.Data3
20 , guid.Data4[0], guid.Data4[1]
21 , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
22 , guid.Data4[6], guid.Data4[7]
23 );
24 }
25 return (const char*)buf;
26}
27
28int main(int argc, char* argv[])
29{
30 //--COM
31 CoInitialize(NULL);
32
33 printf(newGUID());
34 printf("\r\n");
35
36 //--COM
37 CoUninitialize();
38
39 return 0;
40}
41
42
2//--相关程序/微软guidgen.exe
3
4#include "stdafx.h"
5#include <objbase.h>
6#include <stdio.h>
7
8//--生成GUID
9const char* newGUID()
10{
11 static char buf[64] = {0};
12 GUID guid;
13 if (S_OK == ::CoCreateGuid(&guid))
14 {
15 _snprintf(buf, sizeof(buf)
16 , "{%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}"
17 , guid.Data1
18 , guid.Data2
19 , guid.Data3
20 , guid.Data4[0], guid.Data4[1]
21 , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
22 , guid.Data4[6], guid.Data4[7]
23 );
24 }
25 return (const char*)buf;
26}
27
28int main(int argc, char* argv[])
29{
30 //--COM
31 CoInitialize(NULL);
32
33 printf(newGUID());
34 printf("\r\n");
35
36 //--COM
37 CoUninitialize();
38
39 return 0;
40}
41
42