vb如何动态加载dll文件,并且调用其中的函数
vb动态加载dll的,我加载的函数是int max(int *a,int b)参数怎么传递,
a为数组,存放的整数,b为数组中整数的个数
周文星 (用户名:SupermanKing) 他好像以前写过这个文章,但是加载的函数是字符串文本。我现在的函数是求数组中最大值,忘高人指点,给出源代码。
[解决办法]
把数组首地址传入 ,传入个数 就可以了
[解决办法]
- VB code
Private Declare Function Max Lib "a.dll" Alias "MaxA" (ByRef a() As Double, ByVal L As Integer) As DoublePrivate Sub Form_Load() Dim a(1 To 10) As Double Dim i As Integer Dim MaxNum As Double For i = 1 To 10 a(i) = CDbl(Rnd * 10) Next i MaxNum = Max(a, 10)End Sub
[解决办法]
[解决办法]
http://msdn.microsoft.com/zh-cn/library/chsc1tx6(VS.80).aspx
callbyname[size=18px][/size],这是vb6中新增的函数,可以在程序中动态调用dll,然后执行dll含的函数和方法,具体参照上面链接
[解决办法]
CallByName函数是一个灵活性很强的函数,通过它可以实现通过字符串调用方法以及回调的功能,这里给出一个使用示例,具体应用具体分析并实现。
首先,定义一个类模块,并起名为CMyObject,类实现代码如下:
Option Explicit
Private MyPropValue As Integer
Public Function Multiply(x As Integer, y As Integer) As Integer
Multiply = x * y
End Function
Public Property Get MyProperty() As Variant
MyProperty = MyPropValue
End Property
Public Property Let MyProperty(ByVal vNewValue As Variant)
MyPropValue = vNewValue
End Property
Private Sub Test()
Dim myclass As New CMyObject
Dim sum As Integer
Dim prop As Integer
' Example of calling a method with CallByName
' equivalent to -- sum = myclass.Multiply(12, 12)
sum = CallByName(myclass, "Multiply", VbMethod, 12, 12)
MsgBox sum
' Example of a property let with CallByName
' equivalent to -- myclass.MyProperty = 5
CallByName myclass, "MyProperty", VbLet, 5
' Example of a property get with CallByName
' equivalent to -- prop = myclass.MyProperty
prop = CallByName(myclass, "MyProperty", VbGet)
MsgBox prop
End Sub
[解决办法]
Option Explicit
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 Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Public Function MyFunction(strDll As String, strFunction As String) As Long
Dim lngRtn As Long, lngFunAddr As Long
lngRtn = LoadLibrary(strDll) 'strDll 是你需要调用的dll
If lngRtn = 0 Then MsgBox "Err.": End ’调用失败就退出了
lngFunAddr = GetProcAddress(lngRtn, strFunction) ’如果成功则
If lngFunAddr = 0 Then MsgBox "Err.": End
MyFunction = lngFunAddr
End Function
Private Sub main()
Dim lngRtn As Long
lngRtn = 0
lngRtn = MyFunction("kernel32.dll", "Beep")
MsgBox "函数地址为:" & " " & cstr(lngRtn)
End Sub
[解决办法]
这里有一个类,根据DLL名、API名调用API函数,前提必须是标准的DLL:
http://topic.csdn.net/u/20110329/10/78f7baa9-d526-4a6f-8e23-6141d9bb76d3.html
[解决办法]
- VB code
Private Function Minimum(ParamArray Vals())Dim n As Integer, MinValOn Error Resume Next MinVal = Vals(0) For n = 1 To UBound(Vals) If Vals(n) < MinVal Then MinVal = Vals(n) Next n Minimum = MinValEnd FunctionPrivate Function Maximum(ParamArray Vals())Dim n As Integer, MaxValOn Error Resume Next MaxVal = Vals(0) For n = 1 To UBound(Vals) If Vals(n) > MaxVal Then MaxVal = Vals(n) Next n Maximum = MaxValEnd Function
[解决办法]
看看我写的这个
http://topic.csdn.net/u/20111007/18/284d0b5a-1945-40d8-aaa7-8d13afba5196.html
[解决办法]
6楼是vb.net,vb6用不了的吧
[解决办法]
你的DLL是STDCALL的么?不是的话压栈方式是不同的
还有,如果是Stdcall的话,要用dumpbin看看DLL中函数真正的名字,可能会变成_max@8之类的
另外如果用我的方法调用你的DLL,应该这样使用
Dim hModule As Long, pProc As Long,p(1) as Long
hModule = LoadLibrary("????????")
pProc = GetProcAddress(hModule, "???????")
dim a as integer ,b as integer
a=111
b=123
If pProc <> 0 Then
p(0)=varptr(a) '第一个参数
p(1)=b '第2个参数
Form1.Print CallProc(pProc, VarPtr(p(1)),2)
Else
MsgBox "err"
End If
FreeLibrary hModule
哦,还有我的调用方法支持的是LONG作为参数的,不知道int为参数行不行
[解决办法]
正好看到的
http://topic.csdn.net/u/20080928/15/287c677b-69e6-432e-9f77-ee58b3fa619f.html