?
Solved

DLL call back function in caller

Posted on 2003-02-22
9
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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