最后更新于 .

这几天在写一个server,由于框架相同,仅仅是获取数据源的地方有区别,所以,研究了一下如何使用面向对象的方法来动态加载so。

主要思想就是:
1.通过一个函数能够获得一个基类的指针,这样在调用基类的函数的时候,就能自动调用子类的实现了。
2.存储so对象的指针应该是外层类的一个static变量。

详细还是看代码吧:
1)首先定义一个公共的头文件,里面存储的基类的定义:(需要注意的就是,只要不是纯虚函数,那么就一定要有实现;还有就是析构函数需要为虚函数)
so和主调程序都需要包含这个头文件。
source_base.h

#ifndef _SOURCE_BASE_H_
#define _SOURCE_BASE_H_
#include <iostream>
using namespace std;
class CSourceBase;
/** 
 * @brief    获取实例
 * 
 * @param    client        new出的指针
 * 
 * @return    0            succ
 *             else        fail
 */
extern “C” int CreatObj(CSourceBase *& client);
class CSourceBase
{
    public:
        CSourceBase(){};
        virtual ~CSourceBase(){};
    public:
        virtual int GetFriends(int iUin,char *data,int &iLen,int maxLen)
        {
            return 0;
        }
};
#endif

2)我们来看so的实现
xy_source.h

#ifndef _XY_SOURCE_H_
#define _XY_SOURCE_H_
#include <iostream>
#include “sourcebase.h”
using namespace std;
class CXY_Source:public CSourceBase
{
    public:
        CXY_Source();
        virtual ~CXY_Source();
        {}
        int GetFriends(int iUin,char *data,int &iLen,int maxLen);
};
#endif

xy_source.cpp

#include “xy_source.h”
int CreatObj(CSourceBase *& client)
{
    client = new CXY_Source();
    return 0;
}
CXY_Source::CXY_Source() { }
CXY_Source::~CXY_Source() { }
int CXY_Source::GetFriends(int iUin,char *data,int &iLen,int maxLen)
{
    return 0;
}

3)调用者的实现
这里说明一下,因为这里想要对外封装成透明的,所以,采用了如下的方式。
xy_client.h

#ifndef _XY_CLIENT_H_
#define _XY_CLIENT_H_
#include <iostream>
#include “sourcebase.h”
using namespace std;
typedef int (*FunPtr)(CSourceBase *& client);
class CXY_Client
{
    public:
        static void *SoObj;
    public:
        CXY_Client();
        virtual ~CXY_Client();
        //载入so
        int Init(const char * soname);
        int GetFriends(int iUin,char *data,int &iLen,int maxLen);
    private:
        CSourceBase *m_Client;
};

xy_client.cpp

#include “xy_client.h”
void* CXY_Client::SoObj=NULL;
CXY_Client::CXY_Client()
{
    m_Client = NULL;
}
CXY_Client::~CXY_Client()
{
    if(m_Client)
    {
        delete m_Client;
    }
}
int CXY_Client::Init(const char * soname)
{
    string strSoName;
    if(soname==NULL)
    {
        strSoName = “../lib/xysource.so”;
    }
    else
    {
        strSoName = soname;
    }
    if(strSoName.size()==0)
    {
        strSoName = “../lib/xysource.so”;
    }
    if(CXY_Client::SoObj == NULL)
    {
        SoObj=dlopen((char*)strSoName.c_str(),RTLD_LAZY);
        if(SoObj==NULL)
        {
            return -1;
        }
    }
    if(m_Client==NULL)
    {
        FunPtr func;
        func = (FunPtr)dlsym(SoObj, “CreatObj”);
        int ret = (*func)(m_Client);
        if(ret)
        {
            return -2;
        }
    }
    return 0;
}
int CXY_Client::GetFriends(int iUin,char *data,int &iLen,int maxLen)
{
    return m_Client->GetFriends(iUin,data,iLen,maxLen);
}

OK,到此为止代码就结束了,大家可能会发现我没有调用dlclose,这是因为static变量没有必要来调用,在进程结束时会自动释放句柄,当然如果需要有释放的应用场景的话,可以通过增加计数来实现。

另外由于上面的这个实例是从项目中抠出来的,所以并不能直接编译,还望大家见谅。
但是在这里可以下载到一个简单的可编译实例,可以用来作为实现so动态加载编程的第一步~~

代码下载

附:dlopen参数说明:

void *dlopen(const char *filename, int flag);
其中flag有:RTLD_LAZY RTLD_NOW RTLD_GLOBAL,其含义分别为:
RTLD_LAZY:在dlopen返回前,对于动态库中存在的未定义的变量(如外部变量extern,也可以是函数)不执行解析,就是不解析这个变量的地址。
RTLD_NOW:与上面不同,他需要在dlopen返回前,解析出每个未定义变量的地址,如果解析不出来,在dlopen会返回NULL,错误为:
: undefined symbol: xxxx…….
RTLD_GLOBAL:它的含义是使得库中的解析的定义变量在随后的随后其它的链接库中变得可以使用。

 

Pingbacks

Pingbacks已打开。

Trackbacks

  1. camisetas de futbol baratas on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  2. Minecraft Apk Mod on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  3. KunciJawabanTts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  4. Kuncijawabantts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  5. KunciJawabantts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  6. KunciJawabanTts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  7. Kuncijawabantts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  8. Túi xách công sở nữ đựng laptop on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  9. Gems on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  10. Unlocked Download on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  11. Unlocked Download on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  12. Unlocked Download on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  13. Minecraft Apk Mod Unlimited Coins and Guns on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  14. KunciJawabanTts.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  15. Unlocked Download on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  16. blogforhamstertips.blogspot.com on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  17. slot gacor on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  18. Nha Thong Minh on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  19. judi slot on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  20. nha thong minh co chong Trom Khong on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  21. autoservice63.ru on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  22. Nha Thong Minh Chong Trom on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  23. telegraph on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  24. boobies on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  25. Azartplay casino для on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

  26. Slot machine spigot on #

    用C++面向对象的方式动态加载so - 熊猫的程序世界

引用地址

评论

  1. 먹튀검증업체

    먹튀검증업체 on #

    What you published was actually very reasonable.
    However, think about this, suppose you added a little content?
    I am not suggesting your information is not good., but what if you added a title that grabbed folk's attention? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is kinda vanilla.
    You could glance at Yahoo's front page and watch how
    they create post headlines to get people interested.
    You might add a related video or a related picture
    or two to grab readers interested about everything've written. Just
    my opinion, it might make your posts a little livelier.

    Reply

  2. 먹튀검증업체

    먹튀검증업체 on #

    What you published was actually very reasonable.
    However, think about this, suppose you added a little content?

    I am not suggesting your information is not good., but what if you added a title that grabbed folk's attention? I
    mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is kinda vanilla.
    You could glance at Yahoo's front page and watch how they create post headlines to get people interested.
    You might add a related video or a related picture or two to grab readers interested about everything've written.
    Just my opinion, it might make your posts a little livelier.

    Reply

  3. rajacuan live

    rajacuan live on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界
    <Loved it!

    Reply

  4. Universitas Alma Ata

    Universitas Alma Ata on #

    I think that everything typed made a lot of sense.
    But, think on this, suppose you were to create a killer headline?
    I am not saying your content isn't solid., but what if you
    added a headline to maybe get folk's attention? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is
    a little vanilla. You might glance at Yahoo's home page and see how they write
    news headlines to grab viewers to open the links. You might add a video or a related picture or two
    to grab people excited about everything've written. Just my opinion, it might make
    your website a little livelier.

    Reply

  5. Universitas Alma Ata

    Universitas Alma Ata on #

    I think that everything typed made a lot of sense. But, think
    on this, suppose you were to create a killer headline?
    I am not saying your content isn't solid., but what if you
    added a headline to maybe get folk's attention? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界
    is a little vanilla. You might glance at Yahoo's home page and see how they write news headlines to grab
    viewers to open the links. You might add a video or a related picture or two to grab people excited
    about everything've written. Just my opinion, it might make your website a little livelier.

    Reply

  6. booth container kekinian

    booth container kekinian on #

    Thanks for finally talking about >用C++面向对象的方式动态加载so - 熊猫的程序世界 <Loved it!

    Reply

  7. booth container kekinian

    booth container kekinian on #

    Thanks for finally talking about >用C++面向对象的方式动态加载so
    - 熊猫的程序世界 <Loved it!

    Reply

  8. 카지노검증

    카지노검증 on #

    I believe everything said was actually very reasonable. However, think about this, suppose you were to create a awesome post title?
    I mean, I don't wish to tell you how to run your blog, but what if you added a title that grabbed a person's attention? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little plain. You ought to look at Yahoo's home page and note how
    they create post titles to grab viewers to click. You might add
    a related video or a related picture or two to grab readers excited about what you've written. In my opinion, it would bring
    your blog a little livelier.

    Reply

  9. Antabuse

    Antabuse on #

    Thanks for finally talking about >用C++面向对象的方式动态加载so - 熊猫的程序世界
    <Liked it!

    Reply

  10. Antabuse

    Antabuse on #

    Thanks for finally talking about >用C++面向对象的方式动态加载so -
    熊猫的程序世界 <Liked it!

    Reply

  11. Ordu Escort

    Ordu Escort on #

    I tend nott to wrote a leave a response, however I browsed some responses here 用C++面向对象的方式动态加载so - 熊猫的程序世界.
    I do have 2 questions for you if you do not mind. Could it be only mme or do some oof
    the rmarks look like they are coming from brain dead folks?
    :-P And, if you are writing at additional social sites, I'd like to follow everything new yoou have
    to post. Would you make a list of every one of your social community
    sites like your Facebook page, twitter feed, or linkedin profile?

    Reply

  12. Ordu Escort

    Ordu Escort on #

    I tend noot tto write a lave a response, however I browsewd some responses here 用C++面向对象的方式动态加载so - 熊猫的程序世界.
    I do have 2 questions for you if you do noot mind. Could it be only me or
    do some of the remarks look like they are coming from braion dead folks?
    :-P And, if you are writing at additional social
    sites, I'dlike to follow everything new you have to post.
    Would you make a list of every one of your ssocial community sites like your Facebook page, twitter feed, or linkedin profile?

    Reply

  13. website

    website on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界
    <Liked it!

    Reply

  14. website

    website on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界 <Liked it!

    Reply

  15. login bonanza178

    login bonanza178 on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界 <Liked it!

    Reply

  16. trực tiếp bóng đá fpt play hôm nay

    trực tiếp bóng đá fpt play hôm nay on #

    I think that everything published made a bunch of sense.
    But, what about this? what if you were to write a killer headline?
    I ain't saying your content isn't good, however suppose you added a headline that
    makes people want more? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little vanilla.
    You should look at Yahoo's home page and note how they
    create news titles to grab people to open the links.
    You might add a video or a related picture or
    two to grab readers interested about everything've got to say.
    In my opinion, it would make your posts a little bit more interesting.

    Reply

  17. trực tiếp bóng đá fpt play hôm nay

    trực tiếp bóng đá fpt play hôm nay on #

    I think that everything published made a bunch of sense. But, what
    about this? what if you were to write a killer headline?
    I ain't saying your content isn't good, however suppose you added a headline that makes people
    want more? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little vanilla.
    You should look at Yahoo's home page and note how they create news titles to grab people
    to open the links. You might add a video or a related picture or
    two to grab readers interested about everything've got to say.

    In my opinion, it would make your posts a little bit more interesting.

    Reply

  18. essay help online

    essay help online on #

    I think what you posted made a bunch of sense. But, what about this?
    what if you added a little information? I am not saying your content is not solid, but suppose you
    added a headline that makes people desire more? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little vanilla.

    You might peek at Yahoo's front page and watch how they write post titles to get viewers to open the links.

    You might add a video or a picture or two to grab people interested about what you've written. Just my opinion, it would make your
    website a little livelier.

    Reply

  19. essay help online

    essay help online on #

    I think what you posted made a bunch of sense.
    But, what about this? what if you added a little
    information? I am not saying your content is not solid, but suppose you added a headline
    that makes people desire more? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界
    is a little vanilla. You might peek at Yahoo's front page and watch how they write post titles to get viewers to
    open the links. You might add a video or a picture or two to grab people interested about what you've written. Just my opinion, it would make your website a little
    livelier.

    Reply

  20. 파라오카지노

    파라오카지노 on #

    What you composed made a bunch of sense. But, consider this, what
    if you were to write a awesome title? I mean, I don't want to tell you how to run your blog, however what if
    you added a post title that makes people want more?
    I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little boring.
    You ought to look at Yahoo's front page and note how they create article headlines to get people to open the links.
    You might add a related video or a related pic or
    two to grab readers interested about everything've written. Just my opinion, it might bring your blog a little bit more interesting.

    Reply

  21. 파라오카지노

    파라오카지노 on #

    What you composed made a bunch of sense. But, consider this,
    what if you were to write a awesome title?
    I mean, I don't want to tell you how to run your blog, however what if you added a
    post title that makes people want more? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little boring.

    You ought to look at Yahoo's front page and note how they create
    article headlines to get people to open the links. You might add a related video or a related pic or
    two to grab readers interested about everything've written. Just my opinion, it might bring your blog a little bit more interesting.

    Reply

  22. SEO lists

    SEO lists on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界 <Liked it!

    Reply

  23. try this

    try this on #

    I think what you said made a bunch of sense. But, what about
    this? suppose you were to write a killer title?
    I am not saying your information isn't good., however suppose you
    added a post title to maybe grab a person's attention?
    I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little plain. You might peek at
    Yahoo's front page and see how they write article
    titles to get people interested. You might try adding a video
    or a picture or two to grab readers excited about
    everything've written. Just my opinion, it might make your posts a little bit more
    interesting.

    Reply

  24. try this

    try this on #

    I think what you said made a bunch of sense. But, what about this?

    suppose you were to write a killer title? I am not saying your information isn't good.,
    however suppose you added a post title to maybe grab a person's attention? I mean 用C++面向对象的方式动态加载so - 熊猫的程序世界 is a little plain. You
    might peek at Yahoo's front page and see how they write article titles to get
    people interested. You might try adding a video or a picture or two to grab readers excited about everything've written. Just my opinion, it might make your posts a
    little bit more interesting.

    Reply

  25. 시흥 선수나라

    시흥 선수나라 on #

    Thanks for finally writing about >用C++面向对象的方式动态加载so - 熊猫的程序世界 <Loved it!

    Reply

发表评论