Visual Basic Programming Code Examples Visual Basic > API and Miscellaneous Code Examples Determine if an API exists (can be called) from a machine Determine if an API exists (can be called) from a machine The following code checks to see if an API can be called from a machine: Option Explicit Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long 'Purpose : Determines if a specific API is available on a machine. 'Inputs : sFunctionName The name of the function within the library eg. "FindWindowA". ' sDllName The name of the DLL eg. "user32". 'Outputs : Returns True if the API is available on this machine. 'Notes : Do NOT use an alias name for sFunctionName, you must use the original API name. ' eg. use "FindWindowA" rather then the commonly used alias "FindWindow". Function APIAvailable(ByVal sFunctionName As String, ByVal sDllName As String) As Boolean Dim lHandle As Long, lAddr As Long On Error Resume Next 'Load the library lHandle = LoadLibrary(sDllName) If lHandle <> 0 Then 'Get the address to the function name APIAvailable = CBool(GetProcAddress(lHandle, sFunctionName)) 'Release the resource FreeLibrary lHandle Else APIAvailable = False End If On Error Goto 0 End Function 'Demonstration routine Sub Test() If APIAvailable("FindWindowA", "user32") Then MsgBox "API call FindWindowA is available..." Else MsgBox "API call FindWindowA is NOT available..." End If End Sub