Blender V2.61 - r43446

util_dynlib.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdlib.h>
00020 
00021 #include "util_dynlib.h"
00022 
00023 #ifdef _WIN32
00024 
00025 #include <Windows.h>
00026 
00027 CCL_NAMESPACE_BEGIN
00028 
00029 struct DynamicLibrary {
00030     HMODULE module;
00031 };
00032 
00033 DynamicLibrary *dynamic_library_open(const char *name)
00034 {
00035     HMODULE module = LoadLibrary(name);
00036 
00037     if(!module)
00038         return NULL;
00039 
00040     DynamicLibrary *lib = new DynamicLibrary();
00041     lib->module = module;
00042 
00043     return lib;
00044 }
00045 
00046 void *dynamic_library_find(DynamicLibrary *lib, const char *name)
00047 {
00048     return (void*)GetProcAddress(lib->module, name);
00049 }
00050 
00051 void dynamic_library_close(DynamicLibrary *lib)
00052 {
00053     FreeLibrary(lib->module);
00054     delete lib;
00055 }
00056 
00057 CCL_NAMESPACE_END
00058 
00059 #else
00060 
00061 #include <dlfcn.h>
00062 
00063 CCL_NAMESPACE_BEGIN
00064 
00065 struct DynamicLibrary {
00066     void *module;
00067 };
00068 
00069 DynamicLibrary *dynamic_library_open(const char *name)
00070 {
00071     void *module = dlopen(name, RTLD_NOW);
00072 
00073     if(!module)
00074         return NULL;
00075 
00076     DynamicLibrary *lib = new DynamicLibrary();
00077     lib->module = module;
00078 
00079     return lib;
00080 }
00081 
00082 void *dynamic_library_find(DynamicLibrary *lib, const char *name)
00083 {
00084     return dlsym(lib->module, name);
00085 }
00086 
00087 void dynamic_library_close(DynamicLibrary *lib)
00088 {
00089     dlclose(lib->module);
00090     delete lib;
00091 }
00092 
00093 CCL_NAMESPACE_END
00094 
00095 #endif
00096