00001 /****************************************************************************** 00002 * $Id: cplgetsymbol_cpp-source.html,v 1.13 2002/12/21 19:13:12 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.13 2002/12/21 19:13:12 warmerda 00030 * updated 00030 * 00031 * Revision 1.12 2002/11/20 17:16:48 warmerda 00032 * Added debug report from dummy CPLGetSymbol(). 00033 * 00034 * Revision 1.11 2001/07/18 04:00:49 warmerda 00035 * added CPL_CVSID 00036 * 00037 * Revision 1.10 2001/01/19 21:16:41 warmerda 00038 * expanded tabs 00039 * 00040 * Revision 1.9 2000/09/25 19:59:03 warmerda 00041 * look for WIN32 not _WIN32 00042 * 00043 * Revision 1.8 1999/05/20 02:54:38 warmerda 00044 * Added API documentation 00045 * 00046 * Revision 1.7 1999/04/23 13:56:36 warmerda 00047 * added stub implementation. Don't check for __unix__ 00048 * 00049 * Revision 1.6 1999/04/21 20:06:05 warmerda 00050 * Removed incorrect comment. 00051 * 00052 * Revision 1.5 1999/03/02 21:20:00 warmerda 00053 * test for dlfcn.h, not -ldl 00054 * 00055 * Revision 1.4 1999/03/02 21:08:11 warmerda 00056 * autoconf switch 00057 * 00058 * Revision 1.3 1999/01/28 18:35:44 warmerda 00059 * minor windows cleanup. 00060 * 00061 * Revision 1.2 1999/01/27 20:16:03 warmerda 00062 * Added windows implementation. 00063 * 00064 * Revision 1.1 1999/01/11 15:34:57 warmerda 00065 * New 00066 * 00067 */ 00068 00069 #include "cpl_conv.h" 00070 00071 CPL_CVSID("$Id: cplgetsymbol_cpp-source.html,v 1.13 2002/12/21 19:13:12 warmerda Exp $"); 00072 00073 /* ==================================================================== */ 00074 /* Unix Implementation */ 00075 /* ==================================================================== */ 00076 #if defined(HAVE_DLFCN_H) 00077 00078 #define GOT_GETSYMBOL 00079 00080 #include <dlfcn.h> 00081 00082 /************************************************************************/ 00083 /* CPLGetSymbol() */ 00084 /************************************************************************/ 00085 00119 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00120 00121 { 00122 void *pLibrary; 00123 void *pSymbol; 00124 00125 pLibrary = dlopen(pszLibrary, RTLD_LAZY); 00126 if( pLibrary == NULL ) 00127 { 00128 CPLError( CE_Failure, CPLE_AppDefined, 00129 "%s", dlerror() ); 00130 return NULL; 00131 } 00132 00133 pSymbol = dlsym( pLibrary, pszSymbolName ); 00134 00135 if( pSymbol == NULL ) 00136 { 00137 CPLError( CE_Failure, CPLE_AppDefined, 00138 "%s", dlerror() ); 00139 return NULL; 00140 } 00141 00142 return( pSymbol ); 00143 } 00144 00145 #endif /* def __unix__ && defined(HAVE_DLFCN_H) */ 00146 00147 /* ==================================================================== */ 00148 /* Windows Implementation */ 00149 /* ==================================================================== */ 00150 #ifdef WIN32 00151 00152 #define GOT_GETSYMBOL 00153 00154 #include <windows.h> 00155 00156 /************************************************************************/ 00157 /* CPLGetSymbol() */ 00158 /************************************************************************/ 00159 00160 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00161 00162 { 00163 void *pLibrary; 00164 void *pSymbol; 00165 00166 pLibrary = LoadLibrary(pszLibrary); 00167 if( pLibrary == NULL ) 00168 { 00169 CPLError( CE_Failure, CPLE_AppDefined, 00170 "Can't load requested DLL: %s", pszLibrary ); 00171 return NULL; 00172 } 00173 00174 pSymbol = GetProcAddress( (HINSTANCE) pLibrary, pszSymbolName ); 00175 00176 if( pSymbol == NULL ) 00177 { 00178 CPLError( CE_Failure, CPLE_AppDefined, 00179 "Can't find requested entry point: %s\n", pszSymbolName ); 00180 return NULL; 00181 } 00182 00183 return( pSymbol ); 00184 } 00185 00186 #endif /* def _WIN32 */ 00187 00188 /* ==================================================================== */ 00189 /* Dummy implementation. */ 00190 /* ==================================================================== */ 00191 00192 #ifndef GOT_GETSYMBOL 00193 00194 /************************************************************************/ 00195 /* CPLGetSymbol() */ 00196 /* */ 00197 /* Dummy implementation. */ 00198 /************************************************************************/ 00199 00200 void *CPLGetSymbol(const char *pszLibrary, const char *pszEntryPoint) 00201 00202 { 00203 CPLDebug( "CPL", 00204 "CPLGetSymbol(%s,%s) called. Failed as this is stub" 00205 " implementation.", pszLibrary, pszEntryPoint ); 00206 return NULL; 00207 } 00208 #endif