Use Safe String functions in Registry.c and add a unicode version of WriteLocalMachineRegistryDword function to avoid doing conversions when used.

This commit is contained in:
Mounir IDRASSI 2014-07-14 17:20:32 +02:00
parent 016edc150b
commit ba733dd032
2 changed files with 31 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#include "Tcdefs.h" #include "Tcdefs.h"
#include "Registry.h" #include "Registry.h"
#include <Strsafe.h>
BOOL ReadLocalMachineRegistryDword (char *subKey, char *name, DWORD *value) BOOL ReadLocalMachineRegistryDword (char *subKey, char *name, DWORD *value)
{ {
@ -105,13 +106,13 @@ char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *st
DWORD size = sizeof (value); DWORD size = sizeof (value);
str[maxLen-1] = 0; str[maxLen-1] = 0;
strncpy (str, defaultValue, maxLen-1); StringCbCopyA (str, maxLen, defaultValue);
ZeroMemory (value, sizeof value); ZeroMemory (value, sizeof value);
if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey, if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
0, KEY_READ, &hkey) == ERROR_SUCCESS) 0, KEY_READ, &hkey) == ERROR_SUCCESS)
if (RegQueryValueEx (hkey, name, 0, 0, (LPBYTE) value, &size) == ERROR_SUCCESS) if (RegQueryValueEx (hkey, name, 0, 0, (LPBYTE) value, &size) == ERROR_SUCCESS)
strncpy (str, value, maxLen-1); StringCbCopyA (str, maxLen,value);
RegCloseKey (hkey); RegCloseKey (hkey);
return str; return str;
@ -169,6 +170,30 @@ BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value)
return TRUE; return TRUE;
} }
BOOL WriteLocalMachineRegistryDwordW (WCHAR *subKey, WCHAR *name, DWORD value)
{
HKEY hkey = 0;
DWORD disp;
LONG status;
if ((status = RegCreateKeyExW (HKEY_LOCAL_MACHINE, subKey,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
{
SetLastError (status);
return FALSE;
}
if ((status = RegSetValueExW (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value)) != ERROR_SUCCESS)
{
RegCloseKey (hkey);
SetLastError (status);
return FALSE;
}
RegCloseKey (hkey);
return TRUE;
}
BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size) BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size)
{ {
HKEY hkey = 0; HKEY hkey = 0;
@ -279,9 +304,9 @@ void DeleteRegistryValue (char *subKey, char *name)
} }
void GetStartupRegKeyName (char *regk) void GetStartupRegKeyName (char *regk, size_t cbRegk)
{ {
// The string is split in order to prevent some antivirus packages from falsely reporting // The string is split in order to prevent some antivirus packages from falsely reporting
// TrueCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan). // TrueCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan).
sprintf (regk, "%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run"); StringCbPrintfA (regk, cbRegk,"%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run");
} }

View File

@ -19,13 +19,14 @@ char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *st
DWORD ReadRegistryBytes (char *path, char *name, char *value, int maxLen); DWORD ReadRegistryBytes (char *path, char *name, char *value, int maxLen);
void WriteRegistryInt (char *subKey, char *name, int value); void WriteRegistryInt (char *subKey, char *name, int value);
BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value); BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value);
BOOL WriteLocalMachineRegistryDwordW (WCHAR *subKey, WCHAR *name, DWORD value);
BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size); BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size);
BOOL WriteLocalMachineRegistryString (char *subKey, char *name, char *str, BOOL expandable); BOOL WriteLocalMachineRegistryString (char *subKey, char *name, char *str, BOOL expandable);
void WriteRegistryString (char *subKey, char *name, char *str); void WriteRegistryString (char *subKey, char *name, char *str);
BOOL WriteRegistryBytes (char *path, char *name, char *str, DWORD size); BOOL WriteRegistryBytes (char *path, char *name, char *str, DWORD size);
BOOL DeleteLocalMachineRegistryKey (char *parentKey, char *subKeyToDelete); BOOL DeleteLocalMachineRegistryKey (char *parentKey, char *subKeyToDelete);
void DeleteRegistryValue (char *subKey, char *name); void DeleteRegistryValue (char *subKey, char *name);
void GetStartupRegKeyName (char *regk); void GetStartupRegKeyName (char *regk, size_t cbRegk);
#ifdef __cplusplus #ifdef __cplusplus
} }