[efi] check for 7-zip path in HKLM

* Closes #121
* Also update registry handling
* Issues reported by Ronny Kalusniok
This commit is contained in:
Pete Batard 2013-02-02 16:57:46 +00:00
parent c51bf5ce45
commit d2e7e003c3
6 changed files with 70 additions and 72 deletions

View file

@ -26,6 +26,9 @@
extern "C" {
#endif
#define REGKEY_HKCU HKEY_CURRENT_USER
#define REGKEY_HKLM HKEY_LOCAL_MACHINE
/*
* List of registry keys used by this application
* These keys go into HKCU\Software\COMPANY_NAME\APPLICATION_NAME\
@ -36,14 +39,14 @@ extern "C" {
#define REGKEY_INCLUDE_BETAS "CheckForBetas"
#define REGKEY_COMM_CHECK "CommCheck"
/* Delete a registry key from HKCU\Software and all its values
/* Delete a registry key from <key_root>\Software and all its values
If the key has subkeys, this call will fail. */
static __inline BOOL DeleteRegistryKey(const char* key_name)
static __inline BOOL DeleteRegistryKey(HKEY key_root, const char* key_name)
{
HKEY hSoftware = NULL;
LONG s;
if (RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS) {
if (RegOpenKeyExA(key_root, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS) {
return FALSE;
}
@ -59,7 +62,7 @@ static __inline BOOL DeleteRegistryKey(const char* key_name)
/* Read a generic registry key value. If a short key_name is used, assume that it belongs to
the application and create the app subkey if required */
static __inline BOOL _GetRegistryKey(const char* key_name, DWORD reg_type, LPBYTE dest, DWORD dest_size)
static __inline BOOL _GetRegistryKey(HKEY key_root, const char* key_name, DWORD reg_type, LPBYTE dest, DWORD dest_size)
{
BOOL r = FALSE;
size_t i = 0;
@ -78,10 +81,10 @@ static __inline BOOL _GetRegistryKey(const char* key_name, DWORD reg_type, LPBYT
safe_strcat(long_key_name, sizeof(long_key_name), key_name);
long_key_name[sizeof("SOFTWARE\\") + i-1] = 0;
i++;
if (RegOpenKeyExA(HKEY_CURRENT_USER, long_key_name, 0, KEY_READ, &hApp) != ERROR_SUCCESS)
if (RegOpenKeyExA(key_root, long_key_name, 0, KEY_READ, &hApp) != ERROR_SUCCESS)
goto out;
} else {
if ( (RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS)
if ( (RegOpenKeyExA(key_root, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS)
|| (RegCreateKeyExA(hSoftware, COMPANY_NAME "\\" APPLICATION_NAME, 0, NULL, 0,
KEY_SET_VALUE|KEY_QUERY_VALUE|KEY_CREATE_SUB_KEY, NULL, &hApp, &dwDisp) != ERROR_SUCCESS) )
goto out;
@ -99,13 +102,13 @@ out:
}
/* Write a generic registry key value (create the key if it doesn't exist) */
static __inline BOOL _SetRegistryKey(const char* key_name, DWORD reg_type, LPBYTE src, DWORD src_size)
static __inline BOOL _SetRegistryKey(HKEY key_root, const char* key_name, DWORD reg_type, LPBYTE src, DWORD src_size)
{
BOOL r = FALSE;
HKEY hSoftware = NULL, hApp = NULL;
DWORD dwDisp, dwType = reg_type;
if ( (RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS)
if ( (RegOpenKeyExA(key_root, "SOFTWARE", 0, KEY_READ|KEY_CREATE_SUB_KEY, &hSoftware) != ERROR_SUCCESS)
|| (RegCreateKeyExA(hSoftware, COMPANY_NAME "\\" APPLICATION_NAME, 0, NULL, 0,
KEY_SET_VALUE|KEY_QUERY_VALUE|KEY_CREATE_SUB_KEY, NULL, &hApp, &dwDisp) != ERROR_SUCCESS) ) {
goto out;
@ -120,52 +123,52 @@ out:
}
/* Helpers for 64 bit registry operations */
#define GetRegistryKey64(key, pval) _GetRegistryKey(key, REG_QWORD, (LPBYTE)pval, sizeof(LONGLONG))
#define SetRegistryKey64(key, val) _SetRegistryKey(key, REG_QWORD, (LPBYTE)&val, sizeof(LONGLONG))
#define GetRegistryKey64(root, key, pval) _GetRegistryKey(root, key, REG_QWORD, (LPBYTE)pval, sizeof(LONGLONG))
#define SetRegistryKey64(root, key, val) _SetRegistryKey(root, key, REG_QWORD, (LPBYTE)&val, sizeof(LONGLONG))
// Check that a key is accessible for R/W (will create a key if not already existing)
static __inline BOOL CheckRegistryKey64(const char* key) {
static __inline BOOL CheckRegistryKey64(HKEY root, const char* key) {
LONGLONG val;
return GetRegistryKey64(key, &val); // && SetRegistryKey64(key, val));
return GetRegistryKey64(root, key, &val); // && SetRegistryKey64(key, val));
}
static __inline int64_t ReadRegistryKey64(const char* key) {
static __inline int64_t ReadRegistryKey64(HKEY root, const char* key) {
LONGLONG val;
GetRegistryKey64(key, &val);
GetRegistryKey64(root, key, &val);
return (int64_t)val;
}
static __inline BOOL WriteRegistryKey64(const char* key, int64_t val) {
static __inline BOOL WriteRegistryKey64(HKEY root, const char* key, int64_t val) {
LONGLONG tmp = (LONGLONG)val;
return SetRegistryKey64(key, tmp);
return SetRegistryKey64(root, key, tmp);
}
/* Helpers for 32 bit registry operations */
#define GetRegistryKey32(key, pval) _GetRegistryKey(key, REG_DWORD, (LPBYTE)pval, sizeof(DWORD))
#define SetRegistryKey32(key, val) _SetRegistryKey(key, REG_DWORD, (LPBYTE)&val, sizeof(DWORD))
static __inline BOOL CheckRegistryKey32(const char* key) {
#define GetRegistryKey32(root, key, pval) _GetRegistryKey(root, key, REG_DWORD, (LPBYTE)pval, sizeof(DWORD))
#define SetRegistryKey32(root, key, val) _SetRegistryKey(root, key, REG_DWORD, (LPBYTE)&val, sizeof(DWORD))
static __inline BOOL CheckRegistryKey32(HKEY root, const char* key) {
DWORD val;
return (GetRegistryKey32(key, &val) && SetRegistryKey32(key, val));
return (GetRegistryKey32(root, key, &val) && SetRegistryKey32(root, key, val));
}
static __inline int32_t ReadRegistryKey32(const char* key) {
static __inline int32_t ReadRegistryKey32(HKEY root, const char* key) {
DWORD val;
GetRegistryKey32(key, &val);
GetRegistryKey32(root, key, &val);
return (int32_t)val;
}
static __inline BOOL WriteRegistryKey32(const char* key, int32_t val) {
static __inline BOOL WriteRegistryKey32(HKEY root, const char* key, int32_t val) {
DWORD tmp = (DWORD)val;
return SetRegistryKey32(key, tmp);
return SetRegistryKey32(root, key, tmp);
}
/* Helpers for boolean registry operations */
#define GetRegistryKeyBool(key) (ReadRegistryKey32(key) != 0)
#define SetRegistryKeyBool(key, b) WriteRegistryKey32(key, (b)?1:0)
#define GetRegistryKeyBool(root, key) (ReadRegistryKey32(root, key) != 0)
#define SetRegistryKeyBool(root, key, b) WriteRegistryKey32(root, key, (b)?1:0)
#define CheckRegistryKeyBool CheckRegistryKey32
/* Helpers for String registry operations */
#define GetRegistryKeyStr(key, str, len) _GetRegistryKey(key, REG_SZ, (LPBYTE)str, (DWORD)len)
#define SetRegistryKeyStr(key, str) _SetRegistryKey(key, REG_SZ, (LPBYTE)str, safe_strlen(str))
#define GetRegistryKeyStr(root, key, str, len) _GetRegistryKey(root, key, REG_SZ, (LPBYTE)str, (DWORD)len)
#define SetRegistryKeyStr(root, key, str) _SetRegistryKey(root, key, REG_SZ, (LPBYTE)str, safe_strlen(str))
// Use a static buffer - don't allocate
static __inline char* ReadRegistryKeyStr(const char* key) {
static __inline char* ReadRegistryKeyStr(HKEY root, const char* key) {
static char str[512];
_GetRegistryKey(key, REG_SZ, (LPBYTE)str, (DWORD)sizeof(str)-1);
_GetRegistryKey(root, key, REG_SZ, (LPBYTE)str, (DWORD)sizeof(str)-1);
return str;
}
#define WriteRegistryKeyStr SetRegistryKeyStr