Handling .dll files

spurs

Active Member
Joined
Oct 18, 2006
Messages
433
I am writing VBA code that has to interact with a .dll file named master.dll which will be located on a USB memory stick (for example possibly drive E: F: or G:. In each case the subdirectory where master.dll is located will be in a subdirectory called Tickets

So the .dll file can be
any drive letter X followed by
X:\Tickets\Master.dll

The excel file containing the VBA code can be run from a different drive than where the Master.dll resides

I realize that I need a statement something like:
Private Declare Function MasterRT Lib "X:\Tickets\Master.dll" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer

1) If I don't know the letter of the drive where the USB is plugged in; how should I code the above line?
2) How can I trap the error if Master.dll is not found or available ? If this is the case I want to properly shut down the macro.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,203
Office Version
  1. 2016
Platform
  1. Windows
Not sure if your dll will support aliases but try the following and see if it works for you :

Code:
Option Explicit

Private Declare Function C_MasterRT Lib "C:\Tickets\Master.dll" Alias "MasterRT" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function D_MasterRT Lib "D:\Tickets\Master.dll" Alias "MasterRT" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function E_MasterRT Lib "E:\Tickets\Master.dll" Alias "MasterRT" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function F_MasterRT Lib "F:\Tickets\Master.dll" Alias "MasterRT" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Declare Function G_MasterRT Lib "G:\Tickets\Master.dll" Alias "MasterRT" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer


Sub Test()

    Dim oFSO As Object, oDrv As Object, lPathLength As Long
    Dim lErrNumer As Long, sErrDescription As String
    Dim lpBuffer As String, nSize As Integer
    
    lpBuffer = "Hello":       nSize = 5
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    For Each oDrv In oFSO.Drives
    
        On Error Resume Next
        lPathLength = Len(Dir(oDrv.DriveLetter & ":\Tickets\Master.dll"))
        
        If lPathLength Then
            Select Case oDrv.DriveLetter
                Case "C"
                    Debug.Print C_MasterRT(lpBuffer, nSize)
                Case "D"
                    Debug.Print D_MasterRT(lpBuffer, nSize)
                Case "E"
                    Debug.Print E_MasterRT(lpBuffer, nSize)
                Case "F"
                    Debug.Print F_MasterRT(lpBuffer, nSize)
                Case "G"
                    Debug.Print G_MasterRT(lpBuffer, nSize)
            End Select
            Exit For
        End If
        
    Next oDrv
    
    lErrNumer = Err.Number
    sErrDescription = Err.Description
    If lErrNumer Then
        On Error GoTo 0
        Err.Raise lErrNumer, , sErrDescription
    End If

End Sub
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,129,553
Messages
5,636,976
Members
416,953
Latest member
broexc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top