Cleanup of code
[openwide.git] / owSharedUtil.c
blobaa2de69ab183eba9d16f7e0713881d7fe984d3ad
1 /*
2 * Openwide -- control Windows common dialog
3 *
4 * Copyright (c) 2000 Luke Hudson
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <windows.h>
24 #include <objbase.h>
25 #include <shobjidl.h>
26 #include <shlguid.h>
27 #include <shlobj.h>
28 #include <shlwapi.h>
29 #include <shellapi.h>
30 #include <stdio.h>
31 #include "openwidedll.h"
32 //#include "openwideres.h"
33 #include "owSharedUtil.h"
36 * Printf-style logging using OutputDebugString.
37 * This is best monitored using the free DbgMon program from Sysinternals.
38 * @see www.sysinternals.com
40 void dbg(char *szError, ...)
42 #ifdef DEBUG
43 char szBuff[256];
44 va_list vl;
45 va_start(vl, szError);
46 vsnprintf(szBuff, 256, szError, vl); // print error message to string
47 OutputDebugString(szBuff);
48 va_end(vl);
49 #endif
53 * Get the text of control 'uID' from window 'hwnd'
55 char * getDlgItemText(HWND hwnd, UINT uID)
57 HWND hwCtl = GetDlgItem(hwnd, uID);
58 if(! IsWindow(hwCtl) )
59 return NULL;
60 int len = GetWindowTextLength(hwCtl);
61 if( len <= 0 )
62 return NULL;
63 char * buf = malloc(len+1);
64 if(!buf)
65 return NULL;
66 len = GetWindowText(hwCtl, buf, len+1);
67 if( len > 0 )
68 return buf;
69 free(buf);
70 return NULL;
74 /* From MS sample code */
75 DWORD GetDllVersion(LPCTSTR lpszDllName)
77 HINSTANCE hinstDll;
78 DWORD dwVersion = 0;
79 char szDll[MAX_PATH+1];
80 /* For security purposes, LoadLibrary should be provided with a
81 fully-qualified path to the DLL. The lpszDllName variable should be
82 tested to ensure that it is a fully qualified path before it is used. */
83 if( !PathSearchAndQualify(lpszDllName, szDll, MAX_PATH) )
84 return 0;
85 hinstDll = LoadLibrary(szDll);
87 if(hinstDll)
89 DLLGETVERSIONPROC pDllGetVersion;
90 pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hinstDll, "DllGetVersion");
92 /* Because some DLLs might not implement this function, you
93 must test for it explicitly. Depending on the particular
94 DLL, the lack of a DllGetVersion function can be a useful
95 indicator of the version. */
97 if(pDllGetVersion)
99 DLLVERSIONINFO dvi;
100 HRESULT hr;
102 ZeroMemory(&dvi, sizeof(dvi));
103 dvi.cbSize = sizeof(dvi);
105 hr = (*pDllGetVersion)(&dvi);
107 if(SUCCEEDED(hr))
109 dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion);
113 FreeLibrary(hinstDll);
115 return dwVersion;
119 /* Determines if winXP is running -- TODO: This may need checking for newer versions ? */
120 BOOL isWinXP(void)
122 return GetDllVersion("Shell32.dll") >= PACKVERSION(6,00);
127 /* Wrapper around RegCloseKey, used for consistency with the other reg* functions */
128 void regCloseKey(HKEY hk)
130 RegCloseKey(hk);
133 /* Create the registry subkey 'szSubKey' under the key 'hkParent'.
134 * Note that hkParent can also be one of the HKEY_* constants, such as HKEY_CURRENT_USER
136 HKEY regCreateKey(HKEY hkParent, const char *szSubKey){
137 LONG res;
138 HKEY hk;
139 res = RegCreateKeyEx(hkParent, szSubKey, 0L, NULL, REG_OPTION_NON_VOLATILE,
140 KEY_READ | KEY_WRITE, NULL, &hk, NULL);
141 return ( res == ERROR_SUCCESS ) ? hk : NULL;
144 /* Delete (recursively) the registry subkey 'szSubKey' under 'hkParent' */
145 int regDeleteKey(HKEY hkParent, const char *szSubKey)
147 DWORD rv;
148 rv = ( SHDeleteKey(hkParent, szSubKey) == ERROR_SUCCESS );
149 return rv;
152 /* Enumerate the values store within the registry key hkRoot, calling
153 * the callback function fp once for each value.
155 * The callback works as follows:
157 * TODO:: Fill this in !
159 int regEnumValues(HKEY hkRoot, RegValEnumProc fp, LPVOID param)
161 DWORD ccBuf = regGetMaxValueNameLength(hkRoot, NULL);
162 if(!ccBuf) return 0;
163 char *buf = malloc(ccBuf * sizeof(char));
164 if(!buf) return 0;
166 int i=0, res;
167 LONG rCode;
168 DWORD tmp;
169 DWORD dwType;
171 while(1)
173 tmp = ccBuf;
174 rCode = RegEnumValue(hkRoot, i, buf, &tmp, NULL, &dwType, NULL, NULL );
175 if(rCode != ERROR_SUCCESS)
177 if(rCode != ERROR_NO_MORE_ITEMS)
178 Warn("Error code %d : \r\n%s\r\non calling RegEnumValue()", rCode, geterrmsg() );
179 break;
181 res = fp(hkRoot, buf, dwType, param);
182 if(!res)
183 break;
184 i++;
186 free(buf);
187 return 1;
190 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
191 DWORD regGetDWORD(HKEY hkRoot, const char *szValue, int *pSuccess){
192 DWORD dwType, dwSize = 0;
193 LONG res;
194 DWORD dword = 0L;
196 if( pSuccess )
197 *pSuccess = 0;
198 res = RegQueryValueEx(hkRoot, szValue, NULL, &dwType, NULL, &dwSize);
199 if( res == ERROR_SUCCESS
200 && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN)
201 && dwSize == sizeof(dword))
203 res = RegQueryValueEx(hkRoot, szValue, NULL, NULL, (BYTE*)&dword, &dwSize);
204 if( res == ERROR_SUCCESS && pSuccess)
205 *pSuccess = 1;
207 return dword;
210 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
211 int regGetMaxValueNameLength(HKEY hk, LPDWORD pValueDataLen)
213 DWORD dwNLen, dwDLen;
215 if(!pValueDataLen)
216 pValueDataLen = &dwDLen;
218 if( RegQueryInfoKey(
219 hk, NULL, NULL, NULL, NULL,
220 NULL, NULL, NULL, &dwNLen, pValueDataLen,
221 NULL, NULL) == ERROR_SUCCESS )
222 return ++dwNLen;
223 return 0;
226 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
227 HKEY regOpenKey(HKEY hkParent, const char *szSubKey){
228 LONG res;
229 HKEY hk;
231 if(!szSubKey) return NULL;
232 res = RegOpenKeyEx(hkParent, szSubKey, 0L, KEY_READ | KEY_WRITE, &hk);
233 return ( res == ERROR_SUCCESS ) ? hk : NULL;
236 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
237 BYTE *regReadBinaryData(HKEY hkRoot, const char *szValueName){
238 DWORD dwType, dwSize = 0;
239 LONG res;
240 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
241 if( res == ERROR_SUCCESS && dwType == REG_BINARY && dwSize > 0)
243 BYTE * buf = malloc(dwSize);
244 if(!buf)
245 return NULL;
246 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, buf, &dwSize);
247 if( res == ERROR_SUCCESS )
248 return buf;
250 return NULL;
253 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
254 DWORD regReadDWORD(HKEY hkRoot, const char *szValueName, int *pSuccess)
256 LONG res;
257 DWORD dwType, dwSize = 0;
258 DWORD dword = 0L;
260 if( pSuccess )
261 *pSuccess = 0;
262 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
263 if( res == ERROR_SUCCESS && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN) && dwSize == sizeof(DWORD) )
265 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, (BYTE*)&dword, &dwSize);
266 if( res == ERROR_SUCCESS && pSuccess)
267 *pSuccess = 1;
269 return dword;
272 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
273 char *regReadSZ(HKEY hk, const char *szValueName){
274 LONG res;
275 DWORD dwType, dwSize = 0L;
277 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, NULL, &dwSize);
278 if( res == ERROR_SUCCESS )
280 if(dwSize > 1 && (dwType == REG_SZ || dwType == REG_EXPAND_SZ) )
282 char * szData = malloc(dwSize);
283 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, (BYTE*)szData, &dwSize);
284 if(res == ERROR_SUCCESS)
285 return szData;
288 return NULL;
291 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
292 int regWriteBinaryData(HKEY hkRoot, const char *szValue, BYTE *buf, int bufSize ){
293 LONG res;
294 res = RegSetValueEx(hkRoot, szValue, 0L, REG_BINARY, buf, bufSize);
295 return (res == ERROR_SUCCESS);
298 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
299 int regWriteDWORD(HKEY hkRoot, const char *szValue, DWORD dwData){
300 LONG res;
301 res = RegSetValueEx(hkRoot, (LPCTSTR)szValue, 0L, REG_DWORD,
302 (BYTE *)&dwData,
303 sizeof(dwData));
304 return (res == ERROR_SUCCESS);
307 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
308 int regWriteSZ(HKEY hkRoot, const char *szValueName, const char *szData){
309 LONG res;
310 if(!szData)
311 szData = "";
312 //return 1;
313 res = RegSetValueEx(hkRoot, (LPCTSTR)szValueName, 0L, REG_SZ,
314 (BYTE *)szData,
315 strlen(szData) + 1);
316 return (res == ERROR_SUCCESS);
319 /* Copied on : Tue Jul 12 19:00:24 2005 */
320 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
321 const char * geterrmsg(void)
323 static char szMsg[256];
324 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
325 FORMAT_MESSAGE_IGNORE_INSERTS,
326 NULL,
327 GetLastError(),
328 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
329 (LPTSTR) szMsg,
330 255,
331 NULL
333 int len = strlen(szMsg) - 1;
334 if(len < 0) return NULL;
335 while( szMsg[len] == '\r' || szMsg[len] == '\n'){
336 szMsg[len--] = 0;
337 if(len < 0) break;
339 return szMsg;
342 /* Copied on : Tue Jul 12 19:07:35 2005 */
343 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
344 void Warn(char *szError, ...)
346 char szBuff[256];
347 va_list vl;
348 va_start(vl, szError);
349 vsnprintf(szBuff, 256, szError, vl); // print error message to string
350 OutputDebugString(szBuff);
351 MessageBox(NULL, szBuff, "Error", MB_OK); // show message
352 va_end(vl);