AllApi.net

[an error occurred while processing this directive]
 

Howto check whether a function can be called on a machine, without calling it first

If we can retrieve the procedure address of the function, the function exists and can be called. But how do we retrieve this address? This code shows it to you:

Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Function FunctionExists(sLibrary As String, sFunction As String) As Boolean
Dim hLib As Long, hProc As Long
hLib = LoadLibrary(sLibrary)
If hLib = 0 Then
MsgBox "Unable to load the library!"
Exit Function
End If
hProc = GetProcAddress(hLib, sFunction)
If hProc = 0 Then
MsgBox "Unable to find the '" + sFunction + "' entry point in the library '" + sLibrary + "'!"
Exit Function
End If
FreeLibrary hLib
FunctionExists = True
End Function

Here are some examples:

MsgBox FunctionExists("kernel32", "RtlMoveMemory") 'Should return True
MsgBox FunctionExists("kern32", "RtlMoveMemory") 'kern32 doesn't exist => the function should return False
MsgBox FunctionExists("kernel32", "RtlMoveMyMemory") 'RtlMoveMyMemory doesn't exist => the function should return False

Note: always check with the function's alias.
For example, if you try to check whether the GetWindowText-function is available on that computer, call the
FunctionExists-function with the parameters "user32" and "GetWindowTextA", not "GetWindowText".

 
Copyright © 1998-2000, The KPD-Team.
Send mail to KPDTeam@Allapi.net with comments about this web site.
This site is located at
http://www.Allapi.net/