-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (78 loc) · 2.69 KB
/
main.cpp
File metadata and controls
96 lines (78 loc) · 2.69 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <windows.h>
#include <Dbghelp.h>
typedef std::map<std::string, std::string> DepMapType;
//------------------------------------------------------------------------------
const DepMapType getDependencies(const HMODULE hMod)
{
DepMapType depsMap;
IMAGE_DOS_HEADER* pDosHeader = (IMAGE_DOS_HEADER*)hMod;
IMAGE_OPTIONAL_HEADER * pOptHeader = (IMAGE_OPTIONAL_HEADER*)((BYTE*)hMod + pDosHeader->e_lfanew +24);
IMAGE_IMPORT_DESCRIPTOR* pImportDesc = (IMAGE_IMPORT_DESCRIPTOR*) ((BYTE*)hMod + pOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while ( pImportDesc->FirstThunk )
{
char* dllName = (char*) ((BYTE*)hMod + pImportDesc->Name);
std::string dllPath = "";
HMODULE hModDep = ::LoadLibraryEx(dllName, NULL, DONT_RESOLVE_DLL_REFERENCES);
if(hModDep != 0)
{
LPTSTR strDLLPath = new TCHAR[_MAX_PATH];
::GetModuleFileName(hModDep, strDLLPath, _MAX_PATH);
dllPath = std::string(strDLLPath);
delete strDLLPath;
}
FreeLibrary(hModDep);
depsMap[std::string(dllName)] = dllPath;
pImportDesc++;
}
return depsMap;
}
//------------------------------------------------------------------------------
int printDependencies(const std::string libPath)
{
HMODULE hMod = ::LoadLibraryEx(libPath.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES);
if(hMod == 0)
{
// Retrieves the last error message.
DWORD lastError = GetLastError();
char buffer[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, lastError, 0, buffer, 1024, 0 );
std::cerr << "\t" << buffer << std::endl;
return -1;
}
const DepMapType& depMap = getDependencies(hMod);
FreeLibrary(hMod);
DepMapType::const_iterator iter;
for(iter = depMap.begin(); iter != depMap.end(); ++iter)
{
std::cout << "\t" << iter->first << " => " << iter->second << std::endl;
}
return 0;
}
//------------------------------------------------------------------------------
int printUsage()
{
std::cout << "ldd for Windows, Version 1.0" << std::endl;
std::cout << "usage:\n ldd FILE..." << std::endl;
return -1;
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
if(argc <= 1)
{
return printUsage();
}
int res = 0;
for(int i = 1; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
res = printDependencies(argv[i]);
}
return res;
}
//------------------------------------------------------------------------------