Call .DLL then callback

J

Joemanc

Hi,
I'm trying to figure out the Callback method. I have a .dll that I call from
a sub in my unmanaged code/calling program. That works just fine. But I'd
like to have the .dll finish doing it's thing before returning to my calling
program. Right now, with the code I've been testing with, the calling program
goes to the .dll and then returns back to the calling program without letting
the .dll do it's thing. Just having a hard time figuring out the callback
routine. If someone could lead me to a good example, that would be great.
Thanks!
 
A

Anil Gupte/iCinema.com

I am not real familiar with it, but I think DoEvents() might be your answer.

You could just put your program in some kind of loop controlled by a timer
and wait till the DLL sets some variable signifying it is done.
 
J

Joemanc

Tom,
I had found a similar sample online, but yours does not work either. But I
feel as if I'm close.

This is the code I'm now using in my calling program:

Sub Outlook_Startup
Dim cb As New EnumWindowCallbackDelegate(AddressOf MyProject.Main)
EnumWindows(cb, IntPtr.Zero)
Call ReturnContact 'code to process after the .dll is finished running
End Sub

This is the code called in the .dll

Public Function Main(ByVal hwnd As Integer, ByVal lParam As Integer)
As Boolean

objOutlook = New Outlook.Application()
ns = objOutlook.Session
Dim MyContactFolders As Outlook.MAPIFolder =
(ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts))

Dim MyDefaultProfile As RegistryKey
Dim MyKey As String

Try
MyDefaultProfile =
Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows
NT\CurrentVersion\Windows Messaging Subsystem\Profiles", False)
MyKey = MyDefaultProfile.GetValue("DefaultProfile", "")
ns.Logon("Outlook", , False, False)
Call GetAllContactFolders()

MyContacts.Show()
MyContacts.BringToFront()

Catch ex As Exception
MsgBox(Err.Description)
End Try

End Function

The behavior I'm getting is different from the code you sent me that I
tested. The code execution runs through the .dll and then returns to the
calling program and then tries to run that 'ReturnCode' sub. It never stops
to allow the .dll to do it's job.

I was able to call the Main sub in the .dll using this code:

Dim caller As New AsyncMethodCaller(AddressOf MyProject.Main)
threadId = Thread.CurrentThread.ManagedThreadId()
caller.Invoke(3000, threadId)

This code was giving full control to the .dll. The only problem is, I'm not
able to return back to my .dll.
Do you have any ideas on what I may be doing wrong? Thanks!
 
J

Joemanc

ok, I think I got it! I'm displaying my forms in the .dll with .show
instead of .showdialog. I changed the calls to .showdialog and that seems to
be doing the trick. The code was just showing the form and then continuing
on. Looks like showdialog is helping to keep the code in the .dll. Very
tricky stuff!
 
M

Michael Leithold, WWK

I'm trying to figure out the Callback method. I have a .dll that I call from
a sub in my unmanaged code/calling program. That works just fine. But I'd
like to have the .dll finish doing it's thing before returning to my calling
program. Right now, with the code I've been testing with, the calling program
goes to the .dll and then returns back to the calling program without letting
the .dll do it's thing. Just having a hard time figuring out the callback
routine. If someone could lead me to a good example, that would be great.

Maybe you could use an event? Raise an event when you dll has
finished. Then proceed in your calling program.

Michael
 
J

Joemanc

I actually ended up going all the way back to my original code that I had
posted below, with the caller.invoke method.
Between that and changing my calls in the .dll for the user forms from .Show
to .ShowDialog, that is working!
Thanks for all of your help and suggestions. Hopefully this thread can help
out someone with a similar problem in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top