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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/platform/windows/win32registry.h"
#include "gate/results.h"

#if defined(GATE_SYS_WIN)


static HKEY gate_win32_registry_get_root(int root)
{
    switch (root)
    {
    case GATE_WIN32_REGISTRY_CLASSES:		return HKEY_CLASSES_ROOT;
#ifdef HKEY_CURRENT_CONFIG
    case GATE_WIN32_REGISTRY_CURRENTCONFIG:	return HKEY_CURRENT_CONFIG;<--- Skipping configuration 'GATE_SYS_WIN;HKEY_CURRENT_CONFIG' since the value of 'HKEY_CURRENT_CONFIG' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
    case GATE_WIN32_REGISTRY_CURRENTUSER:	return HKEY_CURRENT_USER;
    case GATE_WIN32_REGISTRY_LOCALMACHINE:	return HKEY_LOCAL_MACHINE;
    case GATE_WIN32_REGISTRY_USERS:			return HKEY_USERS;
#ifdef HKEY_DYN_DATA
    case GATE_WIN32_REGISTRY_DYNDATA:		return HKEY_DYN_DATA;<--- Skipping configuration 'GATE_SYS_WIN;HKEY_DYN_DATA' since the value of 'HKEY_DYN_DATA' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
    }
    return NULL;
}

#if defined(GATE_SYS_WINCE)
#	define has_regfunc(name)	(1)
#	define WinRegOpenKeyEx		RegOpenKeyEx
#	define WinRegQueryValueEx	RegQueryValueEx
#	define WinRegCloseKey		RegCloseKey
#	define WinRegOpenKeyEx		RegOpenKeyEx
#	define WinRegEnumKeyEx		RegEnumKeyEx
#	define WinRegEnumValue		RegEnumValue
#	define WinRegCreateKeyEx	RegCreateKeyEx
#	define WinRegDeleteKey		RegDeleteKey
#	define WinRegDeleteValue	RegDeleteValue
#	define WinRegSetValueEx		RegSetValueEx
#else
#	define has_regfunc(name)	((name) != NULL)
#	define WinRegOpenKeyEx		gate_platform.AdvRegOpenKeyEx
#	define WinRegQueryValueEx	gate_platform.AdvRegQueryValueEx
#	define WinRegCloseKey		gate_platform.AdvRegCloseKey
#	define WinRegOpenKeyEx		gate_platform.AdvRegOpenKeyEx
#	define WinRegEnumKeyEx		gate_platform.AdvRegEnumKeyEx
#	define WinRegEnumValue		gate_platform.AdvRegEnumValue
#	define WinRegCreateKeyEx	gate_platform.AdvRegCreateKeyEx
#	define WinRegDeleteKey		gate_platform.AdvRegDeleteKey
#	define WinRegDeleteValue	gate_platform.AdvRegDeleteValue
#	define WinRegSetValueEx		gate_platform.AdvRegSetValueEx
#endif

static gate_result_t gate_win32_registry_read(int root, LPCTSTR reg_path, LPCTSTR value_name,
    void* buffer, gate_size_t buffer_len, gate_size_t* buffer_used, DWORD* ptr_type)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hkey_root = gate_win32_registry_get_root(root);
    HKEY hkey = NULL;
    LONG result;
    DWORD datalen = (DWORD)buffer_len;
    do
    {
        if (!has_regfunc(WinRegOpenKeyEx) || !has_regfunc(WinRegQueryValueEx) || !has_regfunc(WinRegCloseKey))
        {
            ret = GATE_RESULT_NOTSUPPORTED;
            break;
        }

        result = WinRegOpenKeyEx(hkey_root, reg_path, 0, KEY_READ, &hkey);
        if (result != ERROR_SUCCESS)
        {
            gate_win32_print_lasterror_code(result, &ret, NULL, 0);
            break;
        }

        result = WinRegQueryValueEx(hkey, value_name, NULL, ptr_type, buffer, &datalen);
        if (result != ERROR_SUCCESS)
        {
            gate_win32_print_lasterror_code(result, &ret, NULL, 0);
            break;
        }
        if (buffer_used != NULL)
        {
            *buffer_used = datalen;
        }
        ret = GATE_RESULT_OK;
    } while (0);

    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}

gate_result_t gate_win32_registry_read_dword(int root, LPCTSTR reg_path, LPCTSTR value_name, DWORD* ptr_value)
{
    gate_result_t ret;
    DWORD data_type;
    ret = gate_win32_registry_read(root, reg_path, value_name, ptr_value, sizeof(DWORD), NULL, &data_type);
    if (GATE_SUCCEEDED(ret))
    {
        if (data_type != REG_DWORD)
        {
            ret = GATE_RESULT_INCORRECTTYPE;
        }
    }
    return ret;
}

#ifndef REG_QWORD
#define REG_QWORD 11
#endif 
gate_result_t gate_win32_registry_read_qword(int root, LPCTSTR reg_path, LPCTSTR value_name, ULONGLONG* ptr_value)
{
    DWORD data_type;
    gate_result_t ret = gate_win32_registry_read(root, reg_path, value_name, ptr_value, sizeof(DWORD), NULL, &data_type);
    if (GATE_SUCCEEDED(ret))
    {
        if (data_type != REG_QWORD)
        {
            ret = GATE_RESULT_INCORRECTTYPE;
        }
    }
    return ret;
}

gate_result_t gate_win32_registry_read_winstr(int root, LPCTSTR reg_path, LPCTSTR value_name, TCHAR* ptr_text, gate_size_t text_len, gate_size_t* text_used)
{
    gate_result_t ret;
    gate_size_t buffer_used = (text_len - 1) * sizeof(TCHAR);
    DWORD data_type;
    gate_size_t used;
    ret = gate_win32_registry_read(root, reg_path, value_name, ptr_text, buffer_used, &buffer_used, &data_type);
    if (GATE_SUCCEEDED(ret))
    {
        if ((data_type != REG_EXPAND_SZ) && (data_type != REG_SZ))
        {
            ret = GATE_RESULT_INCORRECTTYPE;
        }
        else
        {
            used = buffer_used / sizeof(TCHAR);
            if (used > 0)
            {
                /* strings are returned with included NULL-terminator */
                if (ptr_text[used - 1] == 0)
                {
                    --used;
                }
            }
            ptr_text[used] = 0;
            if (text_used != NULL)
            {
                *text_used = used;
            }
            ret = GATE_RESULT_OK;
        }
    }
    return ret;
}

gate_result_t gate_win32_registry_read_str(int root, LPCTSTR reg_path, LPCTSTR value_name, char* ptr_text, gate_size_t text_len, gate_size_t* text_used)
{
    gate_result_t ret;
    TCHAR text_buffer[GATE_MAX_COPYBUFFER_LENGTH];
    gate_size_t text_buffer_used = 0;
    gate_size_t used;

    ret = gate_win32_registry_read_winstr(root, reg_path, value_name,
        text_buffer, sizeof(text_buffer) / sizeof(text_buffer[0]), &text_buffer_used);
    if (GATE_SUCCEEDED(ret))
    {
        used = gate_win32_winstr_2_utf8(&text_buffer[0], text_buffer_used, ptr_text, text_len);
        if ((text_buffer_used) != 0 && (used == 0))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
        }
        if (text_used != NULL)
        {
            *text_used = used;
        }
    }
    return ret;
}
gate_result_t gate_win32_registry_read_string(int root, LPCTSTR reg_path, LPCTSTR value_name, gate_string_t* ptr_value)
{
    gate_result_t ret;
    TCHAR text_buffer[GATE_MAX_STACK_COPYBUFFER_LENGTH / sizeof(TCHAR)];
    gate_size_t text_buffer_used = 0;

    ret = gate_win32_registry_read_winstr(root, reg_path, value_name,
        text_buffer, sizeof(text_buffer) / sizeof(text_buffer[0]), &text_buffer_used);
    if (GATE_SUCCEEDED(ret))
    {
        if (NULL == gate_win32_winstr_2_utf8_string(&text_buffer[0], text_buffer_used, ptr_value))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
        }
    }
    return ret;
}
gate_result_t gate_win32_registry_read_binary(int root, LPCTSTR reg_path, LPCTSTR value_name, void* buffer, gate_size_t buffer_length, gate_size_t* buffer_used)
{
    DWORD data_type;
    gate_result_t ret = gate_win32_registry_read(root, reg_path, value_name, buffer, buffer_length, buffer_used, &data_type);
    if (GATE_SUCCEEDED(ret))
    {
        if ((data_type != REG_BINARY))
        {
            ret = GATE_RESULT_INCORRECTTYPE;
        }
    }
    return ret;
}
gate_result_t gate_win32_registry_read_value(int root, LPCTSTR reg_path, LPCTSTR value_name, gate_value_t* ptr_value)
{
    char buffer[GATE_MAX_COPYBUFFER_LENGTH];
    gate_size_t buffer_used = sizeof(buffer);
    DWORD data_type;
    gate_string_t str_value;
    gate_value_t* ptr_result = NULL;

    gate_result_t ret = gate_win32_registry_read(root, reg_path, value_name, buffer, sizeof(buffer), &buffer_used, &data_type);
    if (GATE_SUCCEEDED(ret))
    {
        switch (data_type)
        {
        case REG_DWORD:
        {
            ptr_result = gate_value_create(GATE_TYPE_UI32, &buffer[0], ptr_value);
            break;
        }
        case REG_QWORD:
        {
            ptr_result = gate_value_create(GATE_TYPE_UI64, &buffer[0], ptr_value);
            break;
        }
        case REG_EXPAND_SZ:
        case REG_SZ:
        {
            if (NULL != gate_win32_winstr_2_utf8_string((LPTSTR)&buffer[0], buffer_used / sizeof(TCHAR), &str_value))
            {
                ptr_result = gate_value_create(GATE_TYPE_STRING, &str_value, ptr_value);
                gate_string_release(&str_value);
            }
            break;
        }
        case REG_MULTI_SZ:
        {
            break;
        }
        case REG_BINARY:
        {
            break;
        }
        default:
        {
            ret = GATE_RESULT_INCORRECTTYPE;
            break;
        }
        }

        if (ptr_result == NULL)
        {
            ret = GATE_RESULT_FAILED;
        }
    }
    return ret;

}

gate_result_t gate_win32_registry_enum_keys(int root, LPCTSTR reg_path, gate_win32_registry_keys_callback_t callback, void* userparam)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hroot = NULL;
    HKEY hkey = NULL;
    LONG lresult;
    TCHAR key_path[GATE_MAX_FILEPATH_LENGTH];
    TCHAR buffer[GATE_MAX_FILEPATH_LENGTH];
    DWORD bufferlen;
    gate_size_t key_length;
    DWORD index;
    gate_bool_t continue_loop = true;

    do
    {
        hroot = gate_win32_registry_get_root(root);
        if (hroot == NULL)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        if (!has_regfunc(WinRegOpenKeyEx) || !has_regfunc(WinRegEnumKeyEx) || !has_regfunc(WinRegCloseKey))
        {
            ret = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        key_length = gate_win32_winstr_print_text(key_path, sizeof(key_path) / sizeof(key_path[0]), reg_path, gate_win32_winstr_length(reg_path));

        lresult = WinRegOpenKeyEx(hroot, key_path, 0, KEY_ENUMERATE_SUB_KEYS, &hkey);
        if (lresult != ERROR_SUCCESS)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }

        ret = GATE_RESULT_OK;
        for (index = 0; continue_loop; ++index)
        {
            bufferlen = sizeof(buffer) / sizeof(buffer[0]);
            lresult = WinRegEnumKeyEx(hkey, index, buffer, &bufferlen, NULL, NULL, NULL, NULL);
            if (ERROR_SUCCESS != lresult)
            {
                ret = (ERROR_NO_MORE_ITEMS == lresult) ? GATE_RESULT_OK : GATE_RESULT_FAILED;
                break;
            }
            gate_win32_winstr_print_text(&key_path[key_length], (sizeof(key_path) / sizeof(key_path[0])) - key_length,
                &buffer[0], bufferlen);
            if (callback)
            {
                continue_loop = callback(reg_path, key_path, userparam);
            }
        }

    } while (0);

    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}

gate_result_t gate_win32_registry_enum_values(int root, LPCTSTR reg_path,
    gate_win32_registry_values_callback_t callback, void* userparam)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hroot = NULL;
    HKEY hkey = NULL;
    LONG lresult;
    TCHAR key_path[GATE_MAX_FILEPATH_LENGTH];
    TCHAR buffer[GATE_MAX_FILEPATH_LENGTH];
    DWORD bufferlen;
    gate_size_t key_length;
    DWORD index;
    DWORD valuetype;
    DWORD valuesize;
    gate_bool_t continue_loop = true;

    do
    {
        hroot = gate_win32_registry_get_root(root);
        if (hroot == NULL)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        if (!has_regfunc(WinRegOpenKeyEx) || !has_regfunc(WinRegEnumKeyEx) || !has_regfunc(WinRegCloseKey))
        {
            ret = GATE_RESULT_NOTSUPPORTED;
            break;
        }
        key_length = gate_win32_winstr_print_text(key_path, sizeof(key_path) / sizeof(key_path[0]), reg_path, gate_win32_winstr_length(reg_path));

        lresult = WinRegOpenKeyEx(hroot, key_path, 0, KEY_QUERY_VALUE, &hkey);
        if (lresult != ERROR_SUCCESS)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }

        ret = GATE_RESULT_OK;
        for (index = 0; continue_loop; ++index)
        {
            bufferlen = sizeof(buffer) / sizeof(buffer[0]) - 1;
            valuetype = 0;
            valuesize = 0;
            lresult = WinRegEnumValue(hkey, index, buffer, &bufferlen, NULL, &valuetype, NULL, &valuesize);
            if (ERROR_SUCCESS != lresult)
            {
                ret = (ERROR_NO_MORE_ITEMS == lresult) ? GATE_RESULT_OK : GATE_RESULT_FAILED;
                break;
            }
            buffer[bufferlen] = 0;
            if (callback)
            {
                switch (valuetype)
                {
                case REG_NONE:		valuetype = GATE_WIN32_REGISTRY_TYPE_NONE;		break;
                case REG_SZ:		valuetype = GATE_WIN32_REGISTRY_TYPE_SZ;		break;
                case REG_EXPAND_SZ:	valuetype = GATE_WIN32_REGISTRY_TYPE_EXPAND_SZ;	break;
                case REG_BINARY:	valuetype = GATE_WIN32_REGISTRY_TYPE_BINARY;	break;
                case REG_DWORD:		valuetype = GATE_WIN32_REGISTRY_TYPE_DWORD;		break;
                case REG_MULTI_SZ:	valuetype = GATE_WIN32_REGISTRY_TYPE_MULTI_SZ;	break;
                case REG_QWORD:		valuetype = GATE_WIN32_REGISTRY_TYPE_QWORD;		break;
                default:			valuetype = GATE_WIN32_REGISTRY_TYPE_NONE;		break;
                }
                continue_loop = callback(key_path, buffer, (gate_enumint_t)valuetype, (gate_size_t)valuesize, userparam);
            }
        }

    } while (0);

    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}



gate_result_t gate_win32_registry_create_path(int root, LPCTSTR reg_path)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hroot = NULL;
    HKEY hkey = NULL;
    DWORD dwDisposition = 0;
    LONG result;
    do
    {
        hroot = gate_win32_registry_get_root(root);
        if (hroot == NULL)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }
        result = WinRegCreateKeyEx(hroot, reg_path, 0, NULL,
            REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, NULL, &hkey, &dwDisposition);
        if (result != ERROR_SUCCESS)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }
        ret = GATE_RESULT_OK;
    } while (0);
    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}
gate_result_t gate_win32_registry_delete_path(int root, LPCTSTR reg_path)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hroot = NULL;
    LONG result;
    do
    {
        hroot = gate_win32_registry_get_root(root);
        if (hroot == NULL)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }
        result = WinRegDeleteKey(hroot, reg_path);
        if (result != ERROR_SUCCESS)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }
        ret = GATE_RESULT_OK;
    } while (0);
    return ret;
}
gate_result_t gate_win32_registry_delete_value(int root, LPCTSTR reg_path, LPCTSTR value_name)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hroot = NULL;
    HKEY hkey = NULL;
    LONG result;
    do
    {
        hroot = gate_win32_registry_get_root(root);
        if (hroot == NULL)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }
        result = WinRegOpenKeyEx(hroot, reg_path, 0, KEY_SET_VALUE, &hkey);
        if (ERROR_SUCCESS != result)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }
        result = WinRegDeleteValue(hkey, value_name);
        if (ERROR_SUCCESS != result)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }
        ret = GATE_RESULT_OK;
    } while (0);
    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}


static gate_result_t gate_win32_registry_write(int root, LPCTSTR reg_path, LPCTSTR value_name,
    DWORD data_type, void const* buffer, gate_size_t buffer_len)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HKEY hkey_root = gate_win32_registry_get_root(root);
    HKEY hkey = NULL;
    LONG result;
    DWORD datalen = (DWORD)buffer_len;
    do
    {
        if (!has_regfunc(WinRegOpenKeyEx) || !has_regfunc(WinRegSetValueEx) || !has_regfunc(WinRegCloseKey))
        {
            ret = GATE_RESULT_NOTSUPPORTED;
            break;
        }


        result = WinRegOpenKeyEx(hkey_root, reg_path, 0, KEY_WRITE, &hkey);
        if (result != ERROR_SUCCESS)
        {
            gate_win32_print_lasterror_code(result, &ret, NULL, 0);
            break;
        }

        result = WinRegSetValueEx(hkey, value_name, 0, data_type, (BYTE const*)buffer, datalen);
        if (result != ERROR_SUCCESS)
        {
            gate_win32_print_lasterror_code(result, &ret, NULL, 0);
            break;
        }
        ret = GATE_RESULT_OK;
    } while (0);

    if (hkey != NULL)
    {
        WinRegCloseKey(hkey);
    }
    return ret;
}


gate_result_t gate_win32_registry_write_dword(int root, LPCTSTR reg_path, LPCTSTR value_name, DWORD value)
{
    return gate_win32_registry_write(root, reg_path, value_name, REG_DWORD, &value, sizeof(value));
}
gate_result_t gate_win32_registry_write_qword(int root, LPCTSTR reg_path, LPCTSTR value_name, ULONGLONG value)
{
    return gate_win32_registry_write(root, reg_path, value_name, REG_QWORD, &value, sizeof(value));
}
gate_result_t gate_win32_registry_write_winstr(int root, LPCTSTR reg_path, LPCTSTR value_name, TCHAR const* text, gate_size_t text_len)
{
    gate_result_t ret;
    TCHAR* buffer = NULL;
    if (text[text_len] != 0)
    {
        buffer = (TCHAR*)gate_mem_alloc((text_len + 1) * sizeof(TCHAR));
        if (buffer == NULL)
        {
            return GATE_RESULT_OUTOFMEMORY;
        }
        gate_mem_copy(buffer, text, text_len * sizeof(TCHAR));
        buffer[text_len] = 0;
        text = buffer;
    }
    ret = gate_win32_registry_write(root, reg_path, value_name, REG_SZ, text, text_len * sizeof(TCHAR));
    if (buffer != NULL)
    {
        gate_mem_dealloc(buffer);
    }
    return ret;
}
gate_result_t gate_win32_registry_write_str(int root, LPCTSTR reg_path, LPCTSTR value_name, char const* text, gate_size_t text_len)
{
    TCHAR buffer[4096 + 2048 + 1024] = GATE_INIT_EMPTY;
    gate_size_t len = gate_win32_utf8_2_winstr(text, text_len, buffer, sizeof(buffer) / sizeof(buffer[0]));
    return gate_win32_registry_write_winstr(root, reg_path, value_name, buffer, len);
}
gate_result_t gate_win32_registry_write_string(int root, LPCTSTR reg_path, LPCTSTR value_name, gate_string_t const* text)
{
    return gate_win32_registry_write_str(root, reg_path, value_name, text->str, text->length);
}
gate_result_t gate_win32_registry_write_binary(int root, LPCTSTR reg_path, LPCTSTR value_name, void const* buffer, gate_size_t buffer_length)
{
    return gate_win32_registry_write(root, reg_path, value_name, REG_BINARY, buffer, buffer_length);
}

#endif