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