Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
VB
'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.
VB
'/*******************************************************************
'/*     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.
VB
'/*******************************************************************
'/*     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.
VB
'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.
VB
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
Posted
Comments
Maciej Los 17-Jul-14 1:50am    

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
 
Share this answer
 
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 :
VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900