root/tags/gdal_1_2_0/bridge/gbgetsymbol.cpp
| Revision 2459, 5.1 kB (checked in by warmerda, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /****************************************************************************** |
| 2 | * $Id$ |
| 3 | * |
| 4 | * Project: GDAL Bridge |
| 5 | * Purpose: Fetch a function pointer from a shared library / DLL. |
| 6 | * Author: Frank Warmerdam, warmerda@home.com |
| 7 | * |
| 8 | * Adapted from cplgetsymbol.cpp. |
| 9 | * |
| 10 | ****************************************************************************** |
| 11 | * Copyright (c) 1999, Frank Warmerdam |
| 12 | * |
| 13 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 14 | * copy of this software and associated documentation files (the "Software"), |
| 15 | * to deal in the Software without restriction, including without limitation |
| 16 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 17 | * and/or sell copies of the Software, and to permit persons to whom the |
| 18 | * Software is furnished to do so, subject to the following conditions: |
| 19 | * |
| 20 | * The above copyright notice and this permission notice shall be included |
| 21 | * in all copies or substantial portions of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 24 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 | * DEALINGS IN THE SOFTWARE. |
| 30 | ****************************************************************************** |
| 31 | * |
| 32 | * $Log$ |
| 33 | * Revision 1.3 2001/09/06 14:03:21 warmerda |
| 34 | * upgrade bridge error reporting |
| 35 | * |
| 36 | * Revision 1.2 2000/09/27 13:22:07 warmerda |
| 37 | * also allow for unix in check |
| 38 | * |
| 39 | * Revision 1.1 1999/04/21 23:01:31 warmerda |
| 40 | * New |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include "gdalbridge.h" |
| 46 | |
| 47 | /* ==================================================================== */ |
| 48 | /* Unix Implementation */ |
| 49 | /* ==================================================================== */ |
| 50 | #if defined(__unix__) || defined(unix) |
| 51 | |
| 52 | #include <dlfcn.h> |
| 53 | |
| 54 | /************************************************************************/ |
| 55 | /* GBGetSymbol() */ |
| 56 | /* */ |
| 57 | /* Note that this function doesn't: */ |
| 58 | /* o prevent the reference count on the library from going up */ |
| 59 | /* for every request, or given any opportunity to unload */ |
| 60 | /* the library. */ |
| 61 | /* o Attempt to look for the library in non-standard */ |
| 62 | /* locations. */ |
| 63 | /* o Attempt to try variations on the symbol name, like */ |
| 64 | /* pre-prending or post-pending an underscore. */ |
| 65 | /************************************************************************/ |
| 66 | |
| 67 | void *GBGetSymbol( const char * pszLibrary, const char * pszSymbolName ) |
| 68 | |
| 69 | { |
| 70 | void *pLibrary; |
| 71 | void *pSymbol; |
| 72 | |
| 73 | pLibrary = dlopen(pszLibrary, RTLD_LAZY); |
| 74 | if( pLibrary == NULL ) |
| 75 | { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | pSymbol = dlsym( pLibrary, pszSymbolName ); |
| 80 | |
| 81 | if( pSymbol == NULL ) |
| 82 | { |
| 83 | fprintf( stderr, "GBGetSymbol(): %s\n", dlerror() ); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | return( pSymbol ); |
| 88 | } |
| 89 | |
| 90 | #endif /* def __unix__ && defined(HAVE_DLFCN_H) */ |
| 91 | |
| 92 | /* ==================================================================== */ |
| 93 | /* Windows Implementation */ |
| 94 | /* ==================================================================== */ |
| 95 | #ifdef _WIN32 |
| 96 | |
| 97 | #include <windows.h> |
| 98 | |
| 99 | /************************************************************************/ |
| 100 | /* GBGetSymbol() */ |
| 101 | /* */ |
| 102 | /* Note that this function doesn't: */ |
| 103 | /* o prevent the reference count on the library from going up */ |
| 104 | /* for every request, or given any opportunity to unload */ |
| 105 | /* the library. */ |
| 106 | /* o Attempt to look for the library in non-standard */ |
| 107 | /* locations. */ |
| 108 | /* o Attempt to try variations on the symbol name, like */ |
| 109 | /* pre-prending or post-pending an underscore. */ |
| 110 | /************************************************************************/ |
| 111 | |
| 112 | void *GBGetSymbol( const char * pszLibrary, const char * pszSymbolName ) |
| 113 | |
| 114 | { |
| 115 | void *pLibrary; |
| 116 | void *pSymbol; |
| 117 | |
| 118 | pLibrary = LoadLibrary(pszLibrary); |
| 119 | if( pLibrary == NULL ) |
| 120 | { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | pSymbol = GetProcAddress( (HINSTANCE) pLibrary, pszSymbolName ); |
| 125 | |
| 126 | if( pSymbol == NULL ) |
| 127 | { |
| 128 | fprintf( stderr, |
| 129 | "GBGetSymbol(): Can't find requested entry point: %s\n", |
| 130 | pszSymbolName ); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | return( pSymbol ); |
| 135 | } |
| 136 | |
| 137 | #endif /* def _WIN32 */ |
| 138 |
Note: See TracBrowser for help on using the browser.
