Results 1 to 7 of 7

Thread: Make new instance execute methods of the previous

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Location
    Manchester, UK
    Posts
    14

    Make new instance execute methods of the previous

    Dear all,

    I am rephrasing if someone didn't understand my previous question.

    I have an application that has a collection of objects and I want every time that the user tries to load another instance of the application (clicks on the *.exe file) not to be able to do so, but on the contrary to bring on top the open instance and create a new object and add it to the collection of the previous instance.

    In other words I want to terminate the new instance (I know how to do that) and triger to perform actions in the previous instance of the application.

    IS IT POSSIBLE?

    Thnx a lot!

    Chris

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well, a simple (?) way of doing this is by subclassing you're form and save the window handle (in the registry for example) and then let the next session send a user defined message to you're previous session before ending.

    Hmmm... was this understandable?

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    If you know how to subclass a window, I think this might be possible:
    First define a special unique message that is not being used by windows. Then every time a new instance is created, find your earlier existing window handle, and use the SendMessage api to send the unique message to the existing window. Now since you have subclassed your window, you can know when your message has been sent. Then you can perform any action you like within the existing window.

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Okay so I was a bit late

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by amitabh
    Okay so I was a bit late
    Yes, you where

    Here's some code example:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim hWindow As Long
    3.     If App.PrevInstance Then
    4.         hWindow = CLng(GetSetting("TheAppName", "SectionName", "hWnd", "0")
    5.         SendMessage hWindow, UM_NEWSESSION, 0, 0
    6.         End
    7.    Else
    8.        SaveSetting "TheAppName", "SectionName", "hWnd", Me.hwnd
    9.        HookForm Me.hwnd
    10.    End If
    11. End Sub
    Now add the following code to a module:
    VB Code:
    1. Private Declare Function SetWindowLong _
    2.  Lib "user32" Alias "SetWindowLongA" ( _
    3.  ByVal hwnd As Long, _
    4.  ByVal nIndex As Long, _
    5.  ByVal dwNewLong As Long) As Long
    6.  
    7. Private Declare Function CallWindowProc _
    8.  Lib "user32" Alias "CallWindowProcA" ( _
    9.  ByVal lpPrevWndFunc As Long, _
    10.  ByVal hwnd As Long, _
    11.  ByVal Msg As Long, _
    12.  ByVal wParam As Long, _
    13.  ByVal lParam As Long) As Long
    14.  
    15. Private Const GWL_WNDPROC = (-4)
    16. Private Const WM_DESTROY = &H2&
    17.  
    18. Private nHwnd As Long
    19. Private nPrevWndProc As Long
    20. Public Const UM_NEWSESSION = &H40B&
    21.  
    22. Public Sub HookForm(frmHandle As Long)
    23.     nHwnd = frmHandle
    24.     nPrevWndProc = SetWindowLong(nHwnd, GWL_WNDPROC, AddressOf WinProc)
    25. End Sub
    26.  
    27. Public Sub UnhookForm()
    28.     SetWindowLong nHwnd, GWL_WNDPROC, nPrevWndProc
    29. End Sub
    30.  
    31. Public Function WinProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    32.     Dim sFile As String
    33.     Dim nSize As Long
    34.     Select Case uMsg
    35.         Case UM_NEWSESSION
    36.               ' add code to do whatever you like!!!
    37.         Case WM_DESTROY
    38.             'the window has been closed
    39.             UnhookForm
    40.     End Select
    41.     WinProc = CallWindowProc(nPrevWndProc, hw, uMsg, wParam, lParam)
    42. End Function
    Be carefull when you subclass a form though.
    Never use the End statement (I've used it above but that is before the form is subclassed).
    Never click on the End button in the IDE.
    Always use the Unload statement otherwise you'll get a GPF.

    Best regards

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Oh I forgot. You'll need to declare the SendMessage API function as well. Add the following to the General Declaration section of your form:
    VB Code:
    1. Private Declare Function SendMessage _
    2.  Lib "user32" Alias "SendMessageA" ( _
    3.  ByVal hwnd As Long, _
    4.  ByVal wMsg As Long, _
    5.  ByVal wParam As Long, _
    6.  lParam As Any) As Long
    Best regards

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Location
    Manchester, UK
    Posts
    14
    Thanx Joacim, this worked fine!

    Best Regards

    Chrys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width