Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

cplgetsymbol.cpp

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

doxygen1.2.2 Dimitri van Heesch, © 1997-2000