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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.