PageRenderTime 193ms CodeModel.GetById 102ms app.highlight 6ms RepoModel.GetById 83ms app.codeStats 1ms

/src/ftk_dlfcn.c

http://ftk.googlecode.com/
C | 138 lines | 82 code | 27 blank | 29 comment | 16 complexity | dd051f53d98a8c780087b0db725f94d1 MD5 | raw file
  1/*
  2 * File: ftk_dlfcn.h
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:  wrap dlfcn
  5 *
  6 * Copyright (c) 2011  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2011-05-01 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_dlfcn.h"
 34
 35#if defined(FTK_HAS_DLOPEN)
 36#include <dlfcn.h>
 37void* ftk_dlopen(const char* filename)
 38{
 39	void* handle = NULL;
 40	return_val_if_fail(filename != NULL, NULL);
 41
 42	if((handle = dlopen(filename, RTLD_NOW)) == NULL)
 43	{
 44		ftk_logd("dlopen %s failed(%s)\n", filename, dlerror());
 45	}
 46
 47	return handle;
 48}
 49
 50void* ftk_dlsym(void* handle, const char* func)
 51{
 52	void* fhandle = NULL;
 53	return_val_if_fail(handle != NULL && func != NULL, NULL);
 54
 55	if((fhandle = dlsym(handle, func)) == NULL)
 56	{
 57		ftk_logd("dlsym %s failed(%s)\n", func, dlerror());
 58	}
 59
 60	return fhandle;
 61}
 62
 63void  ftk_dlclose(void* handle)
 64{
 65	if(handle != NULL)
 66	{
 67		dlclose(handle);
 68	}
 69
 70	return;
 71}
 72
 73char* ftk_dl_file_name(const char* name, char filename[FTK_MAX_PATH+1])
 74{
 75	ftk_snprintf(filename, FTK_MAX_PATH, "lib%s.so", name);
 76
 77	return filename;
 78}
 79
 80#elif defined(FTK_HAS_LOAD_LIBRARY)
 81void* ftk_dlopen(const char* filename)
 82{
 83	void* handle = NULL;
 84	return_val_if_fail(filename != NULL, NULL);
 85
 86	handle = LoadLibrary(filename);
 87
 88	return handle;
 89}
 90
 91void* ftk_dlsym(void* handle, const char* func)
 92{
 93	void* fhandle = NULL;
 94	return_val_if_fail(handle != NULL && func != NULL, NULL);
 95	
 96	fhandle = GetProcAddress((HMODULE)handle, func);
 97
 98	return fhandle;
 99}
100
101void  ftk_dlclose(void* handle)
102{
103	if(handle != NULL)
104	{
105		FreeLibrary((HMODULE)handle);
106	}
107
108	return;
109}
110
111char* ftk_dl_file_name(const char* name, char filename[FTK_MAX_PATH+1])
112{
113	ftk_snprintf(filename, FTK_MAX_PATH, "%s.dll", name);
114
115	return filename;
116}
117
118#else
119void* ftk_dlopen(const char* filename)
120{
121	return NULL;
122}
123
124void* ftk_dlsym(void* handle, const char* func)
125{
126	return NULL;
127}
128
129void  ftk_dlclose(void* handle)
130{
131}
132
133char* ftk_dl_file_name(const char* name, char filename[FTK_MAX_PATH+1])
134{
135	return NULL;
136}
137
138#endif