473,412 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Call .DLL then callback

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!
Jun 27 '08 #1
6 2279
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.

--
Anil Gupte
www.keeninc.net
www.icinema.com
www.wizo.tv
"Joemanc" <jm****@cl-law.com.donotspamwrote in message
news:1F**********************************@microsof t.com...
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!

Jun 27 '08 #2
On 2008-06-10, Joemanc <jm****@cl-law.com.donotspamwrote:
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!
Ok... Here you go a simple example that calls the EnumWindows api:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

' callback delegate
Private Delegate Function EnumWindowCallbackDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

' EnumWindows declare
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallbackDelegate, ByVal lParam As IntPtr) As Boolean

Private Declare Auto Function GetWindowTextLength Lib "user32" (ByVal hWnd As IntPtr) As Integer
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer

Sub Main()
Dim cb As New EnumWindowCallbackDelegate(AddressOf EnumWindowsCallbackFunction)
EnumWindows(cb, IntPtr.Zero)
End Sub

Private Function EnumWindowsCallbackFunction(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim bufferLength As Integer = GetWindowTextLength(hWnd)
If (bufferLength 0) Then
Dim buffer As New StringBuilder(bufferLength + 1)
If GetWindowText(hWnd, buffer, buffer.Capacity) 0 Then
Console.WriteLine(buffer.ToString())
End If
End If
Return True
End Function
End Module

Now, the trick here is that the EnumWindows api takes a function pointer - so,
we declare a delegate type with the same signature - and that is what we pass.
A delegate is, after alll, a object oriented function pointer :)

--
Tom Shelton
Jun 27 '08 #3
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.olFo lderContacts))

Dim MyDefaultProfile As RegistryKey
Dim MyKey As String

Try
MyDefaultProfile =
Registry.CurrentUser.OpenSubKey("Software\Microsof t\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!

"Tom Shelton" wrote:
On 2008-06-10, Joemanc <jm****@cl-law.com.donotspamwrote:
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!

Ok... Here you go a simple example that calls the EnumWindows api:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

' callback delegate
Private Delegate Function EnumWindowCallbackDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

' EnumWindows declare
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallbackDelegate, ByVal lParam As IntPtr) As Boolean

Private Declare Auto Function GetWindowTextLength Lib "user32" (ByVal hWnd As IntPtr) As Integer
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer

Sub Main()
Dim cb As New EnumWindowCallbackDelegate(AddressOf EnumWindowsCallbackFunction)
EnumWindows(cb, IntPtr.Zero)
End Sub

Private Function EnumWindowsCallbackFunction(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim bufferLength As Integer = GetWindowTextLength(hWnd)
If (bufferLength 0) Then
Dim buffer As New StringBuilder(bufferLength + 1)
If GetWindowText(hWnd, buffer, buffer.Capacity) 0 Then
Console.WriteLine(buffer.ToString())
End If
End If
Return True
End Function
End Module

Now, the trick here is that the EnumWindows api takes a function pointer - so,
we declare a delegate type with the same signature - and that is what we pass.
A delegate is, after alll, a object oriented function pointer :)

--
Tom Shelton
Jun 27 '08 #4
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!

"Tom Shelton" wrote:
On 2008-06-10, Joemanc <jm****@cl-law.com.donotspamwrote:
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!

Ok... Here you go a simple example that calls the EnumWindows api:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

' callback delegate
Private Delegate Function EnumWindowCallbackDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

' EnumWindows declare
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallbackDelegate, ByVal lParam As IntPtr) As Boolean

Private Declare Auto Function GetWindowTextLength Lib "user32" (ByVal hWnd As IntPtr) As Integer
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer

Sub Main()
Dim cb As New EnumWindowCallbackDelegate(AddressOf EnumWindowsCallbackFunction)
EnumWindows(cb, IntPtr.Zero)
End Sub

Private Function EnumWindowsCallbackFunction(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim bufferLength As Integer = GetWindowTextLength(hWnd)
If (bufferLength 0) Then
Dim buffer As New StringBuilder(bufferLength + 1)
If GetWindowText(hWnd, buffer, buffer.Capacity) 0 Then
Console.WriteLine(buffer.ToString())
End If
End If
Return True
End Function
End Module

Now, the trick here is that the EnumWindows api takes a function pointer - so,
we declare a delegate type with the same signature - and that is what we pass.
A delegate is, after alll, a object oriented function pointer :)

--
Tom Shelton
Jun 27 '08 #5
On 10 Jun., 18:05, Joemanc <jma...@cl-law.com.donotspamwrote:
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

Jun 27 '08 #6
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.

"Michael Leithold, WWK" wrote:
On 10 Jun., 18:05, Joemanc <jma...@cl-law.com.donotspamwrote:
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

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Malcolm Cook | last post by:
Hi, I've created and installed a custom UDF to populate my combobox, and have defined it per :...
2
by: Gerda | last post by:
Hi! I've implemented many times an asynchronous call of a method with a call backfunction successfully. But to implement this with VB.NET is not so successfully. I can implement all events...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
0
by: Richard B | last post by:
I have to call a third-party program from mine. The third-party program expects a string parameter(that will be converted to an integer) that is a handle to a callback function in my program. The...
2
by: archana | last post by:
Hi all, I am processing asynchronous web request with setting timeout using RegisterWaitForSingleObject. On beginwebrequest i am sending address of one callback which i want to execute when...
2
by: MyCrystalGift | last post by:
Hi, I have an old C++ GUI Application CPPAPP.exe that calls a C DLL library RULE.DLL through a C++ class wrapper LoadRule.CPP. Now I need to call the C DLL RULE.DLL from C# GUI application...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
11
by: sameer.oak | last post by:
I am looking for some comprehensive tutorials on how to write call back functions in C. Can anyone help me? - sameer oak.
0
by: tomb | last post by:
Can anybody help me by checking if I have correct call signatures? In my C# Windows Form project (VS2008) I am using dll which has been written for Visual C++ 6.0: This function code is used to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.