Displaying and using a dialog box from external DLL file

The following code sample displays the standard "Run" dialog box of Microsoft Windows. The dialog box is loaded directly from shell32.dll

'The following code snippet shows you how to display a dialog box from external DLL file.
'In this sample, the standard "Run" dialog of Microsoft Windows is shown on the screen.
'The DialogBoxProc function is responsible for interacting with the user actions (e.g., pressing on a button).
'
'Written by Nir Sofer
'Web site: http://nirsoft.mirrorz.com

Public Declare Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" _
(ByVal hInstance As Long, ByVal lpTemplate As Long, ByVal hWndParent As Long, _
ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Long
Public Declare Function EndDialog Lib "user32" _
(ByVal hDlg As Long, ByVal nResult As Long) As Long

Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
(ByVal lpLibFileName As String) As Long
Public Declare Function FreeLibrary Lib "kernel32" _
(ByVal hLibModule As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetDlgItem Lib "user32" _
(ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Public Declare Function GetDlgItemText Lib "user32" Alias "GetDlgItemTextA" _
(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Public Const MB_OK = &H0&
Public Const MB_ICONINFORMATION = &H40&

Public Const CB_ADDSTRING = &H143
Public Const WM_INITDIALOG = &H110
Public Const WM_COMMAND = &H111

Public Const IDOK = 1
Public Const IDCANCEL = 2
Public Const BN_CLICKED = 0

Public Const ID_RUN_DIALOG = 1003
Public Const ID_RUN_DIALOG_COMBO = 12298
Public Const ID_RUN_DIALOG_BROWSE_BUTTON = 12288

Private SelectedValue               As String
Private OkButtonClicked             As Boolean

'The following function truncate the null character from a string.
Private Function TrimZero(str As String) As String
    Dim lngPos          As Long
    
    lngPos = InStr(str, Chr$(0))
    If lngPos > 0 Then
        TrimZero = Mid$(str, 1, lngPos - 1)
    Else
        TrimZero = str
    End If
End Function

'The following function is the main dialog procedure.
'Every message for the dialog goes through this function.
Public Function DialogBoxProc(ByVal hwndDlg As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim NotidyCode      As Long
    Dim ItemID          As Long
    Dim Index           As Integer
    Dim Str1            As String
    Dim hCombo          As Long
    Dim TempStr         As String * 255
    
    If uMsg = WM_COMMAND Then
        NotidyCode = wParam \ 65536
        ItemID = wParam And 65535
        
        'Check if you click the Ok or Cancel buttons.
        If (ItemID = IDOK Or ItemID = IDCANCEL) And NotidyCode = BN_CLICKED Then
            'Close the dialog box
            EndDialog hwndDlg, 0
            DialogBoxProc = 1
            If ItemID = IDOK Then
                'If you click the Ok button, copy the combo value into the SelectedValue variable
                GetDlgItemText hwndDlg, ID_RUN_DIALOG_COMBO, TempStr, 255
                SelectedValue = TrimZero(TempStr)
                OkButtonClicked = True
            Else
                OkButtonClicked = False
                SelectedValue = ""
            End If
            Exit Function
        End If
        
        'Check if you click the Browse button.
        If ItemID = ID_RUN_DIALOG_BROWSE_BUTTON And NotidyCode = BN_CLICKED Then
            'If you clicked the Browse button, show a message.
            MessageBox hwndDlg, "You clicked the browse button !", "Browse Button", MB_OK Or MB_ICONINFORMATION
            DialogBoxProc = 1
            Exit Function
        End If
    End If
    
    If uMsg = WM_INITDIALOG Then
        'Fill the combo-box with our fake values.
        hCombo = GetDlgItem(hwndDlg, ID_RUN_DIALOG_COMBO)
        For Index = 1 To 10
            Str1 = "Item Number " & Index
            SendMessage hCombo, CB_ADDSTRING, 0, ByVal Str1
        Next
    End If
    
    DialogBoxProc = 0
End Function

'The ShowDialog function shows the dialog on the screen, and returns a string in SelValue
'argument when the user exit from the dialog.
'If the user clicks the "Ok" button, the function returns a True value. Otherwise, it returns
'a False value.
Public Function ShowDialog(hParent As Long, SelValue As String) As Boolean
    Dim hShell32            As Long
    
    'Load the shell32 library that contains the dialog resource that i want to load.
    hShell32 = LoadLibrary("shell32.dll")
    If hShell32 <> 0 Then
        'Create a modal dialog from a dialog resource located in shell32.dll
        DialogBoxParam hShell32, ID_RUN_DIALOG, hParent, AddressOf DialogBoxProc, 0
        'After we exit from the dialog, free the shell32 library.
        FreeLibrary hShell32
        
        SelValue = SelectedValue
        ShowDialog = OkButtonClicked
    End If
    
End Function

Download this project