● DLL内の関数の有無をチェックする ●

VBでは使う機会はなさそう。じゃあC言語だと使う機会はあるのか?正直、知らん。

'モジュールをロードする
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

'ロードしたモジュールを開放する
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

'モジュール内の関数のアドレスを取得する
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long

'-----------------------------------------------------------------------
' 関数名 : IsDllFunc
' 機能   : DLL内の関数の有無をチェックする
' 引数   : (in) DllName  … dllファイル名
'           (in) DllFunc  … dllファイルの関数名
' 戻り値 : -1(=True)…正常終了
'           1…DLLファイルが存在しない
'           2…DLLファイルは存在するが、関数は存在しない
'-----------------------------------------------------------------------
Public Function IsDllFunction(ByVal DllName As String, _
                          ByVal DllFunc As String) As Long

    Dim hLibrary As Long

    '正常終了値を割り当てちゃえ
    IsDllFunction = True

    '返り値が0であれば、DLLファイルは存在しない
    hLibrary = LoadLibrary(DllName)
    If hLibrary = 0 Then
        IsDllFunction = 1
        Exit Function
    End If

    'モジュール内に関数が存在するかどうかの取得
    If GetProcAddress(hLibrary, DllFunc) = 0 Then
        IsDllFunction = 2
        Exit Function
    End If

    'ロードしたモジュールを開放する
    Call FreeLibrary(hLibrary)

End Function

呼び出し側はこんな感じでどうぞ。

Private Sub Command1_Click()

    Dim FuncRet As Long
    FuncRet = IsDllFunction("kernel32.dll", "CopyFileA")
    Select Case FuncRet
        Case True
            Debug.Print "関数は存在する"
        Case 1
            Debug.Print "DLLファイルが存在しない"
        Case 2
            Debug.Print "関数は存在しない"
    End Select

End Sub

戻る