+
+/*****************************************************************************
+ * CloseModule: unload a dynamic library
+ *****************************************************************************
+ * This function unloads a previously opened dynamically linked library
+ * using a system dependant method. No return value is taken in consideration,
+ * since some libraries sometimes refuse to close properly.
+ *****************************************************************************/
+static void CloseModule( module_handle_t handle )
+{
+#if defined(HAVE_DL_DYLD)
+ NSUnLinkModule( handle, FALSE );
+
+#elif defined(HAVE_DL_BEOS)
+ unload_add_on( handle );
+
+#elif defined(HAVE_DL_WINDOWS)
+ FreeLibrary( handle );
+
+#elif defined(HAVE_DL_DLOPEN)
+ dlclose( handle );
+
+#elif defined(HAVE_DL_SHL_LOAD)
+ shl_unload( handle );
+
+#endif
+ return;
+}
+
+/*****************************************************************************
+ * GetSymbol: get a symbol from a dynamic library
+ *****************************************************************************
+ * This function queries a loaded library for a symbol specified in a
+ * string, and returns a pointer to it. We don't check for dlerror() or
+ * similar functions, since we want a non-NULL symbol anyway.
+ *****************************************************************************/
+static void * _module_getsymbol( module_handle_t, const char * );
+
+static void * GetSymbol( module_handle_t handle, const char * psz_function )
+{
+ void * p_symbol = _module_getsymbol( handle, psz_function );
+
+ /* MacOS X dl library expects symbols to begin with "_". So do
+ * some other operating systems. That's really lame, but hey, what
+ * can we do ? */
+ if( p_symbol == NULL )
+ {
+ char *psz_call = malloc( strlen( psz_function ) + 2 );
+
+ strcpy( psz_call + 1, psz_function );
+ psz_call[ 0 ] = '_';
+ p_symbol = _module_getsymbol( handle, psz_call );
+ free( psz_call );
+ }
+
+ return p_symbol;
+}
+
+static void * _module_getsymbol( module_handle_t handle,
+ const char * psz_function )
+{
+#if defined(HAVE_DL_DYLD)
+ NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
+ return NSAddressOfSymbol( sym );
+
+#elif defined(HAVE_DL_BEOS)
+ void * p_symbol;
+ if( B_OK == get_image_symbol( handle, psz_function,
+ B_SYMBOL_TYPE_TEXT, &p_symbol ) )
+ {
+ return p_symbol;
+ }
+ else
+ {
+ return NULL;
+ }
+
+#elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
+ wchar_t psz_real[256];
+ MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
+
+ return (void *)GetProcAddress( handle, psz_real );
+
+#elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
+ return (void *)GetProcAddress( handle, (MYCHAR*)psz_function );
+
+#elif defined(HAVE_DL_DLOPEN)
+ return dlsym( handle, psz_function );
+
+#elif defined(HAVE_DL_SHL_LOAD)
+ void *p_sym;
+ shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
+ return p_sym;
+
+#endif
+}
+
+#if defined(HAVE_DL_WINDOWS)
+static char * GetWindowsError( void )
+{
+#if defined(UNDER_CE)
+ wchar_t psz_tmp[256];
+ char * psz_buffer = malloc( 256 );
+#else
+ char * psz_tmp = malloc( 256 );
+#endif
+ int i = 0, i_error = GetLastError();
+
+ FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR) psz_tmp, 256, NULL );
+
+ /* Go to the end of the string */
+#if defined(UNDER_CE)
+ while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
+#else
+ while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
+#endif
+ {
+ i++;
+ }
+
+ if( psz_tmp[i] )
+ {
+#if defined(UNDER_CE)
+ swprintf( psz_tmp + i, L" (error %i)", i_error );
+ psz_tmp[ 255 ] = L'\0';
+#else
+ snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
+ psz_tmp[ 255 ] = '\0';
+#endif
+ }
+
+#if defined(UNDER_CE)
+ WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
+ psz_buffer, 256, NULL, NULL );
+ return psz_buffer;
+#else
+ return psz_tmp;
+#endif
+}
+#endif /* HAVE_DL_WINDOWS */
+