forked from temerkhanov/SetTimerService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.cpp
More file actions
98 lines (75 loc) · 2.34 KB
/
Copy pathRegistry.cpp
File metadata and controls
98 lines (75 loc) · 2.34 KB
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
#include "Registry.h"
#include <tchar.h>
#include <stdio.h>
#define MAX_KEY_NAME_LENGTH 255
#define REG_PATH_TEMPLATE _T("SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters")
LONG RegistryGetServiceParameter(
LPCTSTR ServiceName,
LPCTSTR ParameterName,
LPDWORD ParameterValue
)
{
LONG Result;
LPTSTR KeyName;
HKEY KeyHandle;
HANDLE Heap = GetProcessHeap();
DWORD Disposition;
DWORD Type;
DWORD Size = sizeof(ULONG);
if (Heap == NULL)
return GetLastError();
KeyName = (LPTSTR)HeapAlloc(Heap, HEAP_ZERO_MEMORY, MAX_KEY_NAME_LENGTH * sizeof(TCHAR));
if (KeyName == NULL)
return GetLastError();
_sntprintf(KeyName, MAX_KEY_NAME_LENGTH, REG_PATH_TEMPLATE, ServiceName);
Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &KeyHandle, &Disposition);
if (Result != ERROR_SUCCESS)
{
goto Exit;
}
Result = RegQueryValueEx(KeyHandle, ParameterName, NULL, &Type, (LPBYTE)ParameterValue, &Size);
if (Result != ERROR_SUCCESS)
{
goto Close;
}
Close:
RegCloseKey(KeyHandle);
Exit:
HeapFree(Heap, 0, KeyName);
return Result;
}
LONG RegistrySetServiceParameter(
LPCTSTR ServiceName,
LPCTSTR ParameterName,
DWORD ParameterValue
)
{
LONG Result;
LPTSTR KeyName;
HKEY KeyHandle;
HANDLE Heap = GetProcessHeap();
DWORD Disposition;
if (Heap == NULL)
return GetLastError();
KeyName = (LPTSTR)HeapAlloc(Heap, HEAP_ZERO_MEMORY, MAX_KEY_NAME_LENGTH * sizeof(TCHAR));
if (KeyName == NULL)
return GetLastError();
_sntprintf(KeyName, MAX_KEY_NAME_LENGTH, REG_PATH_TEMPLATE, ServiceName);
Result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &KeyHandle, &Disposition);
if (Result != ERROR_SUCCESS)
{
goto Exit;
}
Result = RegSetValueEx(KeyHandle, ParameterName, NULL, REG_DWORD, (const LPBYTE)&ParameterValue, sizeof(ULONG));
if (Result != ERROR_SUCCESS)
{
goto Close;
}
Close:
RegCloseKey(KeyHandle);
Exit:
HeapFree(Heap, 0, KeyName);
return Result;
}