-
Notifications
You must be signed in to change notification settings - Fork 194
/
RegHelpers.c
92 lines (78 loc) · 2.73 KB
/
RegHelpers.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
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
/**
* Registry Helper Functions
* Copyright (C) Kai Liu. All rights reserved.
*
* Please refer to readme.txt for information about this source code.
* Please refer to license.txt for details about distribution and modification.
**/
#include "RegHelpers.h"
#include "libs/Wow64.h"
#include "libs/SimpleString.h"
#include <Strsafe.h>
#define countof(x) (sizeof(x)/sizeof(x[0]))
HKEY WINAPI RegOpen( HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpSubst, BOOL bCreate )
{
HKEY hKeyRet = NULL;
TCHAR szJoinedKey[0x7F];
if (lpSubst) StringCchPrintf(szJoinedKey, countof(szJoinedKey), lpSubKey, lpSubst);
LONG lResult;
if (bCreate)
lResult = RegCreateKeyEx(hKey, (lpSubst) ? szJoinedKey : lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKeyRet, NULL);
else
lResult = RegOpenKeyEx(hKey, (lpSubst) ? szJoinedKey : lpSubKey, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKeyRet);
if (lResult == ERROR_SUCCESS)
{
Wow64DisableRegReflection(hKeyRet);
return(hKeyRet);
}
return(NULL);
}
BOOL WINAPI RegDelete( HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpSubst )
{
LONG lResult;
TCHAR szJoinedKey[0x7F];
if (lpSubst) StringCchPrintf(szJoinedKey, countof(szJoinedKey), lpSubKey, lpSubst);
lResult = SHDeleteKey(hKey, (lpSubst) ? szJoinedKey : lpSubKey);
return(lResult == ERROR_SUCCESS || lResult == ERROR_FILE_NOT_FOUND);
}
BOOL WINAPI RegSetSZ( HKEY hKey, LPCTSTR lpValueName, LPCTSTR lpData )
{
return(RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpData, ((DWORD)SSLen(lpData) + 1) * sizeof(lpData[0])) == ERROR_SUCCESS);
}
BOOL WINAPI RegSetDW( HKEY hKey, LPCTSTR lpValueName, DWORD dwData )
{
return(RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (LPBYTE)&dwData, sizeof(dwData)) == ERROR_SUCCESS);
}
BOOL WINAPI RegGetSZ( HKEY hKey, LPCTSTR lpValueName, LPTSTR lpData, DWORD cbData )
{
DWORD dwType;
if ( RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)lpData, &cbData) == ERROR_SUCCESS &&
dwType == REG_SZ && cbData >= sizeof(TCHAR) )
{
// The registry does not check for proper null termination, so in order
// to prevent crashes in the case of a malformed registry entry, make
// sure that, at the very least, the final character is a null
*((LPTSTR)((LPBYTE)lpData + cbData - sizeof(TCHAR))) = 0;
return(TRUE);
}
else
{
lpData[0] = 0;
return(FALSE);
}
}
BOOL WINAPI RegGetDW( HKEY hKey, LPCTSTR lpValueName, LPDWORD lpdwData )
{
DWORD cbData = sizeof(DWORD);
DWORD dwType;
if ( RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)lpdwData, &cbData) == ERROR_SUCCESS &&
dwType == REG_DWORD && cbData == sizeof(DWORD) )
{
return(TRUE);
}
else
{
*lpdwData = 0;
return(FALSE);
}
}