static void CopyDll(string inputpe, List <string> searchdir, string target = "", bool overwrite = false) { PEModel model = new PEModel(); model.LoadPE(inputpe); if (model.IsManaged == CompilationMode.Invalid) { Console.WriteLine("The input file is not a valid PE file"); } else { List <string> dependlist = new List <string>(); if (model.IsManaged == CompilationMode.Native) { NativeDepend nd = new NativeDepend(); nd.Init(inputpe); List <string> dllneedcpy = nd.FindMissingDll(); Tuple <List <string>, List <string> > findresult = SearchNativeDllRecur(dllneedcpy, searchdir, model.Arch); if (findresult.Item2.Count() > 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The following dependencies cannot be found"); } foreach (string dllmiss in findresult.Item2) { Console.WriteLine("{0}", dllmiss); } Console.ForegroundColor = ConsoleColor.White; foreach (string dllfound in findresult.Item1) { Console.WriteLine("Found:\t{0} in {1}", Path.GetFileName(dllfound), dllfound); } Console.WriteLine("Copying files to {0}", Path.GetDirectoryName(inputpe)); DllCopy copy = new DllCopy { TargetDir = (target == "") ? Path.GetDirectoryName(inputpe) : target }; foreach (string dllfound in findresult.Item1) { Console.WriteLine("Copying\t{0} to {1}", Path.GetFileName(dllfound), copy.TargetDir); copy.CopyDll(Path.GetFileName(dllfound), Path.GetDirectoryName(dllfound), overwrite); } } } }
static Tuple <List <string>, List <string> > SearchNativeDllRecur(List <string> dllname, List <string> searchpath, MachineType mt) { // Determine the dll need to find List <string> needtofind = new List <string>(); needtofind.AddRange(dllname); List <string> dllfound = new List <string>(); List <string> dllnotfound = new List <string>(); do { Tuple <List <string>, List <string> > findresult = SearchNativeDll(needtofind, searchpath, mt); // These are not found dlls in the determined path dllnotfound.AddRange(findresult.Item2); dllfound.AddRange(findresult.Item1); var nf = from name in dllfound.Union(dllnotfound) where needtofind.Contains(Path.GetFileName(name)) select Path.GetFileName(name); needtofind = needtofind.Except(nf).ToList(); foreach (string dll in findresult.Item1) { NativeDepend nd = new NativeDepend(); nd.Init(dll); List <string> depend = nd.FindMissingDll(true); nf = from name in dllfound.Union(dllnotfound) where depend.Contains(Path.GetFileName(name)) select Path.GetFileName(name); var t = depend.Except(nf); needtofind.AddRange(t); needtofind = needtofind.Distinct().ToList(); } } while (needtofind.Count() != 0); return(new Tuple <List <string>, List <string> >(dllfound, dllnotfound)); }