Determine whether an API function is available

Determine whether an API function is available

In many cases you may want to call an API function, but you aren’t sure whether the Windows version the application is running on supports that particular function.

The easiest way to test whether a function exists and can be called is to replicate through VB code what Windows itself would do when calling the function. First, you try to load the DLL module using the LoadLibrary API function, and then you call the GetProcAddress API to retrieve the actual address. If you get a non-zero address, then the function has been found.

I have enclosed all these details in the followed routine:

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal _    lpLibFileName As String) As LongPrivate Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _    ByVal lpProcName As String) As LongPrivate Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) _    As LongFunction IsProcedureAvailable(ByVal ProcedureName As String, _    ByVal DllFilename As String) As Boolean    Dim hModule As Long, procAddr As Long    ' first, attempt to load the module    hModule = LoadLibrary(DllFilename)    If hModule Then        ' then, retrieve the address of the routine        procAddr = GetProcAddress(hModule, ProcedureName)        ' finally, decrement the DLL usage counter        FreeLibrary hModule    End If    ' if procAddr is non-zero, the function is available    IsProcedureAvailable = (procAddr <> 0)End Function

Here’s an example that shows how you can test whether the machine your app is running on does support the Support the “GetDiskFreeSpaceEx” API function (that was introduced in Windows 95 OSR2 release):

If IsProcedureAvailable("GetDiskFreeSpaceExA", "kernel32") Then    MsgBox "GetDiskFreeSpaceEx is supported"End If

Note that you can omit the “dll” extension in DllFileName, but you have to provide the complete path to the file if it isn’t in one of the system directories. Moreover, keep in mind that you should pass the real name of the procedure, not the alias used in VB. For example, most routines that accept strings among their arguments have a “A” and “W” version – for ANSI and Unicode strings, respectively – and you must include those trailing characters in the argument passed to IsProcedureAvailable.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing