Annotation of truecrypt/common/registry.c, revision 1.1.1.9

1.1.1.7   root        1: /*
1.1.1.9 ! root        2:  Copyright (c) 2004-2008 TrueCrypt Foundation. All rights reserved.
1.1.1.4   root        3: 
1.1.1.9 ! root        4:  Governed by the TrueCrypt License 2.4 the full text of which is contained
1.1.1.7   root        5:  in the file License.txt included in TrueCrypt binary and source code
                      6:  distribution packages.
1.1.1.4   root        7: */
1.1       root        8: 
1.1.1.4   root        9: #include "Tcdefs.h"
                     10: #include "Registry.h"
1.1       root       11: 
1.1.1.9 ! root       12: BOOL ReadLocalMachineRegistryDword (char *subKey, char *name, DWORD *value)
        !            13: {
        !            14:        HKEY hkey = 0;
        !            15:        DWORD size = sizeof (*value);
        !            16: 
        !            17:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
        !            18:                return FALSE;
        !            19: 
        !            20:        if (RegQueryValueEx (hkey, name, NULL, NULL, (BYTE *) value, &size) != ERROR_SUCCESS)
        !            21:        {
        !            22:                RegCloseKey (hkey);
        !            23:                return FALSE;
        !            24:        }
        !            25: 
        !            26:        RegCloseKey (hkey);
        !            27:        return TRUE;
        !            28: }
        !            29: 
1.1.1.4   root       30: int ReadRegistryInt (char *subKey, char *name, int defaultValue)
1.1       root       31: {
                     32:        HKEY hkey = 0;
                     33:        DWORD value, size = sizeof (DWORD);
                     34: 
1.1.1.4   root       35:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root       36:                0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     37:                return defaultValue;
                     38: 
                     39:        if (RegQueryValueEx (hkey, name, 0,     0, (LPBYTE) &value, &size) != ERROR_SUCCESS)
                     40:                value = defaultValue;
                     41: 
                     42:        RegCloseKey (hkey);
                     43:        return value;
                     44: }
                     45: 
1.1.1.4   root       46: char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *str, int maxLen)
1.1       root       47: {
                     48:        HKEY hkey = 0;
1.1.1.4   root       49:        char value[MAX_PATH*4];
1.1       root       50:        DWORD size = sizeof (value);
                     51: 
                     52:        strncpy (str, defaultValue, maxLen-1);
                     53: 
                     54:        ZeroMemory (value, sizeof value);
1.1.1.4   root       55:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root       56:                0, KEY_READ, &hkey) == ERROR_SUCCESS)
                     57:                if (RegQueryValueEx (hkey, name, 0,     0, (LPBYTE) &value,     &size) == ERROR_SUCCESS)
                     58:                        strncpy (str, value, maxLen-1);
                     59: 
                     60:        RegCloseKey (hkey);
                     61:        return str;
                     62: }
                     63: 
1.1.1.4   root       64: DWORD ReadRegistryBytes (char *path, char *name, char *value, int maxLen)
                     65: {
                     66:        HKEY hkey = 0;
                     67:        DWORD size = maxLen;
                     68:        BOOL success = FALSE;
                     69: 
                     70:        if (RegOpenKeyEx (HKEY_CURRENT_USER, path, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     71:                return 0;
                     72: 
                     73:        success = (RegQueryValueEx (hkey, name, 0,      0, (LPBYTE) value,      &size) == ERROR_SUCCESS);
                     74:        RegCloseKey (hkey);
                     75: 
                     76:        return success ? size : 0;
                     77: }
                     78: 
                     79: void WriteRegistryInt (char *subKey, char *name, int value)
1.1       root       80: {
                     81:        HKEY hkey = 0;
                     82:        DWORD disp;
                     83: 
1.1.1.4   root       84:        if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root       85:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                     86:                return;
                     87: 
                     88:        RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value);
                     89:        RegCloseKey (hkey);
                     90: }
                     91: 
1.1.1.9 ! root       92: BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value)
        !            93: {
        !            94:        HKEY hkey = 0;
        !            95:        DWORD disp;
        !            96: 
        !            97:        if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
        !            98:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
        !            99:                return FALSE;
        !           100: 
        !           101:        if (RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value) != ERROR_SUCCESS)
        !           102:        {
        !           103:                RegCloseKey (hkey);
        !           104:                return FALSE;
        !           105:        }
        !           106: 
        !           107:        RegCloseKey (hkey);
        !           108:        return TRUE;
        !           109: }
        !           110: 
1.1.1.4   root      111: void WriteRegistryString (char *subKey, char *name, char *str)
1.1       root      112: {
                    113:        HKEY hkey = 0;
                    114:        DWORD disp;
                    115: 
1.1.1.4   root      116:        if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root      117:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                    118:                return;
                    119: 
                    120:        RegSetValueEx (hkey, name, 0, REG_SZ, (BYTE *) str, strlen (str) + 1);
                    121:        RegCloseKey (hkey);
                    122: }
1.1.1.4   root      123: 
                    124: BOOL WriteRegistryBytes (char *path, char *name, char *str, DWORD size)
                    125: {
                    126:        HKEY hkey = 0;
                    127:        DWORD disp;
                    128:        BOOL res;
                    129: 
                    130:        if (RegCreateKeyEx (HKEY_CURRENT_USER, path,
                    131:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                    132:                return FALSE;
                    133: 
                    134:        res = RegSetValueEx (hkey, name, 0, REG_BINARY, (BYTE *) str, size);
                    135:        RegCloseKey (hkey);
                    136:        return res == ERROR_SUCCESS;
                    137: }
                    138: 
                    139: void DeleteRegistryValue (char *subKey, char *name)
                    140: {
                    141:        HKEY hkey = 0;
                    142: 
                    143:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey, 0, KEY_WRITE, &hkey) != ERROR_SUCCESS)
                    144:                return;
                    145: 
                    146:        RegDeleteValue (hkey, name);
                    147:        RegCloseKey (hkey);
                    148: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.