Release/: Updated readme.txt and added gpl-2.0.txt
[openwide.git] / owSharedUtil.c
blobbf49c04ec5667511f70c06aee82de7d0d6445ac3
1 #include <windows.h>
2 /**
3 * @author Luke Hudson
4 * @licence GPL2
5 */
7 #include <objbase.h>
8 #include <shobjidl.h>
9 #include <shlguid.h>
10 #include <shlobj.h>
11 #include <shlwapi.h>
12 #include <shellapi.h>
13 #include <stdio.h>
14 #include "openwidedll.h"
15 //#include "openwideres.h"
16 #include "owSharedUtil.h"
20 void dbg(char *szError, ...)
22 #ifdef DEBUG
23 char szBuff[256];
24 va_list vl;
25 va_start(vl, szError);
26 vsnprintf(szBuff, 256, szError, vl); // print error message to string
27 OutputDebugString(szBuff);
28 va_end(vl);
29 #endif
34 char * getDlgItemText(HWND hwnd, UINT uID)
36 HWND hwCtl = GetDlgItem(hwnd, uID);
37 if(! IsWindow(hwCtl) )
38 return NULL;
39 int len = GetWindowTextLength(hwCtl);
40 if( len <= 0 )
41 return NULL;
42 char * buf = malloc(len+1);
43 if(!buf)
44 return NULL;
45 len = GetWindowText(hwCtl, buf, len+1);
46 if( len > 0 )
47 return buf;
48 free(buf);
49 return NULL;
55 DWORD GetDllVersion(LPCTSTR lpszDllName)
57 HINSTANCE hinstDll;
58 DWORD dwVersion = 0;
59 char szDll[MAX_PATH+1];
60 /* For security purposes, LoadLibrary should be provided with a
61 fully-qualified path to the DLL. The lpszDllName variable should be
62 tested to ensure that it is a fully qualified path before it is used. */
63 if( !PathSearchAndQualify(lpszDllName, szDll, MAX_PATH) )
64 return 0;
65 hinstDll = LoadLibrary(szDll);
67 if(hinstDll)
69 DLLGETVERSIONPROC pDllGetVersion;
70 pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hinstDll, "DllGetVersion");
72 /* Because some DLLs might not implement this function, you
73 must test for it explicitly. Depending on the particular
74 DLL, the lack of a DllGetVersion function can be a useful
75 indicator of the version. */
77 if(pDllGetVersion)
79 DLLVERSIONINFO dvi;
80 HRESULT hr;
82 ZeroMemory(&dvi, sizeof(dvi));
83 dvi.cbSize = sizeof(dvi);
85 hr = (*pDllGetVersion)(&dvi);
87 if(SUCCEEDED(hr))
89 dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion);
93 FreeLibrary(hinstDll);
95 return dwVersion;
99 BOOL isWinXP(void)
101 return GetDllVersion("Shell32.dll") >= PACKVERSION(6,00);
108 void regCloseKey(HKEY hk)
110 RegCloseKey(hk);
114 HKEY regCreateKey(HKEY hkParent, const char *szSubKey){
115 LONG res;
116 HKEY hk;
117 res = RegCreateKeyEx(hkParent, szSubKey, 0L, NULL, REG_OPTION_NON_VOLATILE,
118 KEY_READ | KEY_WRITE, NULL, &hk, NULL);
119 return ( res == ERROR_SUCCESS ) ? hk : NULL;
123 int regDeleteKey(HKEY hkParent, const char *szSubKey)
125 DWORD rv;
126 rv = ( SHDeleteKey(hkParent, szSubKey) == ERROR_SUCCESS );
127 return rv;
131 int regEnumValues(HKEY hkRoot, RegValEnumProc fp, LPVOID param)
133 DWORD ccBuf = regGetMaxValueNameLength(hkRoot, NULL);
134 if(!ccBuf) return 0;
135 char *buf = malloc(ccBuf * sizeof(char));
136 if(!buf) return 0;
138 int i=0, res;
139 LONG rCode;
140 DWORD tmp;
141 DWORD dwType;
143 while(1)
145 tmp = ccBuf;
146 rCode = RegEnumValue(hkRoot, i, buf, &tmp, NULL, &dwType, NULL, NULL );
147 if(rCode != ERROR_SUCCESS)
149 if(rCode != ERROR_NO_MORE_ITEMS)
150 Warn("Error code %d : \r\n%s\r\non calling RegEnumValue()", rCode, geterrmsg() );
151 break;
153 res = fp(hkRoot, buf, dwType, param);
154 if(!res)
155 break;
156 i++;
158 free(buf);
159 return 1;
163 DWORD regGetDWORD(HKEY hkRoot, const char *szValue, int *pSuccess){
164 DWORD dwType, dwSize = 0;
165 LONG res;
166 DWORD dword = 0L;
168 if( pSuccess )
169 *pSuccess = 0;
170 res = RegQueryValueEx(hkRoot, szValue, NULL, &dwType, NULL, &dwSize);
171 if( res == ERROR_SUCCESS
172 && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN)
173 && dwSize == sizeof(dword))
175 res = RegQueryValueEx(hkRoot, szValue, NULL, NULL, (BYTE*)&dword, &dwSize);
176 if( res == ERROR_SUCCESS && pSuccess)
177 *pSuccess = 1;
179 return dword;
183 int regGetMaxValueNameLength(HKEY hk, LPDWORD pValueDataLen)
185 DWORD dwNLen, dwDLen;
187 if(!pValueDataLen)
188 pValueDataLen = &dwDLen;
190 if( RegQueryInfoKey(
191 hk, NULL, NULL, NULL, NULL,
192 NULL, NULL, NULL, &dwNLen, pValueDataLen,
193 NULL, NULL) == ERROR_SUCCESS )
194 return ++dwNLen;
195 return 0;
199 HKEY regOpenKey(HKEY hkParent, const char *szSubKey){
200 LONG res;
201 HKEY hk;
203 if(!szSubKey) return NULL;
204 res = RegOpenKeyEx(hkParent, szSubKey, 0L, KEY_READ | KEY_WRITE, &hk);
205 return ( res == ERROR_SUCCESS ) ? hk : NULL;
209 BYTE *regReadBinaryData(HKEY hkRoot, const char *szValueName){
210 DWORD dwType, dwSize = 0;
211 LONG res;
212 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
213 if( res == ERROR_SUCCESS && dwType == REG_BINARY && dwSize > 0)
215 BYTE * buf = malloc(dwSize);
216 if(!buf)
217 return NULL;
218 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, buf, &dwSize);
219 if( res == ERROR_SUCCESS )
220 return buf;
222 return NULL;
226 DWORD regReadDWORD(HKEY hkRoot, const char *szValueName, int *pSuccess)
228 LONG res;
229 DWORD dwType, dwSize = 0;
230 DWORD dword = 0L;
232 if( pSuccess )
233 *pSuccess = 0;
234 res = RegQueryValueEx(hkRoot, szValueName, NULL, &dwType, NULL, &dwSize);
235 if( res == ERROR_SUCCESS && (dwType == REG_DWORD || REG_DWORD_LITTLE_ENDIAN) && dwSize == sizeof(DWORD) )
237 res = RegQueryValueEx(hkRoot, szValueName, NULL, NULL, (BYTE*)&dword, &dwSize);
238 if( res == ERROR_SUCCESS && pSuccess)
239 *pSuccess = 1;
241 return dword;
245 char *regReadSZ(HKEY hk, const char *szValueName){
246 LONG res;
247 DWORD dwType, dwSize = 0L;
249 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, NULL, &dwSize);
250 if( res == ERROR_SUCCESS )
252 if(dwSize > 1 && (dwType == REG_SZ || dwType == REG_EXPAND_SZ) )
254 char * szData = malloc(dwSize);
255 res = RegQueryValueEx(hk, szValueName, NULL, &dwType, (BYTE*)szData, &dwSize);
256 if(res == ERROR_SUCCESS)
257 return szData;
260 return NULL;
264 int regWriteBinaryData(HKEY hkRoot, const char *szValue, BYTE *buf, int bufSize ){
265 LONG res;
266 res = RegSetValueEx(hkRoot, szValue, 0L, REG_BINARY, buf, bufSize);
267 return (res == ERROR_SUCCESS);
271 int regWriteDWORD(HKEY hkRoot, const char *szValue, DWORD dwData){
272 LONG res;
273 res = RegSetValueEx(hkRoot, (LPCTSTR)szValue, 0L, REG_DWORD,
274 (BYTE *)&dwData,
275 sizeof(dwData));
276 return (res == ERROR_SUCCESS);
280 int regWriteSZ(HKEY hkRoot, const char *szValueName, const char *szData){
281 LONG res;
282 if(!szData)
283 szData = "";
284 //return 1;
285 res = RegSetValueEx(hkRoot, (LPCTSTR)szValueName, 0L, REG_SZ,
286 (BYTE *)szData,
287 strlen(szData) + 1);
288 return (res == ERROR_SUCCESS);
293 const char * geterrmsg(void)
295 static char szMsg[256];
296 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
297 FORMAT_MESSAGE_IGNORE_INSERTS,
298 NULL,
299 GetLastError(),
300 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
301 (LPTSTR) szMsg,
302 255,
303 NULL
305 int len = strlen(szMsg) - 1;
306 if(len < 0) return NULL;
307 while( szMsg[len] == '\r' || szMsg[len] == '\n'){
308 szMsg[len--] = 0;
309 if(len < 0) break;
311 return szMsg;
316 void Warn(char *szError, ...)
318 char szBuff[256];
319 va_list vl;
320 va_start(vl, szError);
321 _vsnprintf(szBuff, 256, szError, vl); // print error message to string
322 OutputDebugString(szBuff);
323 MessageBox(NULL, szBuff, "Error", MB_OK); // show message
324 va_end(vl);