How to Reference a DLL using <Dllimport> and a variable in the path?

Asked

Viewed 1,332 times

1

I’m having a problem at the moment of referencing a dll using the <DllImport>, because in the place where I put the dll path <DllImport("C:\MinhaDll.dll")>, i would like to use a variable, and is not possible, returns the following error:

"Error 11 Constant expression is required."

The variable I want to use will contain the dll path, so what other method can I use to reference a dll from a specific path created by the user himself, since I need a fixed path to use Dllimport?

  • If the paths absolute of the DLL are different, but the paths relative are equal, you can use a constant expression.

  • It will be different paths, it may be that in a micro a dll stay in C:, in another stay in the system32 or windows folder. That’s why I need to reference in a variable, ouu...?

  • And the name of dll? It is constant?

  • No, neither name nor path, only functions.

1 answer

2


I believe this is not possible to do.

However, you can add a directory to the process DLL search path using the function AddDllDirectory, but as it is is a recent API, if you are using a version inferior to Windows 8, it will be necessary to install a patch.

The signature of the function is:

Imports System.Runtime.InteropServices
'

<DllImport("kernel32", SetLastError:=True, CharSet:=CharSet.Unicode)> _
 Public Function AddDllDirectory(NewDirectory As String) As Integer
 End Function

If you want to modify the default search path, use the function SetDllDirectory.

An article that may be useful to read is Dynamic-Link Library Search Order that mentions:

Before the system searches for a DLL, it scans the following:

  • If a DLL with the same name of the module is already loaded in memory, the system uses the DLL loaded, no matter which directory which is found. The system is not searching for the DLL.
  • If the DLL is in the list of known Dlls for the version of Windows on which the application runs, the system uses its copy of DLL known (and DLLS dependent on the known DLL, if exists). The system does not search for the DLL.

To get a dll list known in the current system, see the following registry key: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerKnownDLLs.

In a passage further down the value is still mentioned SafeDllSearchMode which is kept in the register.

If SafeDllSearchMode is enabled in Winxp it is disabled by default, the search order is as follows:

  1. The directory from which the application was loaded.
  2. The system directory. Use the function GetSystemDirectory to get the path to that directory.
  3. The system directory of 16 bits. There is no function that gets the path from this directory, but it is searched.
  4. The Windows directory. Use the function GetWindowsDirectory to get the path to that directory.
  5. The current directory.
  6. The directories listed in the environment variable PATH. Note that this does not include the path per application specified by the registration key App Paths.
  • 1

    I don’t know if it’s an option for you, but look too: Run-Time Dynamic Linking.

  • Very interesting, in case this function serves only I add the dll I want in a list of dlls, and then when I run the function of my dll it will search all dll that are in that list and check the dll that contains the function I’m calling, right? Ta, but in the way of my function I should put what? I say in the dllImport of my function. 'Cause I didn’t get that part, or I shouldn’t use money anymore for my job ?

  • 1

    @Felipewalleg Correct. No DLLImport you reference your DLL normally, for example: <DllImport("MinhaDll.dll")>, without the path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.