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

dll export function pointer (best way to provide exported function hooking)

本文关键字:函数 最佳 方式 指针 dll      更新时间:2023-10-16

我正在尝试为要调用的函数导出函数指针。我所追求的是,当dll/exe中的函数需要调用另一个库导出的函数时,它会获得函数指针并调用它。原因是我想提供一种挂钩机制,我认为函数指针是最快、最简单的方法,因为我可以在运行时轻松地更改它们指向的内容。所以我发现了这个从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;
}

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

要返回函数,您需要获取它的地址,并返回

例如

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

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

stackoverflow:通过函数边界传递对STL的引用

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

同样对于visualstudio,运行时实例之间也有单独的堆(new/delete)。当您链接到动态库时,进程中的所有DLL和exe都共享此DLL。但它们都需要一起更新。

因此,这个过程可以工作,但要警惕:-

  1. 在二进制文件(DLL/EXE)之间共享C++类型-无ABI
  2. 在DLL中使用new,在EXE中使用delete。(不同的堆)

STL对象也是有问题的,因为它们是头实现(编译成二进制)和DLL实现(编译到C++运行时)的混合体。

相关文章: