VB and VBA Users Source Code: Determine if an API exists (can be called) from a machine
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determine if an API exists (can be called) from a machine
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, September 27, 2001
Hits:
679
Category:
Windows API
Article:
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. 'Author : Andrew Baker 'Date : 25/03/2000 '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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder