1

I will try to describe my question... I load, for example, "General.dll" in my project (C#) using

//load "General.dll"
dllHandle = LoadLibraryEx(generalPath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);

[DllImport("kernel32.dll")]         
private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);

[DllImport("General.dll")] ...

If I use a method from "General.dll" which requires "Gif.dll" I need to copy "Gif.dll" to the folder with "General.dll" (if I do not use such method I can do not copy "Gif.dll").

If I use a method from"General.dll" which uses "Dutch" language, I need to copy "Dutch.amd" to the folder with "General.dll".

For example, I have wrote some code which uses different methods from "General.dll". How to determine which files from the folder with "General.dll" I can delete?

Can I do this using Visual Studio? For example, compile my code in VS, find out which files were used during runtime and copy only these files.

Thanks!

elias
  • 53
  • 6

2 Answers2

3

Typically, any redistributable component will include deployment guidelines which state what files must be deployed, where they must be deployed to, and any registration that must occur. I highly recommend you review the documentation for general.dll, if it exists.

If general.dll is your own work, then look at the code to find all referenced files and assemblies. If it is too voluminous, a decent heuristic might be to run Process Monitor while using the component to monitor file and registry access. Many installer authoring tools use similar techniques to detect dependencies.

If you do not have documentation nor source code for the component in question, you can use dumpbin /dependents, Dependency Walker / depends.exe, or even just strings to find the imports declared. Beware, however, that this list is not exclusive, and may leave out critical files loaded conditionally (only on certain machine types, cultures, or otherwise).

Community
  • 1
  • 1
Mitch
  • 21,223
  • 6
  • 63
  • 86
  • thank you very much for your advices! I was able to detect all dependencies using Process Monitor. Now I want to do such work automatically (develop own application which will detect all dependencies for my case) :) – elias Apr 18 '14 at 07:53
  • Process monitor uses a kernel mode filesystem filter driver to detect file access (http://stackoverflow.com/questions/4833972/how-does-sysinternals-processmonitor-work). It also places a considerable load on the system unless filters are tight. It is not the sort of thing which is easily automated, but best of luck to you! – Mitch Apr 18 '14 at 16:39
  • Thanks for this information! I have read a bit about such approach. I think it will hard to develop such functionality :) I will need to use some C++ code. In any way, I am able to detect all dependencies (`*.dll` files) of a process using `Process.Modules` (`C#`), but this method does not provide information if a process reads (loads) `*.amd` etc. files. Is it possible only with using a `File System Filter Driver` or is it possible with using standard functionality of `C#`? – elias Apr 21 '14 at 10:24
  • You cannot intercept calls to `CreateFile` / `NtCreateFile` with .Net. The easiest way would probably be to use a project like Detours or EasyHook to inject a hook into the target process and log the path. – Mitch Apr 21 '14 at 14:34
1

Maybe DependencyWalker is the tool you are looking for. Just open an EXE or DLL file with this tool and it builds up a nice tree of dependencies to DLLs that are required. It also supports profiling to detect dynamic dependencies while runtime and offers many other features.

Oliver
  • 1,507
  • 1
  • 12
  • 23
  • Thank you! Unfortunately, DependencyWalker does not show all dependencies for general.dll during runtime (Options->Start Profiling...) – elias Apr 18 '14 at 06:13