2 * Openwide -- control Windows common dialog
4 * Copyright (c) 2000 Luke Hudson
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.
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
, ...)
45 va_start(vl
, szError
);
46 vsnprintf(szBuff
, 256, szError
, vl
); // print error message to string
47 OutputDebugString(szBuff
);
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
) )
60 int len
= GetWindowTextLength(hwCtl
);
63 char * buf
= malloc(len
+1);
66 len
= GetWindowText(hwCtl
, buf
, len
+1);
74 /* From MS sample code */
75 DWORD
GetDllVersion(LPCTSTR lpszDllName
)
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
) )
85 hinstDll
= LoadLibrary(szDll
);
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. */
102 ZeroMemory(&dvi
, sizeof(dvi
));
103 dvi
.cbSize
= sizeof(dvi
);
105 hr
= (*pDllGetVersion
)(&dvi
);
109 dwVersion
= PACKVERSION(dvi
.dwMajorVersion
, dvi
.dwMinorVersion
);
113 FreeLibrary(hinstDll
);
119 /* Determines if winXP is running -- TODO: This may need checking for newer versions ? */
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
)
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
){
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
)
148 rv
= ( SHDeleteKey(hkParent
, szSubKey
) == ERROR_SUCCESS
);
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
);
163 char *buf
= malloc(ccBuf
* sizeof(char));
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() );
181 res
= fp(hkRoot
, buf
, dwType
, param
);
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;
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
)
210 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
211 int regGetMaxValueNameLength(HKEY hk
, LPDWORD pValueDataLen
)
213 DWORD dwNLen
, dwDLen
;
216 pValueDataLen
= &dwDLen
;
219 hk
, NULL
, NULL
, NULL
, NULL
,
220 NULL
, NULL
, NULL
, &dwNLen
, pValueDataLen
,
221 NULL
, NULL
) == ERROR_SUCCESS
)
226 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
227 HKEY
regOpenKey(HKEY hkParent
, const char *szSubKey
){
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;
240 res
= RegQueryValueEx(hkRoot
, szValueName
, NULL
, &dwType
, NULL
, &dwSize
);
241 if( res
== ERROR_SUCCESS
&& dwType
== REG_BINARY
&& dwSize
> 0)
243 BYTE
* buf
= malloc(dwSize
);
246 res
= RegQueryValueEx(hkRoot
, szValueName
, NULL
, NULL
, buf
, &dwSize
);
247 if( res
== ERROR_SUCCESS
)
253 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
254 DWORD
regReadDWORD(HKEY hkRoot
, const char *szValueName
, int *pSuccess
)
257 DWORD dwType
, dwSize
= 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
)
272 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
273 char *regReadSZ(HKEY hk
, const char *szValueName
){
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
)
291 /** Function source : C:\Data\Code\C\openwide\owUtil.c */
292 int regWriteBinaryData(HKEY hkRoot
, const char *szValue
, BYTE
*buf
, int bufSize
){
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
){
301 res
= RegSetValueEx(hkRoot
, (LPCTSTR
)szValue
, 0L, REG_DWORD
,
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
){
313 res
= RegSetValueEx(hkRoot
, (LPCTSTR
)szValueName
, 0L, REG_SZ
,
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
,
328 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
333 int len
= strlen(szMsg
) - 1;
334 if(len
< 0) return NULL
;
335 while( szMsg
[len
] == '\r' || szMsg
[len
] == '\n'){
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
, ...)
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