Bug 17038 - DLLImport search path does not contain current directory
Summary: DLLImport search path does not contain current directory
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-01-03 08:47 UTC by mholenko
Modified: 2014-01-03 19:19 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Example application code (284 bytes, text/x-csharp)
2014-01-03 08:47 UTC, mholenko

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description mholenko 2014-01-03 08:47:24 UTC
Created attachment 5751 [details]
Example application code

Description of Problem:

According to the MS documentation DLLImport attribute should look for a library i.a., in the current directory (the one described by Directory.GetCurrentDirectory()). This behaviour is confirmed in the current version of .NET framework implementation. However, current mono implementation seem to check just base directory (the one application started from) and fails to find library if the current directory was changed.

Steps to reproduce the problem:
1. Create a simple program that uses function from external lib and bind it using DLLImport attribute (example in attachment).

Actual Results:

Expected Results:

How often does this happen? 

Additional Information:
Comment 1 mholenko 2014-01-03 08:53:56 UTC
/* I pressed the submit button too soon, so there is the rest of the problem description */

Steps to reproduce the problem:
1. Create a simple program that uses function from external lib and bind it
using DLLImport attribute (example in attachment).
2. Copy the library to the parent folder of location where binary is stored:
   +--- libLib.so
   +--- bin
        +--- example.exe
3. Go to the project_folder/bin and start the application: mono example.com

Actual Results:
There is an exception saying that Dll was not found.

Expected Results:
Dll should be found, loaded and a function should be executed.

How often does this happen? 

Additional Information:
Starting application with proper LD_LIBRARY_PATH environmental variable does the trick, so when starting application like below works.

project_folder/bin$ LD_LIBRARY_PATH=".." mono example.com
Comment 2 Mikayla Hutchinson [MSFT] 2014-01-03 19:19:49 UTC
P/Invoke is a means of calling into native dynamic libraries. It uses the native loader mechanisms to load these libraries. What MS has documented is the *Windows* Dynamic-Link Library Search Order:

On Linux, Mac, etc, Mono uses the platform' dynamic library search mechanisms.