dll导出函数指针(提供导出函数挂钩的最佳方法)

时间:2015-10-27 19:09:51

标签: c++ pointers dll

我正在尝试导出要调用的函数的函数指针。我所追求的是当一个dll / exe中的函数需要调用另一个库导出的函数时,它获取函数指针并调用它。这样做的原因是我想提供一个钩子机制,我认为函数指针将是最快捷,最简单的方法,因为我可以改变它们指向的很容易的运行时。 所以我发现了这个Exporting a function pointer from dll,我无法让它发挥作用。每当我调用它来获取函数指针时,我都会收到一个错误,它无法找到入口点。所以错误不是函数指针正在工作,而是获取函数指针的函数不起作用。我相信这是一个功能签名问题。这是一个例子:

Colors.h

#ifndef __COLORS
#define __COLORS

#ifdef  MYDLL_EXPORTS 
/*Enabled as "export" while compiling the dll project*/
#define DLLEXPORT __declspec(dllexport)  
#else
/*Enabled as "import" in the Client side for using already created dll file*/
#define DLLEXPORT __declspec(dllimport)  
#endif

#include <string>
#include <vector>
class Colors
{
private:
     std::string myColor;
     static DLLEXPORT std::vector<std::string> allColors;
public:
     Colors(){};
     Colors(std::string MyColor);
     virtual DLLEXPORT std::string getMyColor();
     virtual DLLEXPORT void addToColors(std::string color);
     std::vector<std::string> getAllColors();
};
typedef Colors* (*create)(std::string);

DLLEXPORT create createColors();

Colors* createColors2(std::string color);



#endif

colors.cpp

#define MYDLL_EXPORTS
#include "Color.h"




std::vector<std::string> Colors::allColors;

Colors::Colors(std::string MyColor)
{
     this->myColor = MyColor;
     this->allColors.push_back(this->myColor);
}

std::vector<std::string> Colors::getAllColors()
{
     return this->allColors;
}

std::string Colors::getMyColor()
{
    return this->myColor;
}

Colors* createColors2(std::string color)
{
    return new Colors(color);

}

DLLEXPORT void Colors::addToColors(std::string color)
{
     this->allColors.push_back(color);
}

DLLEXPORT create createColors()
{
     return &createColors2;
}

的main.cpp

#define MYDLL_EXPORTS

#include <iostream>
#include <Windows.h>
#include "Color.h"



int main()
{


    Colors red("red");
    Colors blue("blue");

    Colors* dlltest;

    //Define the function prototype
    typedef Colors* (*createNewColor)();

    BOOL freeResult, runTimeLinkSuccess = FALSE;
    HINSTANCE dllHandle = NULL;
    createNewColor dllCreateNewColor = NULL;

    //Load the dll and keep the handle to it
    dllHandle = LoadLibrary(L"libs/testerdll.dll");

    // If the handle is valid, try to get the function address. 
    if (NULL != dllHandle)
    {

        //Get pointer to our function using GetProcAddress:
        dllCreateNewColor = (createNewColor)GetProcAddress(dllHandle,"createNewColor");

        // If the function address is valid, call the function. 
        if (runTimeLinkSuccess = (NULL != dllCreateNewColor))
        {

            dlltest = dllCreateNewColor();
            std::cout << "Color of dll class: " << dlltest->getMyColor() << std::endl;
        }
        else
        {
            std::cout << "Failed to locate function" << std::endl;
        }

            //Free the library:
            //freeResult = FreeLibrary(dllHandle);
    }
    else
    {
         std::cout << "Failed to load library" << std::endl;
    }

    std::vector<std::string> colorslist = red.getAllColors();
    for (std::string color : colorslist)
    {
        std::cout << color << std::endl;
    }
    return 0;
}

Dll项目 dllmain.cpp

// testerdll.cpp : Defines the exported functions for the DLL application.


#include "stdafx.h"


#include "Color.h"



__declspec(dllexport) Colors* createNewColor()
{
    create temp1 = createColors();  //seems to fail here

    return nullptr;
}

是的我知道我有内存泄漏等。这只是一个复制问题的快速示例代码。

1 个答案:

答案 0 :(得分:0)

要返回该功能,您需要获取它的地址,然后返回

e.g。

__declspec(dllexport) create createNewColor()
{
    create temp1 = createColors;

    return temp1;
}

但是,这个系统(使用std :: string作为返回类型,要求.exe和.dll都使用相同的基于DLL的运行时库。

stackoverflow : passing reference to STL over function boundary

C ++没有在文件之间定义调用约定。这意味着不同的编译器可能会略微不同地设置C ++对象。微软限制了COM的定义,但这仍然是可能的。

对于visual studio,运行时实例之间还有单独的堆(new / delete)。当您链接动态库时,进程中的所有dll和exes共享此DLL。但是他们都需要一起更新。

所以这个过程可以起作用,但要小心: -

  1. 在二进制文件(DLL / EXE)之间共享C ++类型 - 无ABI
  2. 在DLL中使用new,并在EXE中删除。 (不同的堆)。
  3. STL对象也存在问题,因为它们是头实现(编译成二进制文件)和DLL实现(编译到C ++运行时)的混合。