Inhibiting duplicate apps in Windows?

Chris Liechti cliechti at gmx.net
Thu Jun 20 17:34:06 EDT 2002


"Edward K. Ream" <edream at tds.net> wrote in news:3D11F9E8.DFFFC8E4 at tds.net:

> Hi All,
> 
> I have a pure Python app called Leo.  In Windows, I can open Leo by
> double-clicking a .leo file.  I would like to do more: if Leo is open
> and I double-click a .leo file, I would like the _already open_ copy of
> Leo to open the second .leo file, rather than (permanently) opening a
> second copy of Leo.
> 
> What I would like is for the second copy of Leo to detect that another
> copy of leo.py is already running, send a message to the first copy and
> then exit. I wonder if anyone knows how this might be done?
> 
> The C++ version of Leo works as follows:
> 
> 1. It inhibits multiple copies of leo.py from running using a mutex.  I
> think this part is easy because the Python library supports mutexes. 

but the python mutexes do not work across processes.
you can also use FindWindow of win32all to search a process/window, so you 
don't need a mutex.
 
> 2. It sends a message from the second copy of leo.py (the copy that will
> shortly close), instructing the first copy of leo.py to open a file. 
> This is the part that I'm not sure can be done in pure Python.  I don't
> see anything applicable in section 22 (MS Windows Specific Services) of
> the Python Library.  The C++ version of Leo does this by doing:
> 
> SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);
> 
> where hWnd is a handle to the previously open copy of Leo and cds is a
> COPYDATASTRUCT containing the command line used to open the second copy
> of Leo.

that all should be possible with the win32all extensions. those extensions 
are quasi stadard for most windows users, so just use it...

> Perhaps this problem can be solved in Python using processes instead of
> messages, and I don't see how.  Are there any demos that might be of
> use?  Thanks!

dumb solution: create a "lock file" and remove it on programm exit. the 
other problem is that you have to delete it manually when the app crashes 
(i deleted may lock files with older Netscapes ;-)

i would definetly take win32all.

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list