00001 /****************************************************************************** 00002 * $Id: cplgetsymbol_cpp-source.html,v 1.5 2000/11/06 04:49:01 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.5 2000/11/06 04:49:01 warmerda 00030 * *** empty log message *** 00030 * 00031 * Revision 1.9 2000/09/25 19:59:03 warmerda 00032 * look for WIN32 not _WIN32 00033 * 00034 * Revision 1.8 1999/05/20 02:54:38 warmerda 00035 * Added API documentation 00036 * 00037 * Revision 1.7 1999/04/23 13:56:36 warmerda 00038 * added stub implementation. Don't check for __unix__ 00039 * 00040 * Revision 1.6 1999/04/21 20:06:05 warmerda 00041 * Removed incorrect comment. 00042 * 00043 * Revision 1.5 1999/03/02 21:20:00 warmerda 00044 * test for dlfcn.h, not -ldl 00045 * 00046 * Revision 1.4 1999/03/02 21:08:11 warmerda 00047 * autoconf switch 00048 * 00049 * Revision 1.3 1999/01/28 18:35:44 warmerda 00050 * minor windows cleanup. 00051 * 00052 * Revision 1.2 1999/01/27 20:16:03 warmerda 00053 * Added windows implementation. 00054 * 00055 * Revision 1.1 1999/01/11 15:34:57 warmerda 00056 * New 00057 * 00058 */ 00059 00060 #include "cpl_conv.h" 00061 00062 /* ==================================================================== */ 00063 /* Unix Implementation */ 00064 /* ==================================================================== */ 00065 #if defined(HAVE_DLFCN_H) 00066 00067 #define GOT_GETSYMBOL 00068 00069 #include <dlfcn.h> 00070 00071 /************************************************************************/ 00072 /* CPLGetSymbol() */ 00073 /************************************************************************/ 00074 00108 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00109 00110 { 00111 void *pLibrary; 00112 void *pSymbol; 00113 00114 pLibrary = dlopen(pszLibrary, RTLD_LAZY); 00115 if( pLibrary == NULL ) 00116 { 00117 CPLError( CE_Failure, CPLE_AppDefined, 00118 "%s", dlerror() ); 00119 return NULL; 00120 } 00121 00122 pSymbol = dlsym( pLibrary, pszSymbolName ); 00123 00124 if( pSymbol == NULL ) 00125 { 00126 CPLError( CE_Failure, CPLE_AppDefined, 00127 "%s", dlerror() ); 00128 return NULL; 00129 } 00130 00131 return( pSymbol ); 00132 } 00133 00134 #endif /* def __unix__ && defined(HAVE_DLFCN_H) */ 00135 00136 /* ==================================================================== */ 00137 /* Windows Implementation */ 00138 /* ==================================================================== */ 00139 #ifdef WIN32 00140 00141 #define GOT_GETSYMBOL 00142 00143 #include <windows.h> 00144 00145 /************************************************************************/ 00146 /* CPLGetSymbol() */ 00147 /************************************************************************/ 00148 00149 void *CPLGetSymbol( const char * pszLibrary, const char * pszSymbolName ) 00150 00151 { 00152 void *pLibrary; 00153 void *pSymbol; 00154 00155 pLibrary = LoadLibrary(pszLibrary); 00156 if( pLibrary == NULL ) 00157 { 00158 CPLError( CE_Failure, CPLE_AppDefined, 00159 "Can't load requested DLL: %s", pszLibrary ); 00160 return NULL; 00161 } 00162 00163 pSymbol = GetProcAddress( (HINSTANCE) pLibrary, pszSymbolName ); 00164 00165 if( pSymbol == NULL ) 00166 { 00167 CPLError( CE_Failure, CPLE_AppDefined, 00168 "Can't find requested entry point: %s\n", pszSymbolName ); 00169 return NULL; 00170 } 00171 00172 return( pSymbol ); 00173 } 00174 00175 #endif /* def _WIN32 */ 00176 00177 /* ==================================================================== */ 00178 /* Dummy implementation. */ 00179 /* ==================================================================== */ 00180 00181 #ifndef GOT_GETSYMBOL 00182 00183 /************************************************************************/ 00184 /* CPLGetSymbol() */ 00185 /* */ 00186 /* Dummy implementation. */ 00187 /************************************************************************/ 00188 00189 void *CPLGetSymbol(const char *, const char *) 00190 00191 { 00192 return NULL; 00193 } 00194 #endif