Solved

Callbacks from a DLL to its calling form

Posted on 1998-07-05
7
167 Views
Last Modified: 2010-05-03
Hello,

I have just started writing ActiveX DLLs, and I have the following question:-

I start my project off by showing Form1.  Then it calls an ActiveX DLL called DLL1.  Then I want the DLL to be able to access the contents of a textbox in Form1.

Is this what they call a "callback" problem?  When I attempt to refer to the textbox in Form1 from the DLL, I get an error message saying that it does not recognise the object (Form1) being referred to.

Must I pass Form1 as an argument when I call the procedure inside the DLL?  I am not sure how to phrase this.  I would be very grateful for a code example.

Thanks,

Trimtrom
0
Comment
Question by:trimtrom
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
tomook earned 100 total points
ID: 1464963
In VB5, you can set up a callback only to a regular function in a regular module. A form is technically a class, so no callback is possible directly to a form.

I don't think this is really your question. If you could pass a reference to the form you could do what you want. You can't do this in VB5. It will not let you pass a reference to a "User Defined Type" (at least this is the error you get). Of course, you might be able to do this in VB6, but since I signed a confidentiality agreement I cannot tell you.

So back to callbacks. If you can't wait a few months (wink wink), you will have to subclass the form or control so you can catch the windows messages coming to it. You would then send a Windows message (using the API) and do your own processing. A good reference for this is "Hardcore Visual Basic, Second Edition" by Bruce MxKinney, from Microsoft Press, pages 350-362. It also comes on the MSDN Library Edition. If you are serious about VB programming, this is an excellent investment.

0
 
LVL 6

Expert Comment

by:alamo
ID: 1464964
Sorry,  tomook, I have to disagree with you - we interpret this question differently. I'll give my version of the answer for trimtron.

First of all, no, this is not what is normally called a "callback". A callback is when you call an external API function and give it the address of a sub or function in your program to "call back" . One example of this is the windows API "EnumWindows" function to enumerate windows: you call it with the address of one of your functions, it then calls that function once for every window in the system. ActiveX DLLs written in VB can not call callbacks.

Next, you are correct that you can't pass a form to an ActiveX DLL in VB5, but you *can* pass a control such as the textbox itself. The ActiveX DLL can then access and change the contents of the text control. As an example, in the called ActiveX DLL you could declare the following member of Class1:

Public Function GetSetText(ctrl As Variant) As String
    GetSetText = ctrl.Text
    ctrl.Text = "the text changed!"
End Function

You could then call it from your VB program as:

Dim testObj As Class1
Set testObj = New TestDLL.Class1
t$ = testObj.GetSetText(Form1.Text1)

I hope this helps, good luck!
0
 

Author Comment

by:trimtrom
ID: 1464965
Thanks.  Alamo has the closer answer to what I was really intending.  I will try passing the text control to the DLL for it to access.
Many thanks for both answers.
TrimTrom
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:alamo
ID: 1464966
Please explain trimtron, if you liked my answer, why did you accept tomook's answer as correct?
0
 
LVL 4

Expert Comment

by:tomook
ID: 1464967
Hmm. I sure got the points, but if you liked alamo's answer you should have rejected mine. How do we fix this?
0
 

Author Comment

by:trimtrom
ID: 1464968
Well,
basically your answer involved subclassing and the API, which I want to understand.  So I thought I would accept your advice to invest in the next version of VB, and also the Hardcore VB book.
Where you are coming from, I want to go.  So you got the points!

Trimtrom
0
 
LVL 4

Expert Comment

by:tomook
ID: 1464969
OK, works for me. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now