Here is a modification of the above code!
It let you do single instance running just like any "good" application!
Run the program, minimize the window. Then run it again!
If all goes well the program won't start again,
but instead message the old instance and restore it and bring it to the foreground!
Only tested on XP Pro, but should behave in a similar way on all windows versions. not entirely sure if W95 behave exactly the same or not, test to be sure!
Code:
ERROR_ALREADY_EXISTS = 183
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"
Procedure MyWindowCallback(WindowID, message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If message = WM_ACTIVATEOLDINST
ShowWindow_(WindowID,#SW_RESTORE)
SetForegroundWindow_(WindowID)
EndIf
ProcedureReturn Result
EndProcedure
hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = ERROR_ALREADY_EXISTS
SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
End
EndIf
hwnd=OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget,"MyLittleTestApp ")
If hwnd
SetWindowCallback(@MyWindowCallback())
Repeat
ev=WaitWindowEvent()
If showoldwindow
EndIf
Until ev=#PB_EventCloseWindow
EndIf
If hMutex <> 0
CloseHandle_(hMutex)
EndIf
End
I'll leave it up to you as a coding exercise to find out how to send a message to the old (first) app with a filepath. (like many editors do)
Shouldn't be hard, the messaging "example" is already here so. Hehe!