?
Solved

getting info from a dll

Posted on 2003-02-20
11
Medium Priority
?
197 Views
Last Modified: 2010-05-01
hi experts,
I'm trying to code a dll using visual basic and my problem is as follows ...
This dll has a class module with a public procedure (the one used to pass in parameters) and a form. The values are passed on to this form in order to show them up in text boxes. On this form I have a command button (called 'apply') and I want to use it to send back the info in these text boxes (in case the info in them is modified by the user) to the application that called the dll and so be able to take in the new info coming from the text boxes. As you guys can see, the user could click on the 'apply' button at any time and the dll should be prepared to send the new info back to the application that called it. And more, how can the initial application know that someone click on the 'apply' button, so being ready to do 'something' to get the new values ...
I've tried with events but can't get it working.
I've been checking on different sites for a bit of help but all the examples are with public functions that use byref parameters to take values in and give back the results after all the business logic, but everything is whithin the same call (synchronous calls) and don't fit what I'm looking for.

Your help would be appreciated (plus a beer! :-) )

Ta

Toni
0
Comment
Question by:AntoniL
[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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:speke
ID: 7986769

In you dll class use something like:

Public Event ReturnData(Text1, Text2)

Public Sub ApplyClicked()

 RaiseEvent ReturnData("a", "b")

End Sub

In your form use:

Private Sub Apply_Click()
 MyClass.ApplyClicked
end sub

Then in your calling application use:

Dim Withevents MyClass As MyDLL.MyClass

Private Sub MyClass_ReturnData(Text1 As Variant, Text2 As Variant)

    Debug.Print Text1 & " " & Text2

End Sub
0
 
LVL 3

Accepted Solution

by:
kokoloko earned 800 total points
ID: 7987401
Here's a working example without using events:

1) Open a VBProject with a Form (Project1,Form1). Place a command button and a textbox on the form.
Paste the following code in Form1:

Private prj2dll As Project2.Class1

Public Sub Apply(data As String)
  Text1.Text = data
End Sub

Private Sub Command1_Click()
  Set prj2dll = New Project2.Class1
  prj2dll.InitRef Me
  prj2dll.ShowForm
End Sub

Private Sub Form_Unload(Cancel As Integer)
  prj2dll.InitRef Nothing
  Set prj2dll = Nothing
End Sub

2) Add another project, this time an ActiveX DLL (Project2, Class1) with an additional form (Form2).
Place a CommandButton and a textbox on Form2.
Paste the following code in Class1:

Dim parentObj As Object
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

Public Sub InitRef(ref As Object)
  If Not ref Is Nothing Then
    CopyMemory parentObj, ObjPtr(ref), 4
    Set Form2.parentObj = parentObj
  Else
    Set Form2.parentObj = Nothing
    CopyMemory parentObj, 0&, 4
  End If

End Sub

Public Sub ShowForm()
   Form2.Show
End Sub


'---------
'Paste the following code in Form2:

Public parentObj As Object

Private Sub Command1_Click()
  parentObj.Apply Text1.Text
End Sub

Finally, reference project2 (DLL) from project1, and you're done.
Run the project, and press the command button. The DLL's form (Form2) will appear. Change the text, and click the button (Apply)... Voilá, you get the value back to the application that called the DLL.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 7987488
If you make the data propertys of the class as folllows

Public Property Get MyData() as Text
   MyData = Text1.Text
End Property

Add an event declaration to the the class

Public Event ApplyChanges(ByRef bCancelled as Boolean)

In the Command button Apply click evemt enter the following

Private Sub cmdBtnApply_Click()
Dim bCancel as Boolean
   RaiseEvent ApplyChanges bCancel
   DoEvents
End Sub

The bCancel is not used but could be used to indicate the values were not accepted.

Where ever there is an instancce if the class make sure it is declared WithEvents as follows.

Private WithEvents m_MyClass As MyClass

You should now be able to use the events as follows

Private Sub MyClass_ApplyChanges(ByRef bCancelled as Boolean)
   'm_MyClass.MyData
End Sub

'm_MyClass.MyData Now gives you access to the updated data.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:AntoniL
ID: 7987724
Hi Speke,
thanks for your help, but the situation is ...
I need to send some values to this dll, for those values to be seen on the dll's form (a couple of text boxes). Then, the user has the option to modify them and click on the 'apply' button so those new values can be send back to the initial application that loaded the dll ... is not exactly what your example is about (similar though!).
A bit of more help and 200 points will go for you!

Cheers

Toni
0
 

Author Comment

by:AntoniL
ID: 7987807
Hi Speke,
thanks for your help, but the situation is ...
I need to send some values to this dll, for those values to be seen on the dll's form (a couple of text boxes). Then, the user has the option to modify them and click on the 'apply' button so those new values can be send back to the initial application that loaded the dll ... is not exactly what your example is about (similar though!).
A bit of more help and 200 points will go for you!

Cheers

Toni
0
 

Author Comment

by:AntoniL
ID: 7987815
Hi Speke,
thanks for your help, but the situation is ...
I need to send some values to this dll, for those values to be seen on the dll's form (a couple of text boxes). Then, the user has the option to modify them and click on the 'apply' button so those new values can be send back to the initial application that loaded the dll ... is not exactly what your example is about (similar though!).
A bit of more help and 200 points will go for you!

Cheers

Toni
0
 
LVL 3

Expert Comment

by:kokoloko
ID: 7989958
Hey, JohnMcCann, please post a comment on
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20499347.html 
so I can assign you the points I promised you for your help in my prev Q.
0
 
LVL 3

Expert Comment

by:kokoloko
ID: 7989967
AntoniL:
why don't you give my code a try... you said you didn't wanna use events...
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 7992740
Just a thought if you pass the initial values ByRef, when the user click apply you could change these values.  I do not think that is what you want though.

I do believe the events mechanism is the answer to this problem.  If you are having prpoblems when using events make sure

a) You call DoEvents after calling RaiseEvent.
b) You are declaring your variables WithEvents.

Using the approach I suggested above the calling procedure has the oportunity to examine the values to be applyed and indicate wether or not changes have been accepted.

A little examnple, Open Vb create an ActiveX dll goto the file menu and select add project select Standard exe.  Select the sytandard exe and click right mouse then select set as startup.   Go the ActiveX dll project and add a form called Form1.  Make sure the Exe project references the ActiveX project Menu->Project->References.

'In the class (ActiveX.dll) add the following code.

Option Explicit

Private WithEvents m_Form As Form1
Private m_MyData As String

Public Event ApplyChanges(ByRef bCancelled As Boolean)

Private Sub m_Form_ApplyChanges(bCancelled As Boolean)
   m_MyData = m_Form.Text1.Text
   RaiseEvent ApplyChanges(bCancelled)
   DoEvents
End Sub

Public Property Get MyData() As String
  MyData = m_MyData
End Property

Public Property Let MyData(New_MyData As String)
  m_MyData = New_MyData
End Property

Public Sub ShowForm()
   Set m_Form = New Form1
   
   With m_Form
      .Text1.Text = m_MyData
      .Show vbModal
   End With
   'This will will only ba caalled when the form is no longer Modal or Visible.
   Set m_Form = Nothing
End Sub

'On the form (ActiveX.dll)
'Add a TextBox (Text1) and a Command Button (cmdBtnApply)
'Add this code

Public Event ApplyChanges(ByRef bCancelled As Boolean)

Private Sub cmdBtnApply_Click()
Dim bCancel As Boolean

   RaiseEvent ApplyChanges(bCancel)
   DoEvents
   If Not bCancel Then Me.Hide
End Sub

'In the calling form (Your.exe)
'Add a TextBox (Text1) and a Command Button (cmdBtnTest)
'Add this code

Option Explicit

Dim WithEvents obj As Class1

Private Sub cmdBtnTest_Click()

   Set obj = New Class1
   
   With obj
      .MyData = Text1.Text
      .ShowForm
   End With
End Sub

Private Sub obj_ApplyChanges(bCancelled As Boolean)
   Text1.Text = obj.MyData
End Sub

'End Code

To use this example

1) Press run and your test form should appear.
2) Enter some Text in the textbox.
3) Press the command button.
4) You should now see the form with cmdBtnApply on from your dll with a textbox containing the text you entered.
5) Enter some new text and click apply.
6) The form will disapear and the textbox in your exe will now have the text in.

0
 

Author Comment

by:AntoniL
ID: 8002243
Sorry guys for this late comment!. I tried out the three solutions you gave me lads and the one from kokoloko turned out to be the one I was looking for. I just changed a couple of things in my code and went smoothly.
Thanks experts!!, thanks kokoloko!!

I owe you a beer ;-)

Toni
0
 
LVL 3

Expert Comment

by:kokoloko
ID: 8008512
Glad I could help... you still owe me the beer thou
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 (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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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

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