Results 1 to 8 of 8

Thread: Load a custom DLL programmatically from another path
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Load a custom DLL programmatically from another path

    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?

  2. #2
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Quote Originally Posted by yinkajewole View Post
    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 ?
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  3. #3
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Quote Originally Posted by Jaafar Tribak View Post
    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:
    Declare Function GetVersion Lib "MyDLL" () As Integer

  4. #4
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Quote Originally Posted by yinkajewole View Post
    I access the dll through the API declarations like this
    Code:
    Declare Function GetVersion Lib "MyDLL" () As Integer
    Then you could pass the full path as follows :

    Code:
    Declare Function GetVersion Lib "YOUR_DLL_PATH_HERE\MyDll.dll" () As Integer
    Last edited by Jaafar Tribak; Jul 2nd, 2019 at 10:04 AM.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  5. #5
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Quote Originally Posted by Jaafar Tribak View Post
    Then you could pass the full path as follows :

    Code:
    Declare Function GetVersion Lib "YOUR_DLL_PATH_HERE\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?

  6. #6
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    https://docs.microsoft.com/en-us/win...y-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
    
    #If  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
    #Else 
        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
    #End  If
    
    
    Function CallGetVersion(ByVal Dll_Path As String) As Integer
    
        #If  VBA7 Then
            Dim hLib As LongPtr, hProcAddr As LongPtr
        #Else 
            Dim hLib As Long, hProcAddr As Long
        #End  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 by Jaafar Tribak; Jul 2nd, 2019 at 07:35 PM.
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  7. #7
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Thanks. I used the first method

  8. #8
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,351
    Post Thanks / Like
    Mentioned
    39 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Load a custom DLL programmatically from another path

    Quote Originally Posted by yinkajewole View Post
    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
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •