Nando
Nando

Reputation: 33

Declaring third party DLL dynamically in VB.NET

I'm facing a problem importing a DLL on different environments. I have to check Windows platform and import the third-party DLL that will be placed in C:\Program Files\ (for 32-bit) or C:Program Files (x86)\ (for 64-bit).

Before the code was written like this:

Declare Function RDRCConnect Lib "c:\program files\TP-DLL\RDRCAP32.DLL" (ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer  
Declare Function RDRCDisconnect Lib "c:\program files\TP-DLL\RDRCAP32.DLL" (ByVal lNetConn As Integer, ByRef lNetErr As Integer) As Integer

...and I changed to use attributes:

Private Const CheminDLL As String = "C:\Program Files\TP-DLL\RDRCAP32.DLL"

<System.Runtime.InteropServices.DllImport(CheminDLL)>
Private Shared Function RDRCConnect(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

How can I change the DLL path dinamically in this scenario, once the DLLImport expects a Constant as parameter?

Upvotes: 3

Views: 4078

Answers (3)

Steven Doggart
Steven Doggart

Reputation: 43743

There's no way to pass anything other than a constant into the attribute, since attributes, by definition are evaluated at compile-time, not at runtime. There may be better alternatives, but one option I can give you would be to create separate imports for each version:

<DllImport("C:\Program Files\TP-DLL\RDRCAP32.DLL", EntryPoint := "RDRCConnect")>
Private Shared Function RDRCConnect32(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

<DllImport("C:\Program Files (x86)\TP-DLL\RDRCAP32.DLL", EntryPoint := "RDRCConnect")>
Private Shared Function RDRCConnect64(ByVal lpszServerName As String, ByVal lNetConnType As Integer, ByVal lpszParam1 As String, ByVal lpszParam2 As String, ByVal lpszParam3 As String, ByRef lNetConn As Integer, ByRef lNetErr As Integer) As Integer
End Function

Then you would need to choose which one to call appropriately each time you call the method.

Upvotes: 3

tcarvin
tcarvin

Reputation: 10855

If the dlls are the same (same name and signatures) but just in different locations then you can p/invoke LoadLibrary explicitly with the full path of the dll as determined at runtime. As long as you do this before you invoke any of the exported APIs then it will use the one already loaded as long as the name of the dll in the import is the same.

Upvotes: 0

Justin Shep
Justin Shep

Reputation: 295

I just stumbled upon this a couple of days ago when I had a similar issue to yours and it put me on the right path. Look for it here.

Upvotes: 0

Related Questions