使用Visual Basic 6.0,根据客户选择从远端Ftp服务器上先下载一个Dll(这个文件有很多,而且经常更新,出于安全考虑也不能把所有更新版本都放到客户端,只能要什么给什么),然后如何动态的在程序中调用之?(根据文件名的命名规则调用已经下载到本地的dll)。
“Declare ...”需要在程序初始化的时候声明,而且要求文件名确定。
不同Dll的接口是相同的。
现在的做法是用C++写个Wrapper的dll去动态调用(“Loadlibrary”)这些dll,然后让VB去调用这个Wrapper dll,太过复杂了。
“Declare ...”需要在程序初始化的时候声明,而且要求文件名确定。
不同Dll的接口是相同的。
现在的做法是用C++写个Wrapper的dll去动态调用(“Loadlibrary”)这些dll,然后让VB去调用这个Wrapper dll,太过复杂了。
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
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 CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Sub Form_Load()
On Error Resume Next
Dim lb As Long, pa As Long
lb = LoadLibrary("user32")
pa = GetProcAddress(lb, "SetWindowTextA")
'Call the SetWindowTextA-function
CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0&
FreeLibrary lb
End Sub
那么你可以使用如下代码:
Private Sub Form_Load()
Dim Conn As Object
Set Conn = CreateObject("ADODB.Connection")
Conn.connectionstring = "asdf"
MsgBox Conn.connectionstring
End Sub
你想想看,你肯定知道动态库中有方法DllRegisterServer,要不然神仙也没辙的啊Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
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 CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Const ERROR_SUCCESS = &H0
Private Sub Form_Load()
Text1.Text = "C:\WINDOWS\SYSTEM\COMCTL32.OCX"
Command1.Caption = "Register server"
Command2.Caption = "Unregister server"
End Sub
Private Sub Command1_Click()
Call RegisterServer(Me.hWnd, Text1.Text, True)
End Sub
Private Sub Command2_Click()
Call RegisterServer(Me.hWnd, Text1.Text, False)
End Sub
Public Function RegisterServer(hWnd As Long, DllServerPath As String, bRegister As Boolean)
On Error Resume Next
Dim lb As Long, pa As Long
lb = LoadLibrary(DllServerPath)
If bRegister Then
pa = GetProcAddress(lb, "DllRegisterServer")
Else
pa = GetProcAddress(lb, "DllUnregisterServer")
End If
If CallWindowProc(pa, hWnd, ByVal 0&, ByVal 0&, ByVal 0&) = ERROR_SUCCESS Then
MsgBox IIf(bRegister = True, "Registration", "Unregistration") + " Successful"
Else
MsgBox IIf(bRegister = True, "Registration", "Unregistration") + " Unsuccessful"
End If
FreeLibrary lb
End Function