Single instance

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Single instance

Post by blueznl »

Yes. I know. Search the forum. And I did.

And found some code, but comehow they all look a little... kludgy :D

Here's the thing: several programs (editors, for example) when started with a parameter will not create a second instance, but will pass the parameter on to the first instance.

I've seen sendmessage constructions, which may work, though I'm somewhat puzzled how to obtain the windows handle of a previous instance (and findwindow seems a bit wrong to solve this, unless I create a hidden window with an absurd name... yes, I know several apps do it that way).

Another approach would be to do a mapped file, which will work, but again it seems a little overkill.

So, what else is there? Or should I simply jump in and live with a sendmessage / callback construction?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Re: Single instance

Post by thefool »

blueznl wrote:I'm somewhat puzzled how to obtain the windows handle of a previous instance
You know what the name of the exe is. Now, find your own process id. List all processes with the name of your exe, and find the one that does not have your process id but which has the same location on the disk

edit: is there any way to figure out what process set the mutex?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I did something like this is my media player example:
http://www.jlc-software.com/programs/jmp_source.zip

Instead of opening another instance it sends a parameter to the first one and closes itself.
I like logic, hence I dislike humans but love computers.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

here in german-forum is a example by me:
http://www.purebasic.fr/german/viewtopic.php?t=13014
registerwindowmessage is simple
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Joakim Christiansen wrote:I did something like this is my media player example:
http://www.jlc-software.com/programs/jmp_source.zip

Instead of opening another instance it sends a parameter to the first one and closes itself.
You use findwindow which he would like to avoid :)
here in german-forum is a example by me:
He allready knows how to do that.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Don't forget you can always go to your uncle Berikco:

http://www.purebasic.fr/english/viewtop ... 97&start=3

Code updated to PB 4:

Code: Select all

Global WM_ACTIVATEOLDINST 
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB") 
MutexName$ = "mylittletestapp" 

Procedure MyWindowCallback(WindowID, Message, wParam, lParam)  
  Result = #PB_ProcessPureBasicEvents  
  If message = WM_ACTIVATEOLDINST 
    CloseHandle_(hMutex) 
    End 
  EndIf 
  ProcedureReturn Result  
EndProcedure  

hMutex = CreateMutex_(0, 0, @MutexName$) 
If GetLastError_() = #ERROR_ALREADY_EXISTS 
  SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0) 
  Delay(100) 
EndIf 


If OpenWindow(0,100,150,450,200,"MyLittleTestApp " + Str(Random(500)), #PB_Window_SystemMenu) 
  SetWindowCallback(@MyWindowCallback()) 
  Repeat 
    ev=WaitWindowEvent() 
  Until ev=#PB_Event_CloseWindow 
EndIf 

If hMutex <> 0 
 CloseHandle_(hMutex) 
EndIf 

End
BERESHEIT
Post Reply