从DLL加载函数到Boost函数
C++ Load function from DLL into Boost function
我想从DLL中加载一个特定的函数并将其存储在Boost函数中。这可能吗?
typedef void (*ProcFunc) (void);
typedef boost::function<void (void)> ProcFuncObj;
ACE_SHLIB_HANDLE file_handle = ACE_OS::dlopen("test.dll", 1);
ProcFunc func = (ProcFunc) ACE_OS::dlsym(file_handle, "func1");
ProcFuncObj fobj = func; //This compiles fine and executes fine
func(); //executes fine
fobj(); //but crashes when called
谢谢,Gokul .
您需要注意名称混淆和调用约定:
所以,在你的DLL中:// mydll.h
#pragma comment(linker, "/EXPORT:fnmydll=_fnmydll@4")
extern "C" int WINAPI fnmydll(int value);
// mydll.cpp
#include "mydll.h"
extern "C" int WINAPI fnmydll(int value)
{
return value;
}
然后,在您的DLL客户端应用程序中:
#include <windows.h>
#include <boost/function.hpp>
#include <iostream>
int main()
{
HMODULE dll = ::LoadLibrary(L"mydll.dll");
typedef int (WINAPI *fnmydll)(int);
// example using conventional function pointer
fnmydll f1 = (fnmydll)::GetProcAddress(dll, "fnmydll");
std::cout << "fnmydll says: " << f1(3) << std::endl;
// example using Boost.Function
boost::function<int (int)> f2 = (fnmydll)::GetProcAddress(dll, "fnmydll");
std::cout << "fnmydll says: " << f2(7) << std::endl;
return 0;
}
我确信这个例子构建和运行良好。
相关文章:
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- boost::bind()类似的东西,但用于函数调用
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- 传递 boost::p ython::numpy::ndarray 作为 boost::p ython 函数的(默认或非
- 如何正确取消析构函数中的 Boost deadline_timer(在多线程环境中)?
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- 如何通过 boost::p ython 重命名构造函数的关键字参数
- 无法将参数传递给 boost::thread 构造函数
- 如何将类成员函数与param绑定为boost::function的右值
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- 为什么Boost Variant使用模板构造函数而不是Boost::beast::websocket::stream的移
- boost::bind函数缓冲区的最大大小
- 如何正确访问结构值并将其传递给函数 - boost::p ython
- std::bind() 错误:无法确定重载函数"boost::asio::io_service::run"的哪个实例
- 为什么在C++函数 boost::algorithm::join_if 中抛出 std::bad_cast 异常
- 错误:使用已删除的函数boost::shared_mutex::shared_互斥
- 传递一个成员函数来创建一个自由函数boost指针