Page 1 of 1

Single instance

Posted: Fri Jun 01, 2007 4:13 pm
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?

Re: Single instance

Posted: Fri Jun 01, 2007 4:17 pm
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?

Posted: Fri Jun 01, 2007 4:48 pm
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.

Posted: Fri Jun 01, 2007 4:54 pm
by ts-soft
here in german-forum is a example by me:
http://www.purebasic.fr/german/viewtopic.php?t=13014
registerwindowmessage is simple

Posted: Fri Jun 01, 2007 6:05 pm
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.

Posted: Fri Jun 01, 2007 7:07 pm
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