|
|
|
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: |
Click here to copy the following block | 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
Function IsProcedureAvailable(ByVal ProcedureName As String, _ ByVal DllFilename As String) As Boolean
Dim hModule As Long, procAddr As Long hModule = LoadLibrary(DllFilename) If hModule Then procAddr = GetProcAddress(hModule, ProcedureName) FreeLibrary hModule End If 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): |
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.
|
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|