|
-
Nov 7th, 2001, 09:39 AM
#1
Thread Starter
New Member
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
-
Nov 7th, 2001, 09:50 AM
#2
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?
-
Nov 7th, 2001, 09:52 AM
#3
PowerPoster
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.
-
Nov 7th, 2001, 09:54 AM
#4
PowerPoster
Okay so I was a bit late
-
Nov 7th, 2001, 10:12 AM
#5
Originally posted by amitabh
Okay so I was a bit late
Yes, you where 
Here's some code example:
VB Code:
Private Sub Form_Load()
Dim hWindow As Long
If App.PrevInstance Then
hWindow = CLng(GetSetting("TheAppName", "SectionName", "hWnd", "0")
SendMessage hWindow, UM_NEWSESSION, 0, 0
End
Else
SaveSetting "TheAppName", "SectionName", "hWnd", Me.hwnd
HookForm Me.hwnd
End If
End Sub
Now add the following code to a module:
VB Code:
Private Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc _
Lib "user32" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_DESTROY = &H2&
Private nHwnd As Long
Private nPrevWndProc As Long
Public Const UM_NEWSESSION = &H40B&
Public Sub HookForm(frmHandle As Long)
nHwnd = frmHandle
nPrevWndProc = SetWindowLong(nHwnd, GWL_WNDPROC, AddressOf WinProc)
End Sub
Public Sub UnhookForm()
SetWindowLong nHwnd, GWL_WNDPROC, nPrevWndProc
End Sub
Public Function WinProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim sFile As String
Dim nSize As Long
Select Case uMsg
Case UM_NEWSESSION
' add code to do whatever you like!!!
Case WM_DESTROY
'the window has been closed
UnhookForm
End Select
WinProc = CallWindowProc(nPrevWndProc, hw, uMsg, wParam, lParam)
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
-
Nov 7th, 2001, 10:17 AM
#6
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:
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Best regards
-
Nov 8th, 2001, 04:46 AM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|