osDynLibrary.cpp revision 56513f5ff4f3e851c5f0cb38dc8851d18616b3c0
1/* 2* Copyright (C) 2011 The Android Open Source Project 3* 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16#include "osDynLibrary.h" 17 18#ifndef _WIN32 19#include <dlfcn.h> 20#endif 21#include <stdio.h> 22 23namespace osUtils { 24 25dynLibrary *dynLibrary::open(const char *p_libName) 26{ 27 dynLibrary *lib = new dynLibrary(); 28 if (!lib) { 29 return NULL; 30 } 31 32#ifdef _WIN32 33 lib->m_lib = LoadLibrary(p_libName); 34#else // !WIN32 35 lib->m_lib = dlopen(p_libName, RTLD_NOW); 36#endif 37 38 if (lib->m_lib == NULL) { 39 printf("Failed to load %s\n", p_libName); 40#ifndef _WIN32 41 printf("error %s\n", dlerror()); //only on linux 42#endif 43 delete lib; 44 return NULL; 45 } 46 47 return lib; 48} 49 50dynLibrary::dynLibrary() : 51 m_lib(NULL) 52{ 53} 54 55dynLibrary::~dynLibrary() 56{ 57 if (NULL != m_lib) { 58#ifdef _WIN32 59 FreeLibrary(m_lib); 60#else // !WIN32 61 dlclose(m_lib); 62#endif 63 } 64} 65 66dynFuncPtr dynLibrary::findSymbol(const char *p_symName) 67{ 68 if (NULL == m_lib) { 69 return NULL; 70 } 71 72#ifdef _WIN32 73 return (dynFuncPtr) GetProcAddress(m_lib, p_symName); 74#else // !WIN32 75 return (dynFuncPtr) dlsym(m_lib, p_symName); 76#endif 77} 78 79} // of namespace osUtils 80