?
Solved

DLL call back function in caller

Posted on 2003-02-22
9
Medium Priority
?
191 Views
Last Modified: 2010-05-01
I have a Form that instantiates a DLL and calls a function in it. I need the DLL to call a function called "display" in the Form at a random point in time. I tried passing in the reference "Me" into the DLL so that it could call a function in the form, but that threw an error. I'm not sure how to do this. Any ideas?
0
Comment
Question by:verasen
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8001092
I asume the error is indicating method display of form does not exist.

In the dll

Where you Have the method declaration change the type from form to object

e.g.

Public MyFunc(ByRef obj as Object)

If ythis is not the case be a bit more specific and I will try and help

0
 

Expert Comment

by:StonedRaider
ID: 8001111
Is it an Activex DLL created with VB ?
or another language like C/C++ Delphi ASM.

if it is a C/C++ Delphi ASM DLL, you has to pass a pointer to you function to the Dll.
Like:
AddressOf MyFunction = Pointer to MyFunction

If it is a VB DLL you can do that I think but you can Send a Message to your APP, when the APP receives the Message you call the function

0
 

Author Comment

by:verasen
ID: 8001729
Thanks for your reply but that didn't work. I don't think it has to do with me declaring the variable as an object. I think I have to use function pointers using AddressOf like Stoned said, but I'm not sure how to implement the callback. If I get the address of the function "display" using addressOf, I can pass that into the DLL, but how can I can I signal back to the main application that I have information to send back from within the DLL?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:StonedRaider
ID: 8002050
I meant if it is a VB DLL then you can't assign the pointer to a function.
If it is a C/C++, Delphi, ASM DLL you will can, instead of that why you dont send a message to your application ?
when the application detects the message then you call the funcion, it is almost the same.
0
 

Author Comment

by:verasen
ID: 8002119
How do you send a message back to the application?
0
 
LVL 1

Accepted Solution

by:
SilentRage earned 400 total points
ID: 8003250
The easiest way to send messages from one application to another is using the SendMessage API.

I'll spare ya a lengthy tutorial, and go straight to some example code.  Load up a new VB project and enter the below code under Form1.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_RBUTTONDOWN = &H204
Private Sub Form_Load()
    SendMessage Me.hwnd, WM_RBUTTONDOWN, ByVal 0, ByVal &H00010001
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
        Debug.Print X / Screen.TwipsPerPixelX, Y / Screen.TwipsPerPixelY
    End If
End Sub

---

Explanation of what's going on.  SendMessage is sending a message to the handle of Form1.  The message is of type WM_RBUTTONDOWN.  In other words, we're simulating a right-click on your window.  The 3rd parameter is left at 0, and then we're including a number for the 4th parameter.  This number is in hex.  0001 0001.  The high 2 bytes is of value 1.  The low 2 bytes is of value 1.

Then VB recieves the message in the Form1 message queue.  Then it calls the Form_MouseDown event.  Button is set to 2 since this is a right click.  X is set to the first 2 bytes of the 4th argument to SendMessage.  Y is set to the last 2 bytes of the 4th argument to SendMessage.  Since the form ScaleMode is in Twips, VB converted X and Y to twips values.  You have 2 choices.  Either set the form ScaleMode to pixels - or convert the values to pixels yourself.  I showed you the example of this.

You may of course send whatever values you want to X and Y to mean whatever you want.  If you combine X and Y together into a 4 byte number, you can recieve a pointer to a string, and then read that string - but that takes some more VB hacks which I just love to do.  :P  But if you're into VB hacks, then you should instead hook the message queue and get a lot more flexibility in this message sending/recieving setup.

I could go on forever on this.  Just remember that you may send whatever message you want to the window.  Most standard messages are handled by Visual Basic's various events.  Use my method illustrated above for convenience in sending simple message code values like:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
        X = X / Screen.TwipsPerPixelX
        Y = Y / Screen.TwipsPerPixelY

        Select Case X
        Case 1: 'Operation Successful
        Case 2: 'Error code 2 - no memory
        Case 3: 'Error code 3 - bad programmer
        Case 4: 'Error code 4 - Windows did it!
        End Select
    End If
End Sub

Just a little something like that.  :)
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8003278
oh, and btw.  If you are trying to call a function from a C dll (or any language with powerful pointer handling capabilities), then passing a function pointer is the easiest way to go.  It just takes some tricks on the dll end to convert that function pointer to a callable function name.  I could give ya C example code for that as well.
0
 

Expert Comment

by:StonedRaider
ID: 8005018
you can send a message with SendMessage() API function.

if you are going to send a custom message, you will have to subclass the form to catch the message.
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8005123
no.  I just demonstrated how it would not be necessary to subclass the form.  Read my entire post.  Two quotes:

"Most standard messages are handled by Visual Basic's various events."

"But if you're into VB hacks, then you should instead hook the message queue and get a lot more flexibility in this message sending/recieving setup."

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question