Making it so only one instance of a program can be opened

Everything else that doesn't fall into one of the other PB categories.
clipper
User
User
Posts: 44
Joined: Fri Aug 29, 2003 7:47 am
Location: Germany

Post by clipper »

Thanks for your Remark Shardik!!

Because my Software uses ProgramParameter(0) to open files, I have to open the files only with the first instance of my Programm before I close the seceond instance!
Do do this, I need the handle of the already running Application.

The remark from http://www.developerfusion.co.uk/show/1716/5/ is important so I have to find another way...


Only for information my Code:
Please compile and call it with a Programm parameter.

Code: Select all

Declare PreviousInstance(title.s)
Declare WindowCallBack(hwnd, uMsg, wParam, lParam)


progtitle.s = "Run Once" 
parameter.s = ProgramParameter(0)
previnsthdl = PreviousInstance(progtitle)
If previnsthdl
   If parameter
      send.COPYDATASTRUCT
      send\cbData = Len(parameter)
      send\lpData = @parameter
      MessageRequester("","Already running! Send '" + parameter + "' to previous instance") 
      SendMessage_(previnsthdl,#WM_COPYDATA,hwnd,@send)
   Else
      MessageRequester("","Already running")   
   EndIf
   ShowWindow_(previnsthdl,#SW_RESTORE)
   SetForegroundWindow_(previnsthdl)
   End
EndIf

If OpenWindow(0,0,0,300,280,progtitle + FormatDate(" %hh:%mm:%ss",Date()),#PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
SetWindowCallback(@WindowCallBack(),0)
Repeat
   Event=WaitWindowEvent() 
Until Event = #PB_Event_CloseWindow
EndIf


Procedure WindowCallBack(hwnd, uMsg, wParam, lParam)
result = #PB_ProcessPureBasicEvents
Select uMsg
   Case #WM_COPYDATA
      *recv.COPYDATASTRUCT = Lparam   
      MessageRequester("Parameter from second Instance ",PeekS(*recv\lpData,*recv\cbData))
EndSelect
ProcedureReturn result
EndProcedure 

Procedure.l PreviousInstance(title.s)
Hwnd = FindWindow_( 0, 0 )
l = Len(title)
While Hwnd <> 0
   txt.s = Space(256)
   GetWindowText_(Hwnd, txt, 256)  
   If Left(txt,l) = title
      ProcedureReturn Hwnd
   EndIf
   Hwnd = GetWindow_(Hwnd, #GW_HWNDNEXT)
Wend
ProcedureReturn #False
EndProcedure
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post by Baldrick »

@Netmaestro,
Definately elegantly nice... :)
Post Reply