Load a custom DLL programmatically from another path

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
212
I have a DLL with I don't want to put it in the system folder.
I do I let VBA, how do I load dll from the path I want?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,493
Office Version
2016
Platform
Windows
I have a DLL with I don't want to put it in the system folder.
I do I let VBA, how do I load dll from the path I want?
Is that an ActiveX dll or a standard windows dll ?
Do you access the dll through the API declarations like the way you access windows dlls ?
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
212
Is that an ActiveX dll or a standard windows dll ?
Do you access the dll through the API declarations like the way you access windows dlls ?
I access the dll through the API declarations like this
Code:
[/COLOR]Declare Function GetVersion Lib "MyDLL" () As Integer
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,493
Office Version
2016
Platform
Windows
I access the dll through the API declarations like this
Rich (BB code):
Declare Function GetVersion Lib "MyDLL" () As Integer
Then you could pass the full path as follows :

Code:
Declare Function GetVersion Lib "[B]YOUR_DLL_PATH_HERE\[/B]MyDll.dll" () As Integer
 
Last edited:

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
212
Then you could pass the full path as follows :

Code:
Declare Function GetVersion Lib "[B]YOUR_DLL_PATH_HERE\[/B]MyDll.dll" () As Integer
I know much about doing it this way. but the problem with this is that if i move the file to another computer, the path won't work anymore.
so, i don't know if it's possible to put the dll in the same path of the workbook and make reference to it in vba to that anywhere i copy the file and the dll to, there won't be path failure?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,493
Office Version
2016
Platform
Windows
https://docs.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-search-order

1- According to the MS documentation ,the standard dll search order used by the system always includes the current directory therefore you could try, for example, placing your dll file in the workbook path and temporarly change the current Dir via code before calling the dll function as follows:

Code:
Declare Function GetVersion Lib "MyDLL" () As Integer

Function CallGetVersion() As Integer

    Dim sDir As String
    
    sDir = CurDir
    ChDir ThisWorkbook.Path
    CallGetVersion = GetVersion
    ChDir sDir

End Function


Sub Test()

    Dim iRet As Integer
    
    iRet = CallGetVersion
    MsgBox iRet

End Sub


2- Alternatively, you could palce the dll file in the workbook directory just as above but load the dll dynamically using the Loadlibrary API... something like this :

Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
    Declare PtrSafe Function FreeLibrary Lib "kernel32" (ByVal hLibModule As LongPtr) As Long
    Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
    Declare PtrSafe Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As LongPtr, ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If


Function CallGetVersion(ByVal Dll_Path As String) As Integer

    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
        Dim hLib As LongPtr, hProcAddr As LongPtr
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
        Dim hLib As Long, hProcAddr As Long
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

    hLib = LoadLibrary(Dll_Path)
    hProcAddr = GetProcAddress(hLib, "GetVersion")
    If hProcAddr Then
        CallGetVersion = CallWindowProc(hProcAddr, 0, 0, 0, 0)
    End If
    FreeLibrary hLib

End Function


Sub Test()

    Dim iRet As Integer
    
    iRet = CallGetVersion(ThisWorkbook.Path & "\MyDll.dll")
    MsgBox iRet

End Sub
The second method would need to be tweaked depending on the type and number of args of your dll function.
 
Last edited:

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,493
Office Version
2016
Platform
Windows
Thanks. I used the first method
You are welcome.

I would still test the code on a different machine before distributing the workbook and dll just to double-check that the code works
 

Forum statistics

Threads
1,082,126
Messages
5,363,315
Members
400,725
Latest member
excelingtolearn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top