Commit 3be44a48c3bd34a7a710bbf32d9b898a21ecf2eb

Authored by Perry Werneck
1 parent 6198bbcb

Adding auto-cleanup macro for windows registry.

src/core/host.c
... ... @@ -57,6 +57,7 @@
57 57  
58 58 #include <errno.h>
59 59 #include <lib3270/internals.h>
  60 +#include <lib3270/os.h>
60 61 #include <lib3270/properties.h>
61 62 #include <lib3270/log.h>
62 63 #include <lib3270/trace.h>
... ... @@ -321,7 +322,7 @@ LIB3270_EXPORT const char * lib3270_get_default_host(const H3270 GNUC_UNUSED(*hS
321 322 {
322 323 #ifdef _WIN32
323 324 {
324   - HKEY hKey;
  325 + lib3270_auto_cleanup(HKEY) hKey;
325 326 DWORD disp = 0;
326 327 LSTATUS rc = RegCreateKeyEx(
327 328 HKEY_LOCAL_MACHINE,
... ... @@ -350,8 +351,6 @@ LIB3270_EXPORT const char * lib3270_get_default_host(const H3270 GNUC_UNUSED(*hS
350 351  
351 352 DWORD dwRet = RegQueryValueEx(hKey,"host",NULL,NULL,(LPBYTE) default_host, &cbData);
352 353  
353   - RegCloseKey(hKey);
354   -
355 354 if(dwRet == ERROR_SUCCESS)
356 355 {
357 356 default_host = (char *) realloc(default_host,cbData+1);
... ...
src/core/session.c
... ... @@ -359,7 +359,7 @@ static void lib3270_session_init(H3270 *hSession, const char *model, const char
359 359 #ifdef _WIN32
360 360 // Get defaults from registry.
361 361 {
362   - HKEY hKey;
  362 + lib3270_auto_cleanup(HKEY) hKey = 0;
363 363 DWORD disp = 0;
364 364 LSTATUS rc = RegCreateKeyEx(
365 365 HKEY_LOCAL_MACHINE,
... ... @@ -398,7 +398,6 @@ static void lib3270_session_init(H3270 *hSession, const char *model, const char
398 398  
399 399 }
400 400  
401   - RegCloseKey(hKey);
402 401 }
403 402 }
404 403 #endif // _WIN32
... ...
src/core/toggles/init.c
... ... @@ -163,7 +163,7 @@ void initialize_toggles(H3270 *session)
163 163  
164 164 #ifdef _WIN32
165 165 {
166   - HKEY hKey;
  166 + lib3270_auto_cleanup(HKEY) hKey = 0;
167 167 DWORD disp = 0;
168 168 LSTATUS rc = RegCreateKeyEx(
169 169 HKEY_LOCAL_MACHINE,
... ... @@ -201,7 +201,6 @@ void initialize_toggles(H3270 *session)
201 201 }
202 202  
203 203 }
204   - RegCloseKey(hKey);
205 204 }
206 205  
207 206 }
... ...
src/include/lib3270.h
... ... @@ -56,6 +56,7 @@
56 56  
57 57 #define LIB3270_AUTOPTR_FUNC_NAME(TypeName) lib3270_autoptr_cleanup_##TypeName
58 58 #define lib3270_autoptr(TypeName) TypeName * __attribute__ ((__cleanup__(LIB3270_AUTOPTR_FUNC_NAME(TypeName))))
  59 + #define lib3270_auto_cleanup(TypeName) TypeName __attribute__ ((__cleanup__(LIB3270_AUTOPTR_FUNC_NAME(TypeName))))
59 60  
60 61 #endif // __GNUC__
61 62  
... ...
src/include/windows/lib3270/os.h
... ... @@ -41,4 +41,11 @@
41 41 #include <winsock2.h>
42 42 #include <windows.h>
43 43  
  44 + inline void lib3270_autoptr_cleanup_HKEY(HKEY *hKey) {
  45 + if(*hKey) {
  46 + RegCloseKey(*hKey);
  47 + *hKey = 0;
  48 + }
  49 + }
  50 +
44 51 #endif // LIB3270_OS_H_INCLUDED
... ...