列出 DLL 的导出函数

2021-12-25 00:00:00 windows dll export c++ dbghelp

我正在寻找一种方法(在 C++/Windows 中)使用 dbgHelp 列出 DLL 的导出函数(甚至可能是未导出的方法).
有人知道哪种方法可以做到吗?

I'm looking for a way (in C++/Windows) to list the exported functions of a DLL (and maybe even methods which are not exported) using dbgHelp.
Does anybody know which method can do it?

推荐答案

有代码此处 执行此操作.我已经清理了一下,它在下面显示的场景中工作,从 Kernel32.Dll 检索函数名称.

There is code here to do this. I have cleaned it up a bit and it worked in the scenario shown below, retrieving function names from Kernel32.Dll.

#include "imagehlp.h"

void ListDLLFunctions(string sADllName, vector<string>& slListOfDllFunctions)
{
    DWORD *dNameRVAs(0);
    _IMAGE_EXPORT_DIRECTORY *ImageExportDirectory;
    unsigned long cDirSize;
    _LOADED_IMAGE LoadedImage;
    string sName;
    slListOfDllFunctions.clear();
    if (MapAndLoad(sADllName.c_str(), NULL, &LoadedImage, TRUE, TRUE))
    {
        ImageExportDirectory = (_IMAGE_EXPORT_DIRECTORY*)
            ImageDirectoryEntryToData(LoadedImage.MappedAddress,
            false, IMAGE_DIRECTORY_ENTRY_EXPORT, &cDirSize);
        if (ImageExportDirectory != NULL)
        {
            dNameRVAs = (DWORD *)ImageRvaToVa(LoadedImage.FileHeader, 
                LoadedImage.MappedAddress,
            ImageExportDirectory->AddressOfNames, NULL);
            for(size_t i = 0; i < ImageExportDirectory->NumberOfNames; i++)
            {
                sName = (char *)ImageRvaToVa(LoadedImage.FileHeader, 
                        LoadedImage.MappedAddress,
                       dNameRVAs[i], NULL);
             slListOfDllFunctions.push_back(sName);
            }
        }
        UnMapAndLoad(&LoadedImage);
    }
}

int main(int argc, char* argv[])
{
    vector<string> names;
    ListDLLFunctions("KERNEL32.DLL", names);

    return 0;   
}

相关文章