6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
33 static BOOL
CheckResult(LONG error
)
35 if (error
!= ERROR_SUCCESS
) {
37 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
38 0, error
, MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
), (PTSTR
)&msg
, 0, NULL
))
39 MessageBox(NULL
, msg
, szTitle
, MB_ICONERROR
| MB_OK
);
41 MessageBox(NULL
, _T("Error"), szTitle
, MB_ICONERROR
| MB_OK
);
48 static BOOL
CreateRegistryPath(LPTSTR szRegPath
, int nMaxLen
)
50 LPTSTR pRegPath
= szRegPath
;
52 // Initialise registry path string from application PATH and KEY resources
53 int nLength
= LoadString(hInst
, IDS_APP_REG_PATH
, szRegPath
, nMaxLen
);
54 nLength
+= LoadString(hInst
, IDS_APP_REG_KEY
, szRegPath
+ nLength
, nMaxLen
- nLength
);
55 ASSERT(nLength
< (nMaxLen
- 1));
56 szRegPath
[nLength
] = _T('\\');
58 // walk the registry path string creating the tree if required
59 while (pRegPath
= _tcschr(pRegPath
, _T('\\'))) {
63 // Open (or create) the key
64 result
= RegCreateKeyEx(HKEY_CURRENT_USER
, szRegPath
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
);
65 if (!CheckResult(result
)) return FALSE
;
68 pRegPath
= pRegPath
+ 1;
70 szRegPath
[nLength
] = _T('\0');
74 void LoadSettings(void)
76 TCHAR szRegPath
[MAX_LOADSTRING
];
82 if (!CreateRegistryPath(szRegPath
, MAX_LOADSTRING
)) return;
85 result
= RegOpenKeyEx(HKEY_CURRENT_USER
, szRegPath
, 0, KEY_READ
, &hKey
);
86 if (!CheckResult(result
)) return;
89 dwSize
= sizeof(CALC_TYPES
);
90 result
= RegQueryValueEx(hKey
, _T("Preferences"), NULL
, NULL
, (LPBYTE
)&CalcType
, &dwSize
);
96 void SaveSettings(void)
98 TCHAR szRegPath
[MAX_LOADSTRING
];
102 if (!CreateRegistryPath(szRegPath
, MAX_LOADSTRING
)) return;
105 result
= RegOpenKeyEx(HKEY_CURRENT_USER
, szRegPath
, 0, KEY_WRITE
, &hKey
);
106 if (!CheckResult(result
)) return;
109 result
= RegSetValueEx(hKey
, _T("Preferences"), 0, REG_DWORD
, (LPBYTE
)&CalcType
, sizeof(CALC_TYPES
));
110 if (!CheckResult(result
)) goto abort
;