Call .DLL then callback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sm9lbWFuYw==?=

    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!
  • Anil Gupte/iCinema.com

    #2
    Re: Call .DLL then callback

    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
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving



    "Joemanc" <jmanci@cl-law.com.donotsp amwrote in message
    news:1FA36E4D-0453-4595-A6C5-C29C37B85241@mi crosoft.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!

    Comment

    • Tom Shelton

      #3
      Re: Call .DLL then callback

      On 2008-06-10, Joemanc <jmanci@cl-law.com.donotsp amwrote:
      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 EnumWindowCallb ackDelegate(ByV al hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

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

      Private Declare Auto Function GetWindowTextLe ngth 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 EnumWindowCallb ackDelegate(Add ressOf EnumWindowsCall backFunction)
      EnumWindows(cb, IntPtr.Zero)
      End Sub

      Private Function EnumWindowsCall backFunction(By Val hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
      Dim bufferLength As Integer = GetWindowTextLe ngth(hWnd)
      If (bufferLength 0) Then
      Dim buffer As New StringBuilder(b ufferLength + 1)
      If GetWindowText(h Wnd, buffer, buffer.Capacity ) 0 Then
      Console.WriteLi ne(buffer.ToStr ing())
      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

      Comment

      • =?Utf-8?B?Sm9lbWFuYw==?=

        #4
        Re: Call .DLL then callback

        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 EnumWindowCallb ackDelegate(Add ressOf 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.Applica tion()
        ns = objOutlook.Sess ion
        Dim MyContactFolder s As Outlook.MAPIFol der =
        (ns.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts))

        Dim MyDefaultProfil e As RegistryKey
        Dim MyKey As String

        Try
        MyDefaultProfil e =
        Registry.Curren tUser.OpenSubKe y("Software\Mic rosoft\Windows
        NT\CurrentVersi on\Windows Messaging Subsystem\Profi les", False)
        MyKey = MyDefaultProfil e.GetValue("Def aultProfile", "")
        ns.Logon("Outlo ok", , False, False)
        Call GetAllContactFo lders()

        MyContacts.Show ()
        MyContacts.Brin gToFront()

        Catch ex As Exception
        MsgBox(Err.Desc ription)
        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 AsyncMethodCall er(AddressOf MyProject.Main)
        threadId = Thread.CurrentT hread.ManagedTh readId()
        caller.Invoke(3 000, 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 <jmanci@cl-law.com.donotsp amwrote:
        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 EnumWindowCallb ackDelegate(ByV al hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
        >
        ' EnumWindows declare
        Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallb ackDelegate, ByVal lParam As IntPtr) As Boolean
        >
        Private Declare Auto Function GetWindowTextLe ngth 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 EnumWindowCallb ackDelegate(Add ressOf EnumWindowsCall backFunction)
        EnumWindows(cb, IntPtr.Zero)
        End Sub
        >
        Private Function EnumWindowsCall backFunction(By Val hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
        Dim bufferLength As Integer = GetWindowTextLe ngth(hWnd)
        If (bufferLength 0) Then
        Dim buffer As New StringBuilder(b ufferLength + 1)
        If GetWindowText(h Wnd, buffer, buffer.Capacity ) 0 Then
        Console.WriteLi ne(buffer.ToStr ing())
        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
        >

        Comment

        • =?Utf-8?B?Sm9lbWFuYw==?=

          #5
          Re: Call .DLL then callback

          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 <jmanci@cl-law.com.donotsp amwrote:
          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 EnumWindowCallb ackDelegate(ByV al hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
          >
          ' EnumWindows declare
          Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallb ackDelegate, ByVal lParam As IntPtr) As Boolean
          >
          Private Declare Auto Function GetWindowTextLe ngth 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 EnumWindowCallb ackDelegate(Add ressOf EnumWindowsCall backFunction)
          EnumWindows(cb, IntPtr.Zero)
          End Sub
          >
          Private Function EnumWindowsCall backFunction(By Val hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
          Dim bufferLength As Integer = GetWindowTextLe ngth(hWnd)
          If (bufferLength 0) Then
          Dim buffer As New StringBuilder(b ufferLength + 1)
          If GetWindowText(h Wnd, buffer, buffer.Capacity ) 0 Then
          Console.WriteLi ne(buffer.ToStr ing())
          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
          >

          Comment

          • Michael Leithold, WWK

            #6
            Re: Call .DLL then callback

            On 10 Jun., 18:05, Joemanc <jma...@cl-law.com.donotsp amwrote:
            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

            Comment

            • =?Utf-8?B?Sm9lbWFuYw==?=

              #7
              Re: Call .DLL then callback

              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.donotsp amwrote:
              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
              >
              >

              Comment

              Working...