Load a custom DLL programmatically from another path

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
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,427
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
209
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,427
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
209
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,427
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,427
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,078,488
Messages
5,340,638
Members
399,387
Latest member
amrita34

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top