C语言如何开发简单的插件
linux 通过dlopen来实现:
#include "polygon.h"
#include <stdlib.h>
#include <dlfcn.h> int main()
{
typedef CPolygon* create_t(); void * handle = dlopen("./triangle.so", RTLD_LAZY); if( !handle )
{
std::cerr << dlerror() << std::endl;
exit();
} create_t * create_triangle = (create_t *)dlsym(handle, "create"); CPolygon * pObj = create_triangle(); if( != pObj )
{
pObj->area();
} delete pObj; dlclose(handle);
return ;
}
生成动态库:g++ -fPIC -shared triangle.cpp -o triangle.so
生成test主函数:g++ -fPIC test.cpp -ldl -o test
具体demo:http://files.cnblogs.com/files/hero4china/plugins_1.zip
/***************************************************************************/
/* dynclass.h */
/* 使用的时候#include "dynclass.h",在类的声明后使用DYNCLASS(类名)注册*/
/***************************************************************************/ #ifndef __DYNAMIC_H__
#define __DYNAMIC_H__ #include <cstdio>
#include <string>
#include <typeinfo>
#include <cstring> #if !defined ( DYN_DECLARE )
#define DYN_DECLARE(class_name) DYN_CLASS::CFactory<class_name> class_name
#endif #if !defined ( DYN_CREATE )
#define DYN_CREATE(class_name) DYN_CLASS::Create(class_name)
#endif namespace DYN_CLASS
{
/* create object by class name */
void * Create( const char * class_name ); /* interface of class factory*/
class CAbstractFactory
{
public:
virtual void * Create( const char * class_name ) = ;
}; /* list of class factory */
class CFactoryList
{
friend void * Create( const char * class_name );
private:
static CFactoryList * _head;
CFactoryList * m_next;
CAbstractFactory * m_item;
public:
CFactoryList( CAbstractFactory * fact );
virtual ~CFactoryList( void );
}; /* ctor of CFactoryList, add a class factory to list */
inline CFactoryList::CFactoryList( CAbstractFactory * fact )
: m_item( fact )
{
m_next = _head;
_head = this;
} #if defined ( _MSC_VER )
/* disable warning for the following line : CFactory( void ): m_item( this ) {} */
#pragma warning(disable : 4355)
#endif /* realization of class factory */
template <class t>
class CFactory: public CAbstractFactory
{
static t _object;
CFactoryList m_item; public: /* add itself to list of class factory when constructed */
CFactory( void ) : m_item( this ) {} virtual ~CFactory() {} /* create object of this class if matched */
void * Create( const char * class_name )
{
std::string strClassName; #if defined (_MSC_VER )
strClassName = ( "class " );
#else
char szSize[] = {};
sprintf(szSize, "%d", strlen(class_name) );
strClassName = szSize;
#endif
strClassName += class_name;
printf("%s vs %s \n", typeid(_object).name(), strClassName.c_str());
/* RTTI support */
return !strcmp( typeid(_object).name(), strClassName.c_str() )
? (void*)( new t ) : ; }
};
}
#endif /* __DYNAMIC_H__ */
/***************************************************************************/
/* dynclass.cpp */
/***************************************************************************/
#include "dynclass.h" namespace DYN_CLASS
{
CFactoryList * CFactoryList::_head = ; void *Create( const char * class_name )
{
void * new_object = ;
const CFactoryList * cur = CFactoryList::_head; for( ; cur ; cur = cur->m_next )
{
printf("find \n");
/* if class_name matched, object will then be created and returned */
if( new_object = cur->m_item->Create(class_name) )
{
break;
}
} return new_object;
} /* delete linkage from CFactoryList when some class factory destroyed */
CFactoryList::~CFactoryList( void )
{
CFactoryList ** m_nextp = &CFactoryList::_head; for( ; *m_nextp ; m_nextp = &(*m_nextp)->m_next )
if( *m_nextp == this )
{
*m_nextp = (*m_nextp)->m_next;
break;
}
}
}
参考:
- Why does C++ not have reflection?
- How can I add reflection to a C++ application?
- https://my.oschina.net/u/1450061/blog/204563
https://my.oschina.net/u/1450061/blog/204608
https://my.oschina.net/u/1450061/blog/204564
http://m.blog.csdn.net/article/details?id=9254239
http://blog.csdn.net/dengyunze/article/details/763558
http://www.ithao123.cn/content-9688046.html
C语言如何开发简单的插件的更多相关文章
- 用GO语言开发editplus编辑器插件(附源码)
我要开发的插件功能极为简单,就是对用户选中的内容进行base64编码或解密工作. 其中所涉及的技术部分主要是GO语言程序开发和editplus插件配置的部分,首先我们来看一下GO语言代码的写法,如下: ...
- [Songqw.Net 基础]WPF实现简单的插件化开发
原文:[Songqw.Net 基础]WPF实现简单的插件化开发 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/ar ...
- JVM 平台上的各种语言的开发指南
JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...
- Sublime Text 3前端开发常用优秀插件介绍
. 首页 博客园 联系我 前言:关于Sublime Text 3. Package Control插件管理. Package Control使用方法/安装Emmet插件. Emmet插件. JsFor ...
- 在NPAPI开发火狐浏览器插件在NPAPI插件
1.插件是什么 插件是一种遵循一定规范的应用程序接口编写出来的程序.插件必须依附于一个宿主程序,为宿主程序提供增强功能.插件的种类有很多,这里主要讨论浏览器插件. IE下利用OLE和COM技术开发的浏 ...
- VS简单注释插件——VS插件开发续
VS简单注释插件——VS插件开发续 前些时候,我写过一篇<VS版权信息插件——初试VS插件开发小记>分享过一个用于添加注释信息的插件,但那个插件有几个问题: 不能添加带块注释(/**/), ...
- 如何开发一个 PyCharm 插件
PyCharm 是很多 Python 开发者优先选择的 IDE,功能强大,跨平台,提供免费社区版,非常良心.如果你想自己给PyCharm添加一些功能怎么办呢?有两个办法: 通过提需求实现,到 JetB ...
- C语言可以开发哪些项目?
C语言是我们大多数人的编程入门语言,对其也再熟悉不过了,不过很多初学者在学习的过程中难免会出现迷茫,比如:不知道C语言可以开发哪些项目,可以应用在哪些实际的开发中--,这些迷茫也导致了我们在学习的过程 ...
- 如何开发一个Jquery插件
Jquery有两种开发插件的方法: 1.jquery.fn.extend(object); 2.jquery.extend(object); 第一种方法是给Jquery对象添加方法,jquery.fn ...
随机推荐
- 《图形学》实验六:中点Bresenham算法画圆
开发环境: VC++6.0,OpenGL 实验内容: 使用中点Bresenham算法画圆. 实验结果: 代码: #include <gl/glut.h> #define WIDTH 500 ...
- jquery插件开发继承了jQuery高级编程思路
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...
- php多文件上传
多文件上传<input type="file" name="file[]" multiple /> <?php function reArra ...
- 享元模式/Flyweight模式/对象结构型/设计模式
flyweight 享元模式(对象结构型) Flyweight在拳击比赛中指最轻量级,即"蝇量级"或"雨量级",这里选择使用"享元模式"的意 ...
- Python 元组
#不可变序列-----元组 tuple #元组和列表十分相似,元组和字符串一样都是不可变的. #元组由不同的元素组成,每个元素可以存储不同类型的数据,例如 #字符串.数字和元组 #元组通常代表一行数据 ...
- nfs的挂载方法
对于很多嵌入式驱动开发者,要进行很多次调试,如果nfs搭建不起来,那么对开发是很不方便的.经过三天,我终于把自己编的内核下载到开发板,并实现了nfs文件系统的挂载.今天把过程写下来. 思路 一 编译a ...
- 【centOS】账号管理
一.认识/etc/passwd和/etc/shadow 1.passwd的构造 上图为passwd其中一个用户的用户信息,分别表示为[用户名][密码][UID][GID][注释][家目录][Shell ...
- ASP.NET常见面试题及答案(130题)
1.C#中 property 与 attribute(抽像类)的区别,他们各有什么用处,这种机制的好处在哪里?答:property和attribute汉语都称之为属性.不过property是指类向外提 ...
- WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
- Python中输出格式化的字符串
在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下: >>> 'Hello, %s' % 'world' 'Hello, world' >>> ...