草庐IT

c++ - 以编程方式获取用于在非开发人员 Windows 环境中构建进程或库的 DLL 列表

coder 2024-06-20 原文

我什至不确定这是否适用于 Windows;我还没有看到一个人要求这么普遍的东西并找到解决方案。这可能是可能的,但可能没有用于处理它的 API。

我有一个自动化测试模块,我正在为 Windows 工作,它使用一个模块以通用方式处理检测到的 EXE,除非它检测到二进制文件来自特定的测试框架。到目前为止,我只能通过查询帮助和处理响应/字符串解析来做到这一点。如果我触发某人在框架之外编写的长时间测试,该测试接受命令行参数寻求帮助,但实际上并不处理这些命令行参数而只是自动运行,这可能会导致问题。因此,有时我会卡在等待测试完成的时候,而不是进行闪电般的快速查询。这就是我试图通过这个花哨的新模块避免的。 :)

这个问题的关键是,这个用于获取 DLL 列表的消耗模块将分发到非开发 Windows 系统,我不能说它是用什么构建的(.NET、C++ 等)。这使我无法使用 dumpbinlink作为微软 does not allow them to be distributed .出于我自己的许可要求,我的这个模块不会出售;永远的免费软件。

我被建议调查 dumpbin在我意识到我无法分发它之前。当我使用它时,这就是我得到的:

c:\test_dir>dumpbin /dependents .\qt_unit_test.exe

Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file .\qt_test_unit_test.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    Qt5Test.dll
    Qt5Core.dll
    KERNEL32.dll
    VCRUNTIME140.dll
    api-ms-win-crt-utility-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll

  Summary

        1000 .data
        1000 .gfids
        1000 .pdata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        1000 .text


唯一需要的信息是 DLL 依赖项列表。不需要同时使用动态和静态库。最少需要的是 DLL,但是如果有人我也可以使用静态库,那会很酷;完全可选。

我基本上这样做是为了验证它是否是 Qt 测试二进制文件:
c:\test_dir>dumpbin /dependents .\qt_unit_test.exe | call findstr /i qt5test 1>nul 2>nul
c:\test_dir>if "%errorlevel%"=="0" echo is Qt Test

在编码方面,到目前为止我尝试过的是 C# 的 Assembly.GetReferencedAssemblies ,但这当然只能获取程序集信息,因此常规的旧 STL C++ 应用程序会生成异常。

接下来我比较了 Ed Bayiates's answers 中的一个试图获得一些有意义的东西,但老实说,我是 WinAPI 领域的一条鱼。我可能误解了整个概念......无论如何,您可以检查当前代码here ,但我不明白如何从返回的 IntPtr 翻译到一个字符串或字符串列表,告诉您哪些 DLL 用于它。看起来它在其他方面工作正常,但是是的,经典 voodoo programming场景在这里...

顺便说一句,我研究了只使用 Visual C++ Build Tools standalone 的可能性。 ,但是在您获得所有工具后,这件事最终会结束,这违背了我的轻量级通用自动化测试模块的目的。

我愿意使用任何语言来实现这一点;我从 C++ 和 C# 开始,因为我认为拥有 WinAPI 访问权限可能会让我摆脱这种操作系统特定的困境。

更新:

6.4. The .idata Section All image files that import symbols, including virtually all executable (EXE) files, have an .idata section. A typical file layout for the import information follows...



在努力获取 .idata 的同时为了处理这个问题,我遇到了一个讨厌的小问题。 idata 的“几乎所有可执行文件...”子句部分并不像文档会让您相信的那样包罗万象。查看 Windows 10 内置的 Calculator.exe 应用程序:

C:\WINDOWS\system32>dumpbin /summary "C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1703.601.0_x64__8wekyb3d8bbwe\Calculator.exe"

Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1703.601.0_x64__8wekyb3d8bbwe\Calculator.exe

File Type: EXECUTABLE IMAGE

  Summary

       4C000 .data
        1000 .gfids
        1000 .giats
       21000 .pdata
      135000 .rdata
        A000 .reloc
        1000 .rsrc
      20D000 .text
        1000 .tls
        1000 minATL


.idata部分。但是,这并不能阻止 dumpbin 找到这些依赖项:

C:\WINDOWS\system32>dumpbin /dependents "C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1703.601.0_x64__8wekyb3d8bbwe\Calculator.exe"
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1703.601.0_x64__8wekyb3d8bbwe\Calculator.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    api-ms-win-core-localization-l1-2-1.dll
    api-ms-win-eventing-provider-l1-1-0.dll
    api-ms-win-core-com-l1-1-1.dll
    api-ms-win-core-sysinfo-l1-2-1.dll
    api-ms-win-core-processthreads-l1-1-2.dll
    api-ms-win-core-sysinfo-l1-2-3.dll
    vccorlib140_app.DLL
    MSVCP140_APP.dll
    CONCRT140_APP.dll
    VCRUNTIME140_APP.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-convert-l1-1-0.dll
    api-ms-win-crt-string-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-core-util-l1-1-0.dll
    api-ms-win-core-synch-l1-2-0.dll
    api-ms-win-core-winrt-error-l1-1-1.dll
    api-ms-win-core-winrt-string-l1-1-0.dll
    api-ms-win-core-handle-l1-1-0.dll
    api-ms-win-core-winrt-l1-1-0.dll
    api-ms-win-core-profile-l1-1-0.dll
    api-ms-win-core-libraryloader-l1-2-0.dll
    api-ms-win-core-interlocked-l1-2-0.dll

  Summary

       4C000 .data
        1000 .gfids
        1000 .giats
       21000 .pdata
      135000 .rdata
        A000 .reloc
        1000 .rsrc
      20D000 .text
        1000 .tls
        1000 minATL


更新#2:

在从我的一位导师那里得到一些很好的建议和意见后,我离完成这一目标越来越近了。当使用 Visual C++ 运行时 redists 时,我设法删除了所有需要使用调试库的 Windows 内部组件或 GAC 外部的库。

当前的问题是从获取导入表的 RVA 到获取导入表中的所有名称。即使其余数据不为空,我的第一个查询导入也给出了空名称。
//******************************************************************************
// HEADERS
#include "Windows.h"

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>

#include <experimental\filesystem>

//******************************************************************************
// NAMESPACES
namespace fs = std::experimental::filesystem;

//******************************************************************************
//FUNCTION DECLARATIONS
bool verify_image_file(std::string);
std::vector<char> read_all_bytes(const char* file);
std::vector<std::string> parse_file(std::string file);

//******************************************************************************
// CONSTANTS
//        LABEL                                                HEX            DEC
//
const WORD MAGIC_NUM_32BIT             = static_cast<const WORD>(0x10b);      // 267
const WORD MAGIC_NUM_64BIT             = static_cast<const WORD>(0x20b);      // 523
const int IMG_SIGNATURE_OFFSET         = static_cast<const int>(0x3c);        // 60
const int IMPORT_TABLE_OFFSET_32       = static_cast<const int>(0x68);        // 104
const int IMPORT_TABLE_OFFSET_64       = static_cast<const int>(0x78);        // 120
const int IMG_SIGNATURE_SIZE           = static_cast<const int>(0x4);         // 4
const int OPT_HEADER_OFFSET_32         = static_cast<const int>(0x1c);        // 28
const int OPT_HEADER_OFFSET_64         = static_cast<const int>(0x18);        // 24
const int DATA_DIR_OFFSET_32           = static_cast<const int>(0x60);        // 96
const int DATA_DIR_OFFSET_64           = static_cast<const int>(0x70);        // 112
const int DATA_IAT_OFFSET_64           = static_cast<const int>(0xD0);        // 208
const int DATA_IAT_OFFSET_32           = static_cast<const int>(0xC0);        // 192
const int SZ_OPT_HEADER_OFFSET         = static_cast<const int>(0x10);        // 16
const int RVA_AMOUNT_OFFSET_64         = static_cast<const int>(0x6c);        // 108
const int RVA_AMOUNT_OFFSET_32         = static_cast<const int>(0x5c);        // 92
const char * KNOWN_IMG_SIGNATURE       = static_cast<const char*>("PE\0\0");

//******************************************************************************
// Globals
bool is64Bit = false;
bool is32Bit = false;

//******************************************************************************
// PLACEHOLDERS
const char* ph_file("C:\\Windows\\System32\\notepad.exe");

//******************************************************************************
// ENTRY
int main(int argc, char* argv[])
{
    if (!verify_image_file(ph_file))return -1;

    if (parse_file(ph_file).size() > 1)return 0;
    else return -1;

    return -1;
}

//******************************************************************************
// FILE PARSER
std::vector<std::string> parse_file(std::string file)
{
    std::vector<char> bytes = read_all_bytes(file.c_str());
    std::vector<std::string> dependencies;

    DWORD * signature_offset_location = (DWORD*)&bytes[IMG_SIGNATURE_OFFSET];
    char * signature = (char*)&bytes[*signature_offset_location];

    if (*signature != *KNOWN_IMG_SIGNATURE)return dependencies;

    DWORD coff_file_header_offset = *signature_offset_location + IMG_SIGNATURE_SIZE;
    IMAGE_FILE_HEADER* coff_file_header = (IMAGE_FILE_HEADER*)&bytes[coff_file_header_offset];
    DWORD optional_file_header_offset = coff_file_header_offset + sizeof(IMAGE_FILE_HEADER);

    WORD size_of_optional_header_offset = coff_file_header_offset + SZ_OPT_HEADER_OFFSET;
    WORD* size_of_optional_header = (WORD*)&bytes[size_of_optional_header_offset];  

    //Magic is a 2-Byte value at offset-zero of the optional file header regardless of 32/64 bit
    WORD* magic_number = (WORD*)&bytes[optional_file_header_offset];

    if (*magic_number == MAGIC_NUM_32BIT)is32Bit = true;
    else if (*magic_number == MAGIC_NUM_64BIT)is64Bit = true;
    else
    {
        std::cerr << "Could not parse magic number for 32 or 64-bit PE-format Image File." << std::endl;
        return dependencies;
    }

    if (is64Bit)
    {
        IMAGE_OPTIONAL_HEADER64 * img_opt_header_64 = (IMAGE_OPTIONAL_HEADER64*)&bytes[optional_file_header_offset];
        IMAGE_DATA_DIRECTORY* import_table_data_dir = (IMAGE_DATA_DIRECTORY*)&bytes[optional_file_header_offset + IMPORT_TABLE_OFFSET_64];
        DWORD* import_table_address = (DWORD*)import_table_data_dir;



        // To Get the import table, you need to check all the IMAGE_SECTION_HEADERs for the section that matches size of the direct-query.
        // TO get those you can use normal offsets. To go further, we need to start using the RVA
        // IMAGE_SECTION_HEADERS starts directly after the end of the optional file header for file_header->NumberOfSections
        // Then, your RVA is if (ptr_to_raw_data >= va && ptr_to_raw_data < va + SizeOfData){//isSection}
        // DWORD FileOffset = Ptr_To_Raw_Data - VA + PointerToRawData


        DWORD image_section_header_offset = optional_file_header_offset;

        for (int i = 0; i < coff_file_header->NumberOfSections; i++)
        {
            IMAGE_SECTION_HEADER* queried_section_header = (IMAGE_SECTION_HEADER*)&bytes[image_section_header_offset];
            if  (queried_section_header->PointerToRawData >= import_table_data_dir->VirtualAddress && (queried_section_header->PointerToRawData < (import_table_data_dir->VirtualAddress + queried_section_header->SizeOfRawData)))
            {
                DWORD import_table_offset = queried_section_header->PointerToRawData - import_table_data_dir->VirtualAddress + queried_section_header->PointerToRawData;
                IMAGE_IMPORT_DESCRIPTOR* import_table_descriptor = (IMAGE_IMPORT_DESCRIPTOR*)&bytes[import_table_offset];
                if (import_table_descriptor->Name==NULL && 
                    import_table_descriptor->Characteristics==NULL &&
                    import_table_descriptor->FirstThunk==NULL &&
                    import_table_descriptor->ForwarderChain==NULL &&
                    import_table_descriptor->OriginalFirstThunk==NULL &&
                    import_table_descriptor->TimeDateStamp==NULL)
                {
                    break;//Signifies end of IMAGE_IMPORT_DESCRIPTORs
                }

                DWORD* dependency_name_address = (DWORD*)import_table_descriptor->Name;
                char * dependency_name = (char *)&bytes[import_table_descriptor->Name];
                dependencies.push_back((std::string)dependency_name);
                int breakpoint = 0;
            }
            image_section_header_offset = image_section_header_offset + sizeof(IMAGE_SECTION_HEADER);
        }
    }
    else//32-bit behavior
    {
        //todo
    }

    return dependencies;
}

//******************************************************************************
// FILE READER
std::vector<char> read_all_bytes(const char* filename)
{
    std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
    std::ifstream::pos_type pos = ifs.tellg();

    std::vector<char> result(pos);
    ifs.seekg(0, std::ios::beg);
    ifs.read(&result[0], pos);

    return result;
}

//******************************************************************************
// IMAGE-TYPE FILE VERIFIER
bool verify_image_file(std::string file_to_verify)
{
    if (fs::exists(file_to_verify))
    {
        size_t extension_query = file_to_verify.find(".dll", 0);
        if (extension_query == std::string::npos)
        {
            extension_query = file_to_verify.find(".DLL", 0);
            if (extension_query == std::string::npos)
            {
                extension_query = file_to_verify.find(".exe", 0);
                if (extension_query == std::string::npos)
                {
                    extension_query = file_to_verify.find(".EXE", 0);
                }
                else { return true; }

                if (extension_query != std::string::npos) { return true; }
            }
            else { return true; }
        }
        else { return true; }
    }
    return false;
}

最佳答案

未优化的 POC 代码,针对 x86 和 x64 进行测试

主要使用指针数学而不是访问器,很少使用 Windows.hwinnt.h -- 所有内容都应该包含在为 2017 安装的 Visual C++ Redists 中。如果您想要早期版本,则必须删除 Visual C++ 2017 功能,这些功能除了文件系统帮助程序之外没什么可说的。

//******************************************************************************
// Headers
#include "Windows.h"

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>

#include <experimental\filesystem>

//******************************************************************************
// Namespaces
namespace fs = std::experimental::filesystem;

//******************************************************************************
//FUNCTION DECLARATIONS
bool verify_image_file(std::string);
std::vector<char> read_all_bytes(const char* file);
std::vector<std::string> parse_pe_import_table_names(std::string file);

//******************************************************************************
// Constants
//        LABEL                                                HEX            DEC
//
const WORD MAGIC_NUM_32BIT          = static_cast<const WORD>(0x10b);     // 267
const WORD MAGIC_NUM_64BIT          = static_cast<const WORD>(0x20b);     // 523
const int IMG_SIGNATURE_OFFSET      = static_cast<const int>(0x3c);       // 60
const int IMPORT_TABLE_OFFSET_32    = static_cast<const int>(0x68);       // 104
const int IMPORT_TABLE_OFFSET_64    = static_cast<const int>(0x78);       // 120
const int IMG_SIGNATURE_SIZE        = static_cast<const int>(0x4);        // 4
const int OPT_HEADER_OFFSET_32      = static_cast<const int>(0x1c);       // 28
const int OPT_HEADER_OFFSET_64      = static_cast<const int>(0x18);       // 24
const int DATA_DIR_OFFSET_32        = static_cast<const int>(0x60);       // 96
const int DATA_DIR_OFFSET_64        = static_cast<const int>(0x70);       // 112
const int DATA_IAT_OFFSET_64        = static_cast<const int>(0xD0);       // 208
const int DATA_IAT_OFFSET_32        = static_cast<const int>(0xC0);       // 192
const int SZ_OPT_HEADER_OFFSET      = static_cast<const int>(0x10);       // 16
const int RVA_AMOUNT_OFFSET_64      = static_cast<const int>(0x6c);       // 108
const int RVA_AMOUNT_OFFSET_32      = static_cast<const int>(0x5c);       // 92
const char * KNOWN_IMG_SIGNATURE    = static_cast<const char*>("PE\0\0");

//******************************************************************************
// Globals
bool is64Bit = false;
bool is32Bit = false;

//******************************************************************************
// Exceptions
class invalid_parameters        : public std::exception { const char* what() const throw()
{ return "You did not provide the solitary command-line parameter of the EXE or DLL to check.\n"; } };

class invalid_image_file        : public std::exception { const char* what() const throw()
{ return "The file detected was not determined to be an image file based off its extension.\n"; } };

class unexpected_rva_offset     : public std::exception { const char* what() const throw()
{ return "An unexpected value was returned for the RVA to File Offset.\n"; } };

class non_image_magic_number    : public std::exception { const char* what() const throw()
{ return "The PE Optional Header's Magic Number did not indicate the file was an image.\n"; } };

class invalid_pe_signature      : public std::exception { const char* what() const throw()
{ return "The PE Signature was not detected.\n"; } };

//******************************************************************************
// Entry
int main(int argc, char* argv[])
{
    if (argc != 2)throw invalid_parameters();

    const char* param_one = (const char*)argv[1];



    if (!verify_image_file(argv[1])) { throw invalid_image_file(); }

    std::vector<std::string> static_import_dependencies = parse_pe_import_table_names(argv[1]);
    if (static_import_dependencies.size() > 1)
    {
        for (int i = 0; i < (static_import_dependencies.size()-1);i++)
        {
            std::cout << static_import_dependencies[i] << std::endl;
        }
        return 0;
    }
    else return -1;

    return -1;
}

//******************************************************************************
// PE Parser
std::vector<std::string> parse_pe_import_table_names(std::string file)
{
    std::vector<char> bytes = read_all_bytes(file.c_str());
    std::vector<std::string> dependencies;

    DWORD * signature_offset_location = (DWORD*)&bytes[IMG_SIGNATURE_OFFSET];
    char * signature = (char*)&bytes[*signature_offset_location];

    if (*signature != *KNOWN_IMG_SIGNATURE)return dependencies;

    DWORD coff_file_header_offset = *signature_offset_location + IMG_SIGNATURE_SIZE;
    IMAGE_FILE_HEADER* coff_file_header = (IMAGE_FILE_HEADER*)&bytes[coff_file_header_offset];
    DWORD optional_file_header_offset = coff_file_header_offset + sizeof(IMAGE_FILE_HEADER);

    WORD size_of_optional_header_offset = coff_file_header_offset + SZ_OPT_HEADER_OFFSET;
    WORD* size_of_optional_header = (WORD*)&bytes[size_of_optional_header_offset];

    //Magic is a 2-Byte value at offset-zero of the optional file header regardless of 32/64 bit
    WORD* magic_number = (WORD*)&bytes[optional_file_header_offset];

    if (*magic_number == MAGIC_NUM_32BIT)is32Bit = true;
    else if (*magic_number == MAGIC_NUM_64BIT)is64Bit = true;
    else
    {
        std::cerr << "Could not parse magic number for 32 or 64-bit PE-format Image File." << std::endl;
        return dependencies;
    }

    if (is64Bit)
    {
        IMAGE_OPTIONAL_HEADER64 * img_opt_header_64 = (IMAGE_OPTIONAL_HEADER64*)&bytes[optional_file_header_offset];
        IMAGE_DATA_DIRECTORY* import_table_data_dir = (IMAGE_DATA_DIRECTORY*)&bytes[optional_file_header_offset + IMPORT_TABLE_OFFSET_64];
        DWORD* import_table_address = (DWORD*)import_table_data_dir;

        DWORD image_section_header_offset = optional_file_header_offset + coff_file_header->SizeOfOptionalHeader;

        for (int i = 0; i < coff_file_header->NumberOfSections; i++)
        {
            IMAGE_SECTION_HEADER* queried_section_header = (IMAGE_SECTION_HEADER*)&bytes[image_section_header_offset];
            if (*import_table_address >= queried_section_header->VirtualAddress &&
                (*import_table_address < (queried_section_header->VirtualAddress + queried_section_header->SizeOfRawData)))
            {
                DWORD import_table_offset = *import_table_address - queried_section_header->VirtualAddress + queried_section_header->PointerToRawData;
                while(true)
                {
                    IMAGE_IMPORT_DESCRIPTOR* import_table_descriptor = (IMAGE_IMPORT_DESCRIPTOR*)&bytes[import_table_offset];
                    if (import_table_descriptor->OriginalFirstThunk == 0)
                    {
                        break;//Signifies end of IMAGE_IMPORT_DESCRIPTORs
                    }
                    // (VA from data directory _entry_ to Image Import Descriptor's element you want) - VA from section header + section header's PointerToRawData
                    DWORD dependency_name_address = import_table_descriptor->Name;//VA not RVA; ABSOLUTE
                    DWORD name_offset = dependency_name_address - queried_section_header->VirtualAddress + queried_section_header->PointerToRawData;
                    char * dependency_name = (char *)&bytes[name_offset];
                    dependencies.push_back((std::string)dependency_name);
                    import_table_offset = import_table_offset + sizeof(IMAGE_IMPORT_DESCRIPTOR);
                }
            }
            image_section_header_offset = image_section_header_offset + sizeof(IMAGE_SECTION_HEADER);
        }
    }
    else//32-bit behavior
    {
        IMAGE_OPTIONAL_HEADER32 * img_opt_header_32 = (IMAGE_OPTIONAL_HEADER32*)&bytes[optional_file_header_offset];
        IMAGE_DATA_DIRECTORY* import_table_data_dir = (IMAGE_DATA_DIRECTORY*)&bytes[optional_file_header_offset + IMPORT_TABLE_OFFSET_32];
        DWORD* import_table_address = (DWORD*)import_table_data_dir;

        DWORD image_section_header_offset = optional_file_header_offset + coff_file_header->SizeOfOptionalHeader;

        for (int i = 0; i < coff_file_header->NumberOfSections; i++)
        {
            IMAGE_SECTION_HEADER* queried_section_header = (IMAGE_SECTION_HEADER*)&bytes[image_section_header_offset];
            if (*import_table_address >= queried_section_header->VirtualAddress &&
                (*import_table_address < (queried_section_header->VirtualAddress + queried_section_header->SizeOfRawData)))
            {
                DWORD import_table_offset = *import_table_address - queried_section_header->VirtualAddress + queried_section_header->PointerToRawData;
                while (true)
                {
                    IMAGE_IMPORT_DESCRIPTOR* import_table_descriptor = (IMAGE_IMPORT_DESCRIPTOR*)&bytes[import_table_offset];
                    if (import_table_descriptor->OriginalFirstThunk == 0)
                    {
                        break;//Signifies end of IMAGE_IMPORT_DESCRIPTORs
                    }
                    // (VA from data directory _entry_ to Image Import Descriptor's element you want) - VA from section header + section header's PointerToRawData
                    DWORD dependency_name_address = import_table_descriptor->Name;//VA not RVA; ABSOLUTE
                    DWORD name_offset = dependency_name_address - queried_section_header->VirtualAddress + queried_section_header->PointerToRawData;
                    char * dependency_name = (char *)&bytes[name_offset];
                    dependencies.push_back((std::string)dependency_name);
                    import_table_offset = import_table_offset + sizeof(IMAGE_IMPORT_DESCRIPTOR);
                }
            }
            image_section_header_offset = image_section_header_offset + sizeof(IMAGE_SECTION_HEADER);
        }
    }

    return dependencies;
}

//******************************************************************************
// File Reader
std::vector<char> read_all_bytes(const char* filename)
{
    std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
    std::ifstream::pos_type pos = ifs.tellg();

    std::vector<char> result(pos);
    ifs.seekg(0, std::ios::beg);
    ifs.read(&result[0], pos);

    return result;
}

//******************************************************************************
// IMAGE-TYPE FILE VERIFIER
bool verify_image_file(std::string file_to_verify)
{
    if (fs::exists(file_to_verify))
    {
        size_t extension_query = file_to_verify.find(".dll", 0);
        if (extension_query == std::string::npos)
        {
            extension_query = file_to_verify.find(".DLL", 0);
            if (extension_query == std::string::npos)
            {
                extension_query = file_to_verify.find(".exe", 0);
                if (extension_query == std::string::npos)
                {
                    extension_query = file_to_verify.find(".EXE", 0);
                }
                else { return true; }

                if (extension_query != std::string::npos) { return true; }
            }
            else { return true; }
        }
        else { return true; }
    }
    return false;
}

关于c++ - 以编程方式获取用于在非开发人员 Windows 环境中构建进程或库的 DLL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670731/

有关c++ - 以编程方式获取用于在非开发人员 Windows 环境中构建进程或库的 DLL 列表的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  6. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  7. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  8. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  9. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐