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);
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
)
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
){
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
)
153 rv
= ( SHDeleteKey(hkParent
, szSubKey
) == ERROR_SUCCESS
);
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
);
168 char *buf
= malloc(ccBuf
* sizeof(char));
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() );
186 res
= fp(hkRoot
, buf
, dwType
, param
);
196 DWORD
regGetDWORD(HKEY hkRoot
, const char *szValue
, int *pSuccess
){
197 DWORD dwType
, dwSize
= 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
)
216 int regGetMaxValueNameLength(HKEY hk
, LPDWORD pValueDataLen
)
218 DWORD dwNLen
, dwDLen
;
221 pValueDataLen
= &dwDLen
;
224 hk
, NULL
, NULL
, NULL
, NULL
,
225 NULL
, NULL
, NULL
, &dwNLen
, pValueDataLen
,
226 NULL
, NULL
) == ERROR_SUCCESS
)
232 HKEY
regOpenKey(HKEY hkParent
, const char *szSubKey
){
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;
245 res
= RegQueryValueEx(hkRoot
, szValueName
, NULL
, &dwType
, NULL
, &dwSize
);
246 if( res
== ERROR_SUCCESS
&& dwType
== REG_BINARY
&& dwSize
> 0)
248 BYTE
* buf
= malloc(dwSize
);
251 res
= RegQueryValueEx(hkRoot
, szValueName
, NULL
, NULL
, buf
, &dwSize
);
252 if( res
== ERROR_SUCCESS
)
259 DWORD
regReadDWORD(HKEY hkRoot
, const char *szValueName
, int *pSuccess
)
262 DWORD dwType
, dwSize
= 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
)
278 char *regReadSZ(HKEY hk
, const char *szValueName
){
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
)
297 int regWriteBinaryData(HKEY hkRoot
, const char *szValue
, BYTE
*buf
, int bufSize
){
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
){
306 res
= RegSetValueEx(hkRoot
, (LPCTSTR
)szValue
, 0L, REG_DWORD
,
309 return (res
== ERROR_SUCCESS
);
313 int regWriteSZ(HKEY hkRoot
, const char *szValueName
, const char *szData
){
318 res
= RegSetValueEx(hkRoot
, (LPCTSTR
)szValueName
, 0L, REG_SZ
,
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
,
333 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
338 int len
= strlen(szMsg
) - 1;
339 if(len
< 0) return NULL
;
340 while( szMsg
[len
] == '\r' || szMsg
[len
] == '\n'){
349 void Warn(char *szError
, ...)
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