-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_win32.c
45 lines (33 loc) · 958 Bytes
/
uuid_win32.c
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
//#pragma comment(lib, "crypt32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincrypt.h>
#include "../src/uuid/uuid4.h"
bool win32RandomFiller(void *uuidData, size_t uuidLength) {
HCRYPTPROV hCryptProv;
if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
return false;
}
if(!CryptGenRandom(hCryptProv, uuidLength, uuidData)) {
CryptReleaseContext(hCryptProv, 0);
return false;
}
CryptReleaseContext(hCryptProv, 0);
return true;
}
int main() {
// Creating a new random UUID4.
UUID4 *uuid4 = uuid4_generate(&win32RandomFiller);
// Converting the UUID4 to a `char` string.
// The `uuid_toStringW` function can be used to get a `wchar_t` string.
char *uuidStr = uuid_toStringA(uuid4);
wchar_t *uuidStrW = uuid_toStringW(uuid4);
// Printing it out.
printf("%s\n", uuidStr);
printf("%ls\n", uuidStrW);
// Freeing resources.
free(uuidStrW);
free(uuidStr);
free(uuid4);
}