获取dll编译时生成的pdb文件的名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ExtraPdbName.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
 
#include <iostream>
 
#include <windows.h>
#include <cstdlib>
#include <cstdio>
 
struct PdbInfo
{
    DWORD     Signature;
    BYTE      Guid[16];
    DWORD     Age;
    char      PdbFileName[1];
};
 
int main(int argc, char* argv[])
{
    for (int i = 1; i < argc; ++i)
    {
        HMODULE module = ::LoadLibraryA(argv[i]);
 
        if (module == 0)
        {
            std::cout << "Failed load :" << argv[i] << std::endl;
            continue;
        }
 
        // Figure out where the executable is mapped in memory.
        uintptr_t base_pointer = (uintptr_t)module;
 
        // This is where the MZ...blah header lives (the DOS header)
        IMAGE_DOS_HEADER* dos_header = (IMAGE_DOS_HEADER*)base_pointer;
 
        // We want the PE header.
        IMAGE_FILE_HEADER* file_header = (IMAGE_FILE_HEADER*)(base_pointer + dos_header->e_lfanew + 4);
 
        // Straight after that is the optional header (which technically is optional, but in practice always there.)
        IMAGE_OPTIONAL_HEADER* opt_header = (IMAGE_OPTIONAL_HEADER*)(((char*)file_header) + sizeof(IMAGE_FILE_HEADER));
 
        // Grab the debug data directory which has an indirection to its data
        IMAGE_DATA_DIRECTORY* dir = &opt_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
 
        // Convert that data to the right type.
        IMAGE_DEBUG_DIRECTORY* dbg_dir = (IMAGE_DEBUG_DIRECTORY*)(base_pointer + dir->VirtualAddress);
 
        // Check to see that the data has the right type
        if (IMAGE_DEBUG_TYPE_CODEVIEW == dbg_dir->Type)
        {
            PdbInfo* pdb_info = (PdbInfo*)(base_pointer + dbg_dir->AddressOfRawData);
            if (0 == memcmp(&pdb_info->Signature, "RSDS", 4))
            {
                printf("%s PDB path: %s\n", argv[i], pdb_info->PdbFileName);
            }
        }
         
        ::FreeLibrary(module);
    }
 
    system("pause");
     
    return 0;
}

  

posted @   bodong  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示