在编程时,有时无法确定某个函数是否会在使用者的Windows中存在,这种情况一般是由于Win9X和WinNT的区别造成的。用BCB可以使用动态加载的方法解决这个问题,但是按照这个思路用VB做的时候遇到了一点麻烦,那就是GetProcAddress返回的是一个指向函数的指针,VB里不知道怎么通过函数指针调用函数。不过,我们可以通过一些变通的方法来达到我们的目的,请看程序例。程序是用VB.NET写的,如果你用VB,请自己改一下函数声明和类型定义
Module Module1
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Integer
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Integer) As Integer
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Integer, ByVal lpProcName As String) As Integer
'要使用动态调用的函数,也要在这里声明,VB.NET是可以声明不存在的函数的。
'例如我们需要调用DllGetVersion这个函数,但我们其实不知道这个函数是否存在。
'为了达到效果,我们故意声明了一个不存在的函数MyDllGetVersion。
'先定义需要的类型。
Structure DLLVERSIONINFO
Public cbSize As Integer
Public dwMajor As Integer
Public dwMinor As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer
Public dwFlag As Integer
Public ddUllVer As Long
End Structure
Declare Function DllGetVersion Lib "COMCTL32" (ByRef pdvi As DLLVERSIONINFO) As Integer
Declare Function MyDllGetVersion Lib "COMCTL32" (ByRef pdvi As DLLVERSIONINFO) As Integer
Sub Main()
Dim hMod As Integer
Dim Ret As Integer
Dim pfDllVersion As Integer
Dim tDVI As DLLVERSIONINFO
hMod = LoadLibrary("comctl32.dll")
If hMod <> 0 Then
pfDllVersion = GetProcAddress(hMod, "DllGetVersion")
If (pfDllVersion <> 0) Then
tDVI.cbSize = Len(tDVI)
Ret = DllGetVersion(tDVI) '注意,我们无法将pfDllVersion转换为需要的函数类型指针,但由于已经声明,可以直接使用。
If (Ret = 0) Then
Console.WriteLine("DLL Version:" & tDVI.dwMajor & "." & tDVI.dwMinor & "." & tDVI.dwBuildNumber)
Else
Console.WriteLine("Function DllGetVersion failed.")
End If
Else
Console.WriteLine("DllGetVersion not exist.")
End If
pfDllVersion = GetProcAddress(hMod, "MyDllGetVersion")
If (pfDllVersion <> 0) Then
tDVI.cbSize = Len(tDVI)
Ret = MyDllGetVersion(tDVI)
If (Ret = 0) Then
Console.WriteLine("My DLL Version:" & tDVI.dwMajor & "." & tDVI.dwMinor & "." & tDVI.dwBuildNumber)
Else
Console.WriteLine("Function MyDllGetVersion failed.")
End If
Else
Console.WriteLine("MyDllGetVersion not exist.")
End If
FreeLibrary(hMod)
End If
End Sub
End Module
Module Module1
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Integer
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Integer) As Integer
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Integer, ByVal lpProcName As String) As Integer
'要使用动态调用的函数,也要在这里声明,VB.NET是可以声明不存在的函数的。
'例如我们需要调用DllGetVersion这个函数,但我们其实不知道这个函数是否存在。
'为了达到效果,我们故意声明了一个不存在的函数MyDllGetVersion。
'先定义需要的类型。
Structure DLLVERSIONINFO
Public cbSize As Integer
Public dwMajor As Integer
Public dwMinor As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer
Public dwFlag As Integer
Public ddUllVer As Long
End Structure
Declare Function DllGetVersion Lib "COMCTL32" (ByRef pdvi As DLLVERSIONINFO) As Integer
Declare Function MyDllGetVersion Lib "COMCTL32" (ByRef pdvi As DLLVERSIONINFO) As Integer
Sub Main()
Dim hMod As Integer
Dim Ret As Integer
Dim pfDllVersion As Integer
Dim tDVI As DLLVERSIONINFO
hMod = LoadLibrary("comctl32.dll")
If hMod <> 0 Then
pfDllVersion = GetProcAddress(hMod, "DllGetVersion")
If (pfDllVersion <> 0) Then
tDVI.cbSize = Len(tDVI)
Ret = DllGetVersion(tDVI) '注意,我们无法将pfDllVersion转换为需要的函数类型指针,但由于已经声明,可以直接使用。
If (Ret = 0) Then
Console.WriteLine("DLL Version:" & tDVI.dwMajor & "." & tDVI.dwMinor & "." & tDVI.dwBuildNumber)
Else
Console.WriteLine("Function DllGetVersion failed.")
End If
Else
Console.WriteLine("DllGetVersion not exist.")
End If
pfDllVersion = GetProcAddress(hMod, "MyDllGetVersion")
If (pfDllVersion <> 0) Then
tDVI.cbSize = Len(tDVI)
Ret = MyDllGetVersion(tDVI)
If (Ret = 0) Then
Console.WriteLine("My DLL Version:" & tDVI.dwMajor & "." & tDVI.dwMinor & "." & tDVI.dwBuildNumber)
Else
Console.WriteLine("Function MyDllGetVersion failed.")
End If
Else
Console.WriteLine("MyDllGetVersion not exist.")
End If
FreeLibrary(hMod)
End If
End Sub
End Module
0人赞
分享
二维码
赏一个