2

I am working on an application that imports an unmanaged dll into C#. It has a wrapper class that loads the methods so it can be called. The methods work fine in the program from the dll. I want to add saving the version of the dll that is being used. I found that I need to use FileVersionInfo.GetVersionInfo("my.dll") thanks to C# getting version of unmanaged dll. However, when running this function it exceptions saying it can't find "my.dll". The dll is in a folder off the root of the c:. This folder is in the PATH and according to http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx#search_order_for_desktop_applications it should find it.

Knowing that the my.dll file is loaded and working why can't I also call GetVersionInfo() inside the same wrapper class and find my.dll so I can get the same file's version number? Thanks for the help as I have been looking for a couple of days.

Community
  • 1
  • 1
smurphy
  • 133
  • 6

2 Answers2

1

From MSDN, the parameter for GetVersionInfo is:

The fully qualified path and name of the file to retrieve the version information for.

So it's pretty clear. You need to pass the full path, as it seems this function relies on it. Otherwise it will most likely look for the file in the current directory (so your app's dir).

As a side note, keeping the native DLL in C:\ is bad practice. You should store it in your application's folder. Then this function would work and your app would be more self contained. No files spread around the disk. Of course, this is true unless you have a good reason for storing it in the root of your C drive.

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
  • Ok, I missed the path and name requirement. Is there a way to know where DllImport found a dll? Perhaps search for the dll with the same method that DllImport uses? Note:I am not sure of the reasoning that the dll is located in a sub-folder off the c:\. – smurphy Jun 18 '12 at 19:20
  • In this case you can't do much. Underneath, both versions use the native method GetFileVersionInfo (http://pinvoke.net/default.aspx/coredll/GetFileVersionInfo.html). It would be a little effort to do it but luckily you have some help: http://stackoverflow.com/questions/2283550/retrieving-dll-version-info-via-win32-verqueryvalue-crashes-under-win7-x6 – Marcel N. Jun 18 '12 at 19:27
1

It requires the full path, it won't search for the DLL. That's too risky, the Windows search rules for a DLL are intricate and subject to configuration. If you need to do this before pinvoking any function then the best way is by pinvoking SearchPath(). Which uses the same algorithm as LoadLibrary uses if you set the first argument to null.

If you need to do this after pinvoking a function then the best way is by using the loaded DLL. Iterate Process.GetCurrentProcess().Modules to retrieve the ProcessModule.FileName.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • SearchPath() was the final solution to finding the file. Then GetFileVersionInfo() got my version number. Thanks all that left ideas. More info on SearchPath(): http://msdn.microsoft.com/en-us/library/aa365527(v=vs.85) – smurphy Jun 19 '12 at 13:30