00001 /****************************************************************************** 00002 * $Id: cplgetsymbol_cpp-source.html,v 1.8 2001/07/05 13:24:08 warmerda Exp $ 00003 * 00004 * Project: Common Portability Library 00005 * Purpose: Fetch a function pointer from a shared library / DLL. 00006 * Author: Frank Warmerdam, warmerda@home.com 00007 * 00008 ****************************************************************************** 00009 * Copyright (c) 1999, Frank Warmerdam 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a 00012 * copy of this software and associated documentation files (the "Software"), 00013 * to deal in the Software without restriction, including without limitation 00014 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00015 * and/or sell copies of the Software, and to permit persons to whom the 00016 * Software is furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included 00019 * in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00022 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00024 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00026 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00027 * DEALINGS IN THE SOFTWARE. 00028 ****************************************************************************** 00029 * 00030 * $Log: cplgetsymbol_cpp-source.html,v $ 00030 * Revision 1.8 2001/07/05 13:24:08 warmerda 00030 * *** empty log message *** 00030 * 00031 * Revision 1.10 2001/01/19 21:16:41 warmerda 00032 * expanded tabs 00033 * 00034 * Revision 1.9 2000/09/25 19:59:03 warmerda 00035 * look for WIN32 not _WIN32 00036 * 00037 * Revision 1.8 1999/05/20 02:54:38 warmerda 00038 * Added API documentation 00039 * 00040 * Revision 1.7 1999/04/23 13:56:36 warmerda 00041 * added stub implementation. Don't check for __unix__ 00042 * 00043 * Revision 1.6 1999/04/21 20:06:05 warmerda 00044 * Removed incorrect comment. 00045 * 00046 * Revision 1.5 1999/03/02 21:20:00 warmerda 00047 * test for dlfcn.h, not -ldl 00048 * 00049 * Revision 1.4 1999/03/02 21:08:11 warmerda 00050 * autoconf switch 00051 * 00052 * Revision 1.3 1999/01/28 18:35:44 warmerda 00053 * minor windows cleanup. 00054 * 00055 * Revision 1.2 1999/01/27 20:16:03 warmerda 00056 * Added windows implementation. 00057 * 00058 * Revision 1.1 1999/01/11 15:34:57 warmerda 00059 * New 00060 * 00061 */ 00062 00063 #include "cpl_conv.h" 00064 00065 /* ==================================================================== */ 00066 /* Unix Implementation */ 00067 /* ==================================================================== */ 00068 #if defined(HAVE_DLFCN_H) 00069 00070 #define GOT_GETSYMBOL 00071 00072 #include <dlfcn.h> 00073 00074 /************************************************************************/ 00075 /* CPLGetSymbol() */ 00076 /************************************************************************/ 00077 00111 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00112 00113 { 00114 void *pLibrary; 00115 void *pSymbol; 00116 00117 pLibrary = dlopen(pszLibrary, RTLD_LAZY); 00118 if( pLibrary == NULL ) 00119 { 00120 CPLError( CE_Failure, CPLE_AppDefined, 00121 "%s", dlerror() ); 00122 return NULL; 00123 } 00124 00125 pSymbol = dlsym( pLibrary, pszSymbolName ); 00126 00127 if( pSymbol == NULL ) 00128 { 00129 CPLError( CE_Failure, CPLE_AppDefined, 00130 "%s", dlerror() ); 00131 return NULL; 00132 } 00133 00134 return( pSymbol ); 00135 } 00136 00137 #endif /* def __unix__ && defined(HAVE_DLFCN_H) */ 00138 00139 /* ==================================================================== */ 00140 /* Windows Implementation */ 00141 /* ==================================================================== */ 00142 #ifdef WIN32 00143 00144 #define GOT_GETSYMBOL 00145 00146 #include <windows.h> 00147 00148 /************************************************************************/ 00149 /* CPLGetSymbol() */ 00150 /************************************************************************/ 00151 00152 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00153 00154 { 00155 void *pLibrary; 00156 void *pSymbol; 00157 00158 pLibrary = LoadLibrary(pszLibrary); 00159 if( pLibrary == NULL ) 00160 { 00161 CPLError( CE_Failure, CPLE_AppDefined, 00162 "Can't load requested DLL: %s", pszLibrary ); 00163 return NULL; 00164 } 00165 00166 pSymbol = GetProcAddress( (HINSTANCE) pLibrary, pszSymbolName ); 00167 00168 if( pSymbol == NULL ) 00169 { 00170 CPLError( CE_Failure, CPLE_AppDefined, 00171 "Can't find requested entry point: %s\n", pszSymbolName ); 00172 return NULL; 00173 } 00174 00175 return( pSymbol ); 00176 } 00177 00178 #endif /* def _WIN32 */ 00179 00180 /* ==================================================================== */ 00181 /* Dummy implementation. */ 00182 /* ==================================================================== */ 00183 00184 #ifndef GOT_GETSYMBOL 00185 00186 /************************************************************************/ 00187 /* CPLGetSymbol() */ 00188 /* */ 00189 /* Dummy implementation. */ 00190 /************************************************************************/ 00191 00192 void *CPLGetSymbol(const char *, const char *) 00193 00194 { 00195 return NULL; 00196 } 00197 #endif