MINOR: Change output filename
[openwide.git] / owSharedUtil.c
blob004d8d6af2b71300f00cb4f840fb6411a718ce42
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);
125 /* Determines if windows 7 is running -- TODO: This may need checking for newer versions ? */
126 BOOL DLLEXPORT isWin7(void)
128 return GetDllVersion("Shell32.dll") >= PACKVERSION(6, 01);
132 /* Wrapper around RegCloseKey, used for consistency with the other reg* functions */
133 void regCloseKey(HKEY hk)
135 RegCloseKey(hk);
138 /* Create the registry subkey 'szSubKey' under the key 'hkParent'.
139 * Note that hkParent can also be one of the HKEY_* constants, such as HKEY_CURRENT_USER
141 HKEY regCreateKey(HKEY hkParent, const char *szSubKey){
142 LONG res;
143 HKEY hk;
144 res = RegCreateKeyEx(hkParent, szSubKey, 0L, NULL, REG_OPTION_NON_VOLATILE,
145 KEY_READ | KEY_WRITE, NULL, &hk, NULL);
146 return ( res == ERROR_SUCCESS ) ? hk : NULL;
149 /* Delete (recursively) the registry subkey 'szSubKey' under 'hkParent' */
150 int regDeleteKey(HKEY hkParent, const char *szSubKey)
152 DWORD rv;
153 rv = ( SHDeleteKey(hkParent, szSubKey) == ERROR_SUCCESS );
154 return rv;
157 /* Enumerate the values store within the registry key hkRoot, calling
158 * the callback function fp once for each value.
160 * The callback works as follows:
162 * TODO:: Fill this in !
164 int regEnumValues(HKEY hkRoot, RegValEnumProc fp, LPVOID param)
166 DWORD ccBuf = regGetMaxValueNameLength(hkRoot, NULL);
167 if(!ccBuf) return 0;
168 char *buf = malloc(ccBuf * sizeof(char));
169 if(!buf) return 0;
171 int i=0, res;
172 LONG rCode;
173 DWORD tmp;
174 DWORD dwType;
176 while(1)
178 tmp = ccBuf;
179 rCode = RegEnumValue(hkRoot, i, buf, &tmp, NULL, &dwType, NULL, NULL );
180 if(rCode != ERROR_SUCCESS)
182 if(rCode != ERROR_NO_MORE_ITEMS)
183 Warn("Error code %d : \r\n%s\r\non calling RegEnumValue()", rCode, geterrmsg() );
184 break;
186 res = fp(hkRoot, buf, dwType, param);
187 if(!res)
188 break;
189 i++;
191 free(buf);
192 return 1;
196 DWORD regGetDWORD(HKEY hkRoot, const char *szValue, int *pSuccess){
197 DWORD dwType, dwSize = 0;
198 LONG res;
199 DWORD dword = 0L;
201 if( pSuccess )
202 *pSuccess = 0;
203 res = RegQueryValueEx(hkRoot, szValue, NULL, &dwType, NULL, &dwSize);
204 if( res == ERROR_SUCCESS
205 && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN)
206 && dwSize == sizeof(dword))
208 res = RegQueryValueEx(hkRoot, szValue, NULL, NULL, (BYTE*)&dword, &dwSize);
209 if( res == ERROR_SUCCESS && pSuccess)
210 *pSuccess = 1;
212 return dword;
216 int regGetMaxValueNameLength(HKEY hk, LPDWORD pValueDataLen)
218 DWORD dwNLen, dwDLen;
220 if(!pValueDataLen)
221 pValueDataLen = &dwDLen;
223 if( RegQueryInfoKey(
224 hk, NULL, NULL, NULL, NULL,
225 NULL, NULL, NULL, &dwNLen, pValueDataLen,
226 NULL, NULL) == ERROR_SUCCESS )
227 return ++dwNLen;
228 return 0;
232 HKEY regOpenKey(HKEY hkParent, const char *szSubKey){
233 LONG res;
234 HKEY hk;
236 if(!szSubKey) return NULL;
237 res = RegOpenKeyEx(hkParent, szSubKey, 0L, KEY_READ | KEY_WRITE, &hk);
238 return ( res == ERROR_SUCCESS ) ? hk : NULL;
242 BYTE *regReadBinaryData(HKEY hkRoot, const char *szValueName){
243 DWORD dwType, dwSize = 0;
244 LONG res;
245 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
246 if( res == ERROR_SUCCESS && dwType == REG_BINARY && dwSize > 0)
248 BYTE * buf = malloc(dwSize);
249 if(!buf)
250 return NULL;
251 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, buf, &dwSize);
252 if( res == ERROR_SUCCESS )
253 return buf;
255 return NULL;
259 DWORD regReadDWORD(HKEY hkRoot, const char *szValueName, int *pSuccess)
261 LONG res;
262 DWORD dwType, dwSize = 0;
263 DWORD dword = 0L;
265 if( pSuccess )
266 *pSuccess = 0;
267 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
268 if( res == ERROR_SUCCESS && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN) && dwSize == sizeof(DWORD) )
270 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, (BYTE*)&dword, &dwSize);
271 if( res == ERROR_SUCCESS && pSuccess)
272 *pSuccess = 1;
274 return dword;
278 char *regReadSZ(HKEY hk, const char *szValueName){
279 LONG res;
280 DWORD dwType, dwSize = 0L;
282 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, NULL, &dwSize);
283 if( res == ERROR_SUCCESS )
285 if(dwSize > 1 && (dwType == REG_SZ || dwType == REG_EXPAND_SZ) )
287 char * szData = malloc(dwSize);
288 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, (BYTE*)szData, &dwSize);
289 if(res == ERROR_SUCCESS)
290 return szData;
293 return NULL;
297 int regWriteBinaryData(HKEY hkRoot, const char *szValue, BYTE *buf, int bufSize ){
298 LONG res;
299 res = RegSetValueEx(hkRoot, szValue, 0L, REG_BINARY, buf, bufSize);
300 return (res == ERROR_SUCCESS);
304 int regWriteDWORD(HKEY hkRoot, const char *szValue, DWORD dwData){
305 LONG res;
306 res = RegSetValueEx(hkRoot, (LPCTSTR)szValue, 0L, REG_DWORD,
307 (BYTE *)&dwData,
308 sizeof(dwData));
309 return (res == ERROR_SUCCESS);
313 int regWriteSZ(HKEY hkRoot, const char *szValueName, const char *szData){
314 LONG res;
315 if(!szData)
316 szData = "";
317 //return 1;
318 res = RegSetValueEx(hkRoot, (LPCTSTR)szValueName, 0L, REG_SZ,
319 (BYTE *)szData,
320 strlen(szData) + 1);
321 return (res == ERROR_SUCCESS);
326 const char * geterrmsg(void)
328 static char szMsg[256];
329 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
330 FORMAT_MESSAGE_IGNORE_INSERTS,
331 NULL,
332 GetLastError(),
333 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
334 (LPTSTR) szMsg,
335 255,
336 NULL
338 int len = strlen(szMsg) - 1;
339 if(len < 0) return NULL;
340 while( szMsg[len] == '\r' || szMsg[len] == '\n'){
341 szMsg[len--] = 0;
342 if(len < 0) break;
344 return szMsg;
349 void Warn(char *szError, ...)
351 char szBuff[256];
352 va_list vl;
353 va_start(vl, szError);
354 vsnprintf(szBuff, 256, szError, vl); // print error message to string
355 OutputDebugString(szBuff);
356 MessageBox(NULL, szBuff, "Error", MB_OK); // show message
357 va_end(vl);