From 16362c656aac5daf7337f2ed27aae1fe4838d03a Mon Sep 17 00:00:00 2001 From: Peter Rosin Date: Mon, 18 Jan 2010 09:48:23 +0100 Subject: [PATCH] Use GetErrorMode if it is available. * libltdl/loaders/loadlibrary.c (wrap_geterrormode): New function that checks if GetErrorMode is supported by the system and makes use of it if it is. (fallback_geterrormode): New function that is used otherwise that implements the old workaround. (geterrormode): New function pointer that points at either of the above or directly at GetErrorMode. (vm_open): Make use of the above. Signed-off-by: Peter Rosin --- ChangeLog | 12 +++++++++ libltdl/loaders/loadlibrary.c | 51 ++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index b244706b2..5f2518ca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2010-01-02 Peter Rosin + + Use GetErrorMode if it is available. + * libltdl/loaders/loadlibrary.c (wrap_geterrormode): New + function that checks if GetErrorMode is supported by the + system and makes use of it if it is. + (fallback_geterrormode): New function that is used otherwise + that implements the old workaround. + (geterrormode): New function pointer that points at either + of the above or directly at GetErrorMode. + (vm_open): Make use of the above. + 2010-01-11 Ralf Wildenhues Ensure functions from resident modules work in atexit handlers. diff --git a/libltdl/loaders/loadlibrary.c b/libltdl/loaders/loadlibrary.c index 97fddf461..40435fe4b 100644 --- a/libltdl/loaders/loadlibrary.c +++ b/libltdl/loaders/loadlibrary.c @@ -1,7 +1,7 @@ /* loader-loadlibrary.c -- dynamic linking for Win32 Copyright (C) 1998, 1999, 2000, 2004, 2005, 2006, - 2007, 2008 Free Software Foundation, Inc. + 2007, 2008, 2010 Free Software Foundation, Inc. Written by Thomas Tanner, 1998 NOTE: The canonical source of this file is maintained with the @@ -98,6 +98,14 @@ get_vtable (lt_user_data loader_data) #include +static UINT WINAPI wrap_geterrormode (void); +static UINT WINAPI fallback_geterrormode (void); + +typedef UINT (WINAPI geterrormode_type) (void); + +static geterrormode_type *geterrormode = wrap_geterrormode; + + /* A function called through the vtable when this loader is no longer needed by the application. */ static int @@ -170,16 +178,14 @@ vm_open (lt_user_data LT__UNUSED loader_data, const char *filename, } { - /* Silence dialog from LoadLibrary on some failures. - No way to get the error mode, but to set it, - so set it twice to preserve any previous flags. */ - UINT errormode = SetErrorMode(SEM_FAILCRITICALERRORS); - SetErrorMode(errormode | SEM_FAILCRITICALERRORS); + /* Silence dialog from LoadLibrary on some failures. */ + UINT errormode = geterrormode (); + SetErrorMode (errormode | SEM_FAILCRITICALERRORS); module = LoadLibrary (wpath); /* Restore the error mode. */ - SetErrorMode(errormode); + SetErrorMode (errormode); } /* libltdl expects this function to fail if it is unable @@ -249,3 +255,34 @@ vm_sym (lt_user_data LT__UNUSED loader_data, lt_module module, const char *name) return address; } + + + +/* --- HELPER FUNCTIONS --- */ + + +/* A function called through the geterrormode variable which checks + if the system supports GetErrorMode and arranges for it or a + fallback implementation to be called directly in the future. The + selected version is then called. */ +static UINT WINAPI +wrap_geterrormode (void) +{ + HMODULE kernel32 = GetModuleHandleA ("kernel32.dll"); + geterrormode = (geterrormode_type *) GetProcAddress (kernel32, + "GetErrorMode"); + if (!geterrormode) + geterrormode = fallback_geterrormode; + return geterrormode (); +} + +/* A function called through the geterrormode variable for cases + where the system does not support GetErrorMode */ +static UINT WINAPI +fallback_geterrormode (void) +{ + /* Prior to Windows Vista, the only way to get the current error + mode was to set a new one. In our case, we are setting a new + error mode right after "getting" it, so that's fairly ok. */ + return SetErrorMode (SEM_FAILCRITICALERRORS); +} -- 2.47.2