Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does DllImport obey SafeDllSearchMode when searching for DLLs?

Let's say I have a typical unmanaged call I want to make in some DLL:

[DllImport("unmanaged.dll")]
static extern int SomeFuncion1(int parm);

Does the DllImportAttribute search for the DLL according to the SafeDllSearchMode setting in the registry? I couldn't find anything in MSDN that indicates if indeed the search order follows the "standard search order".

like image 740
Mike Atlas Avatar asked Jan 20 '26 08:01

Mike Atlas


1 Answers

Yes, the P/Invoke marshaller just uses LoadLibrary(). Which observes the setting. It is unprovable that it actually does in a SO post, until you try it yourself, I concluded this by being pretty sure there is no reasonable alternative. LoadLibrary squarely belongs in the category of the 'hard' API functions.

Fwiw, it will never find that DLL with your [DllImport] declaration. Unmanaged DLLs just have a path, they don't have managed assembly properties like version, culture, pkt. If this is in fact a managed assembly with those properties then you load it with Assembly.Load(). But you'll have a hard time calling a static function, the CLR doesn't support that, every method must belong to a class.

Use Dumpbin.exe /exports on that DLL to find out what is actually getting exported from that DLL.

like image 94
Hans Passant Avatar answered Jan 22 '26 22:01

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!