链接库dll-6-简单插件

来源:互联网 发布:mobi的域名含义 编辑:程序博客网 时间:2024/06/05 07:15


***************************************链接库代码:

ImgArithPlugBase.h

#pragma once#ifndef ImgArithPlugBase_LK#define ImgArithPlugBase_LK#include <string>typedef int ImgArithInputType;typedef int ImgArithOutputType;class ImgArithPlugBase{public:virtual std::string description() const = 0;virtual bool validate(const ImgArithInputType& input) const = 0;virtual bool process( const ImgArithInputType& input , ImgArithOutputType& output) = 0;virtual ~ImgArithPlugBase(){}};#endif


插件文件ImgArithPlugAdd.cpp

#include "ImgArithPlugBase.h"#include <laok.h>#include <boost/config.hpp> // for BOOST_SYMBOL_EXPORTclass ImgArithPlugAdd : public ImgArithPlugBase{public:virtual std::string description() const {return "output = input + 200";}virtual bool validate(const ImgArithInputType& input) const {return input > 0;}virtual bool process( const ImgArithInputType& input , ImgArithOutputType& output){output = input + 200;return true;}};extern "C" BOOST_SYMBOL_EXPORT ImgArithPlugAdd plugin;ImgArithPlugAdd plugin;

导出控制dllmain.cpp

#include <iostream>#include <boost/dll.hpp>#include <Windows.h>namespace dll = boost::dll;int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved){std::string location = dll::this_line_location().string();if (pvReserved)std::cout  << location << "********************************loaded implicit!\n" ;elsestd::cout  << location << "********************************loaded explicit!\n";switch(fdwReason) {case DLL_PROCESS_ATTACH:std::cout   << location << "********************************dll process attach\n";//系统在进程空间映射break;case DLL_PROCESS_DETACH:std::cout    << location << "********************************dll process detach\n" ;//系统在进程空间撤销break;case DLL_THREAD_ATTACH:std::cout    << location << "********************************dll thread attach\n" ;//主线程不会触发,进程创建线程时,通知每一个dllmain,然后执行线程函数break;case DLL_THREAD_DETACH:std::cout    << location << "********************************dll thread detach\n" ;//线程正常终止时候,触发break;}return TRUE ;}


***************************************测试代码:

ImgArithPlugAdd-test.cpp

#include <boost/dll.hpp>#include <laok.h>#include "ImgArithPlugBase.h"namespace dll = boost::dll;JOB_DEFINE(dll_app , ImgArithPlugAdd){boost::filesystem::path libpath = GetCurFilePath();libpath /= "ImgArithPlugAdd.dll";boost::shared_ptr<ImgArithPlugBase> plug = dll::import<ImgArithPlugBase>(libpath , "plugin");PS( plug->description() );ImgArithInputType input;ImgArithOutputType output;input = 10;PS(input);PS( plug->validate(input) );PS( plug->process(input , output) );PS(output);}

测试效果:

=====<dll_app_ImgArithPlugAdd>beginE:\ArgusTech\workspace-cpp\boost\dll\app\ImgArithPlugAdd.dll********************************loaded explicit!E:\ArgusTech\workspace-cpp\boost\dll\app\ImgArithPlugAdd.dll********************************dll process attach[plug->description()]:[output = input + 200][input]:[10][plug->validate(input)]:[true][plug->process(input , output)]:[true][output]:[210]E:\ArgusTech\workspace-cpp\boost\dll\app\ImgArithPlugAdd.dll********************************loaded explicit!E:\ArgusTech\workspace-cpp\boost\dll\app\ImgArithPlugAdd.dll********************************dll process detach=====<dll_app_ImgArithPlugAdd>end [State:OK] [Times:0.049s]








0 0
原创粉丝点击