如何调用另一个DLL中的函数? (Win7的/ VB6.0) [英] How to call a function which is in another DLL ? (Win7/VB6.0)
问题描述
你好,
我是Mikito Matsui。
我有关于DLL的问题。
我想从另一个DLL中调用DLL中的函数(假设这是foo)(假设这是DLL_A)(让我们说这是DLL_B)。你能告诉我怎么做吗?
(Windows 7专业版/ Microsoft Visual Basic 6.0)
我可以通过声明函数调用DLL_A中的foo,如下所示。
' 标准模块
声明 功能 foo Lib Dll_A.dll _
( ByRef val1 作为任何, ByVal val2 作为 长,_
ByVal val3 作为 长, ByVal val4 作为 长,_
ByRef val5 As Any) As Long
' 表格
return = foo(val1,val2,val3, val5,val6)
然后,我使用Visual Basic 6的ActiveX Dll制作了Dll_B。(我使用下面的代码作为参考。) DLL_B如下。
' / ** ************************************************** ***************
' / * DLL_B
' / *************** ************************************************** **
私有 功能栏(val1 作为 长,val2 作为 长,val3 作为 长,val4 作为 长,val5 As L ong)
On 错误 继续 下一步
' 我们我将调用API函数,而不是声明它!
Dim lb As 长,pa 作为 长
' 将'user32'映射到地址调用进程的空间。
lb = LoadLibrary( Dll_A)
' 检索'SetWindowTextA'的地址
pa = GetProcAddress(lb, foo)
' 调用SetWindowTextA函数
CallWindowProc pa, ByVal val1, ByVal val2, ByVal val3, ByVal val4, ByVal val5
' 取消映射库的地址
FreeLibrary lb
结束 功能
我用作参考的代码如下:
' / ************* ************************************************** ****
' / *我用作参考的代码。
' / ******************* **********
创建新项目并添加此代码到 Form1
私有 声明 功能 FreeLibrary Lib kernel32 ( ByVal hLibModule 作为 长) As 长
私有 声明 函数 LoadLibrary Lib kernel32 别名 LoadLibraryA( ByVal lpLibFileName As String )作为 长
私有 声明 功能 GetProcAddress Lib < span class =code-string> kernel32( ByVal hModule As 长, ByVal lpProcName 作为 字符串)作为 长
私有 声明 功能 CallWindowProc Lib user32 别名 CallWindowProcA( ByVal lpPrevWndFunc As 长, ByVal hWnd 作为 长, ByVal 消息 As 任何, ByVal wParam As Any, ByVal lParam As Any) 作为 长
私有 Sub Form_Load()
开启 错误 恢复 下一步
' 我们将调用API函数,而不是声明它!
Dim lb 作为 长,pa 作为 长
' 将'user32'映射到调用进程的地址空间。
lb = LoadLibrary( user32)
' 检索地址'SetWindowTextA'
pa = GetProcAddress(lb, SetWindowTextA)
' 调用SetWindowTextA函数
CallWindowProc pa,我 .hWnd, Hello!, ByVal 0&, ByVal 0&
' 取消映射库的地址
FreeLibrary lb
结束 Sub
我称之为 bar通过使用声明函数的方式与DLL_A相同,如下所示。
' 标准模块
声明 功能 bar < span class =code-keyword> Lib Dll_B.dll _
( ByRef val1 As Any, ByVal val2 作为 长,_
ByVal val3 作为 长, ByVal val4 作为 长,_
ByRef val5 As Any) As 长
' 表格
return = bar(val1,val2,val3,val4,val5)
但我执行此代码,显示以下错误。
运行时错误'453'在DLL_B中找不到DLL入口点bar
我试图按如下方式调用bar,但也会显示错误。
Dim objB 作为 对象
set objB = CreateObject( Project1.Class1)
call objB.bar()
set objB = 什么
我在注册表中注册DLL_B后,仍会显示错误。
请告诉我解决方案策略或给我一个线索。提前谢谢大家。
祝你好运
Mikito Matsui
你不能用这种风格调用其他ActiveX Dll中的函数
,除非你要调用的dll是工业标准的dll <这些dll可以由C / Cpp或PowerBasic制作
在使用ActiveX Dll之前,你必须注册它。像批处理代码一样:
regsvr32 xxx.dll
并在VB6 IDE中引用它作为参考
考虑以下示例
创建一个类
代码:
公共类MyFunctions
公共函数AddMyValues (ByVal Value1 As Double,ByVal Value2 As Double)
Dim Result As Double
Result = Value1 + Value2
返回结果
结束函数
结束类
点击Build&创建DLL文件
使用表格创建一个新项目
代码:
Imports PDUNZDLL
Public 类 Form1
私有 Sub Button1_Click(< span class =code-keyword> ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles Button1。点击
Dim 添加作为 新 PDUNZDLL.MyFunctions
Label1.Text = Add.AddMyValues( CDbl (TextBox1.Text), CDbl (TextBox2.Text))。ToString
结束 Sub
结束 类跨度>
Hi there,
I'm Mikito Matsui.
I have a question about the DLL.
I'd like to call a function (let's say this is "foo") in a DLL (let's say this is "DLL_A") from another DLL (let's say this is "DLL_B"). Would you tell me how to do this ?
(Windows 7 Professional / Microsoft Visual Basic 6.0)
I was able to call the "foo" in "DLL_A" by using declare function as follows.
'standard module
Declare Function foo Lib "Dll_A.dll" _
(ByRef val1 As Any, ByVal val2 As Long, _
ByVal val3 As Long, ByVal val4 As Long, _
ByRef val5 As Any) As Long
'form
return = foo(val1, val2, val3, val5, val6)
Then, I made "Dll_B" by using ActiveX Dll of Visual Basic 6. (I used the code below as reference.) "DLL_B" is as follows.
'/*******************************************************************
'/* DLL_B
'/*******************************************************************
Private Function bar(val1 As Long, val2 As Long, val3 As Long, val4 As Long, val5 As L ong)
On Error Resume Next
'We're going to call an API-function, without declaring it!
Dim lb As Long, pa As Long
'map 'user32' into the address space of the calling process.
lb = LoadLibrary("Dll_A")
'retrieve the address of 'SetWindowTextA'
pa = GetProcAddress(lb, "foo")
'Call the SetWindowTextA-function
CallWindowProc pa, ByVal val1, ByVal val2, ByVal val3, ByVal val4, ByVal val5
'unmap the library's address
FreeLibrary lb
End Function
The code which I used as reference is as follows.
'/*******************************************************************
'/* The code which I used as reference.
'/*******************************************************************
Create a New project And add this code To Form1
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
'We're going to call an API-function, without declaring it!
Dim lb As Long, pa As Long
'map 'user32' into the address space of the calling process.
lb = LoadLibrary("user32")
'retrieve the address of 'SetWindowTextA'
pa = GetProcAddress(lb, "SetWindowTextA")
'Call the SetWindowTextA-function
CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0&
'unmap the library's address
FreeLibrary lb
End Sub
I called the "bar" by using declare function in the same way as "DLL_A" as follows.
'standard module
Declare Function bar Lib "Dll_B.dll" _
(ByRef val1 As Any, ByVal val2 As Long, _
ByVal val3 As Long, ByVal val4 As Long, _
ByRef val5 As Any) As Long
'form
return = bar(val1, val2, val3, val4, val5)
But I execute this code, the following error displays.
Run-time error '453' Can't find DLL entry point "bar" in "DLL_B"
I tried to call "bar" as follows, but the error displays as well.
Dim objB As Object
set objB = CreateObject("Project1.Class1")
call objB.bar()
set objB = Nothing
After I registered the "DLL_B" with the registry, the error still displays.
Please tell me the solution strategy or give me a clue. Thank you all in advance.
Best regards
Mikito Matsui
You can not invoke the function in other ActiveX Dll by this style
unless the dll you will invoke is an industrial standard dll
these dlls can be made by C/Cpp or PowerBasic
Before using ActiveX Dll, you must to regist it. Like the batch code:
regsvr32 xxx.dll
and quote it in your VB6 IDE for reference
Consider following example
Create a class
code :
Public Class MyFunctions Public Function AddMyValues(ByVal Value1 As Double, ByVal Value2 As Double) Dim Result As Double Result = Value1 + Value2 Return Result End Function End Class
Click on Build & create DLL file
Create a new project using Form
code :
Imports PDUNZDLL Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Add As New PDUNZDLL.MyFunctions Label1.Text = Add.AddMyValues(CDbl(TextBox1.Text), CDbl(TextBox2.Text)).ToString End Sub End Class
这篇关于如何调用另一个DLL中的函数? (Win7的/ VB6.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!