| 26 | { |
| 27 | public: |
| 28 | bool Initialize(std::string const& path) |
| 29 | { |
| 30 | auto hr = CoCreateInstance(CLSID_DiaSource, |
| 31 | NULL, |
| 32 | CLSCTX_INPROC_SERVER, |
| 33 | __uuidof(IDiaDataSource), |
| 34 | (void**)&source_); |
| 35 | |
| 36 | if (FAILED(hr)) { |
| 37 | std::cout << "Could not CoCreate CLSID_DiaSource. Register the COM DLL msdia140.dll." << std::endl; |
| 38 | std::cout << "(Try 'regsvr32 \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\DIA SDK\\bin\\amd64\\msdia140.dll\"')" << std::endl; |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | wchar_t wszFilename[_MAX_PATH]; |
| 43 | size_t len; |
| 44 | mbstowcs_s(&len, wszFilename, path.c_str(), sizeof(wszFilename) / sizeof(wszFilename[0])); |
| 45 | if (FAILED(source_->loadDataFromPdb(wszFilename))) { |
| 46 | std::cout << "loadDataFromPdb failed" << std::endl; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if (FAILED(source_->openSession(&session_))) { |
| 51 | std::cout << "openSession failed" << std::endl; |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | CComPtr<IDiaEnumTables> pTables; |
| 56 | if (session_->getEnumTables(&pTables) != S_OK) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | VARIANT var; |
| 61 | var.vt = VT_BSTR; |
| 62 | var.bstrVal = SysAllocString(DiaTable_Sections); |
| 63 | IDiaTable* pTable; |
| 64 | if (pTables->Item(var, &pTable) != S_OK) { |
| 65 | std::cout << "Couldn't get section list" << std::endl; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | CComPtr<IDiaEnumSectionContribs> pEnumSections; |
| 70 | if (pTable->QueryInterface(__uuidof(IDiaEnumSectionContribs), (void**)&pEnumSections) != S_OK) |
| 71 | { |
| 72 | std::cout << "Couldn't get IDiaEnumSegments" << std::endl; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | ULONG celt; |
| 77 | CComPtr<IDiaSectionContrib> pSection; |
| 78 | while (SUCCEEDED(pEnumSections->Next(1, &pSection, &celt)) && celt == 1) { |
| 79 | DWORD segmentNo; |
| 80 | pSection->get_addressSection(&segmentNo); |
| 81 | ULONGLONG va; |
| 82 | pSection->get_virtualAddress(&va); |
| 83 | DWORD rva; |
| 84 | pSection->get_relativeVirtualAddress(&rva); |
| 85 | segmentRva_.insert(std::make_pair(segmentNo, rva)); |