From df3ab1c783693eddce4f20ba1348efd5347ad767 Mon Sep 17 00:00:00 2001
From: Zhang Huijie <yitianbuji@gmail.com>
Date: Sat, 21 Dec 2013 14:31:47 +0800
Subject: ntldd can search the app/dll's directory now

---
 programs/ntldd/ntldd.cpp | 62 ++++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 23 deletions(-)

diff --git a/programs/ntldd/ntldd.cpp b/programs/ntldd/ntldd.cpp
index 21fa110..0690b4e 100644
--- a/programs/ntldd/ntldd.cpp
+++ b/programs/ntldd/ntldd.cpp
@@ -11,29 +11,38 @@ typedef std::map<std::string, std::string> DepMapType;
 
 //------------------------------------------------------------------------------
 
-const DepMapType getDependencies(const HMODULE hMod)
+const std::string getDicectory ( const std::string &libPath )
+{
+    unsigned found = libPath.find_last_of ( "/\\" );
+    return libPath.substr ( 0,found+1 );
+}
+
+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);
+    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);
+        char* dllName = ( char* ) ( ( BYTE* ) hMod + pImportDesc->Name );
 
         std::string dllPath = "not found";
-        HMODULE hModDep = ::LoadLibraryEx(dllName, NULL, DONT_RESOLVE_DLL_REFERENCES);
-        if(hModDep != 0)
+        HMODULE hModDep = ::LoadLibraryEx (
+                              dllName, NULL,
+                              DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
+                          );
+        if ( hModDep != 0 )
         {
             LPTSTR  strDLLPath = new TCHAR[_MAX_PATH];
-            ::GetModuleFileName(hModDep, strDLLPath, _MAX_PATH);
-            dllPath = std::string(strDLLPath);
+            ::GetModuleFileName ( hModDep, strDLLPath, _MAX_PATH );
+            dllPath = std::string ( strDLLPath );
             delete strDLLPath;
         }
-        FreeLibrary(hModDep);
-        depsMap[std::string(dllName)] = dllPath;
+        FreeLibrary ( hModDep );
+        depsMap[std::string ( dllName )] = dllPath;
 
         pImportDesc++;
     }
@@ -43,22 +52,29 @@ const DepMapType getDependencies(const HMODULE hMod)
 
 //------------------------------------------------------------------------------
 
-int printDependencies(const std::string libPath)
+int printDependencies ( const std::string libPath )
 {
-    HMODULE hMod = ::LoadLibraryEx(libPath.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES);
-    if(hMod == 0)
+    std::string curDir=getDicectory ( libPath );
+
+    SetCurrentDirectory ( curDir.c_str() );
+
+    HMODULE hMod = ::LoadLibraryEx (
+                       libPath.c_str(), NULL,
+                       DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
+                   );
+    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 );
+        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);
+    const DepMapType& depMap = getDependencies ( hMod );
+    FreeLibrary ( hMod );
     DepMapType::const_iterator iter;
-    for(iter = depMap.begin(); iter != depMap.end(); ++iter)
+    for ( iter = depMap.begin(); iter != depMap.end(); ++iter )
     {
         std::cout << "\t" << iter->first << " => " << iter->second << std::endl;
     }
@@ -70,24 +86,24 @@ int printDependencies(const std::string libPath)
 
 int printUsage()
 {
-    std::cout << "ldd for Windows, Version 1.0" << std::endl;
+    std::cout << "ldd for Windows PE files" << std::endl;
     std::cout << "usage:\n ldd FILE..." << std::endl;
     return -1;
 }
 
 //------------------------------------------------------------------------------
 
-int main(int argc, char** argv)
+int main ( int argc, char** argv )
 {
-    if(argc <= 1)
+    if ( argc <= 1 )
     {
         return printUsage();
     }
     int res = 0;
-    for(int i = 1; i < argc; ++i)
+    for ( int i = 1; i < argc; ++i )
     {
         std::cout << argv[i] << std::endl;
-        res = printDependencies(argv[i]);
+        res = printDependencies ( argv[i] );
     }
     return res;
 }
-- 
1.8.3.2

