tags:

views:

16

answers:

1

We have a VB.NET program that needs to periodically call a function in an external, unmanaged DLL to communicate with our legacy application. We are having a problem with the application (seemingly) randomly not being able to find the DLL with the unmanaged code. Currently we use DECLARE FUNCTION blah LIB for the unmanaged code. Would it be better/more reliable to use DllImportAttribute instead? Or am I missing something else?

+3  A: 

It makes no difference whatsoever, it's the exact same P/Invoke marshaller that gets the job done. The Declare statement assumes different defaults, like CharSet, so watch out if you intend to swap them.

The most typical cause of random DLL search issues is getting Environment.CurrentDirectory changed. Like when you use OpenFileDialog. Keep the DLL (and its dependencies) in the same directory as your EXE to avoid this.

Hans Passant