Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help needed with dialog based application

77 views
Skip to first unread message

Terry Ford

unread,
Mar 11, 1996, 3:00:00 AM3/11/96
to


How can I prevent multiple instances from running with a dialog based
application in Windows 95?

I'm familiar with the use of FindWindow as demonstrated in the
"Generic" sample that comes with Visual C++ 4.0, but this function
doesn't seem appropriate when using a dialog based application.

I know how to "fit" a window around a borderless dialog, and
FindWindow would work in this case, but is there a simpler/more
elegant solution?


All the best,
Terry
--
Name: Terry Ford
E-Mail: ter...@globalnet.co.uk


Chris Marriott

unread,
Mar 12, 1996, 3:00:00 AM3/12/96
to
In article <3144567...@news.globalnet.co.uk>, Terry Ford
<ter...@globalnet.co.uk> writes

>I'm familiar with the use of FindWindow as demonstrated in the
>"Generic" sample that comes with Visual C++ 4.0, but this function
>doesn't seem appropriate when using a dialog based application.

There are many ways to do this. One quite good way is to use a named
kernel object such as a semaphore. In your initialization, call
"OpenSemaphore" to attempt to open the object; if you succeed then you
know you're not the first instance. If you fail, you ARE the first, so
you then call "CreateSemaphore" to create the object for later instances
to find.

Chris
--------------------------------------------------------------------------
Chris Marriott, Warrington, UK | Author of SkyMap v3 award-winning
ch...@chrism.demon.co.uk | shareware Win31/Win95 planetarium.
For full info, see http://www.execpc.com/~skymap
Author member of Association of Shareware Professionals (ASP)
--------------------------------------------------------------------------


Ole Nielsby

unread,
Mar 13, 1996, 3:00:00 AM3/13/96
to
Terry Ford wrote:

> How can I prevent multiple instances ... with a dialog based
> application...? FindWindow ... doesn't seem appropriate.

> I know how to "fit" a window around..., but is there a
> simpler/more elegant solution?

Don't know if this seems simpler/more elegant but it bypasses
the "container" window:

Register a message with the name of your company/application,
and have your dialog procedure answer this message with
TRUE, other dialogs will answer FALSE. Use EnumWindows to
try all dialogs. Be sure to set dialog message result with
SetWindowLong(...DWL_MSGRESULT...)

I haven't tried this with dialogs but use a similar scheme for
an interpreter, to run a Danish and an English version of my
PILS language concurrently, based on copies of the same EXE.
(Actually, I don't run the English version but never mind.)


Ole Nielsby


Chris Pyman

unread,
Mar 14, 1996, 3:00:00 AM3/14/96
to
ter...@globalnet.co.uk (Terry Ford) wrote:


>How can I prevent multiple instances from running with a dialog based
>application in Windows 95?


I'm only guessing here, but doesn't VC4 provide you with a class
derived from CWinApp, in dialog-based applications? And a single
["one and only"] object of that class? Cos I think there's a member
of CWinApp called PrevInstance or something like that. I was looking
it up today actually, just in passing and I never got around to
testing it... if it works, could you let me know? :)

Thanks
chris

Steven Youngs

unread,
Mar 14, 1996, 3:00:00 AM3/14/96
to
In article <Ksu6izAM...@chrism.demon.co.uk>
Chris Marriott <ch...@chrism.demon.co.uk> wrote:

> In article <3144567...@news.globalnet.co.uk>, Terry Ford
> <ter...@globalnet.co.uk> writes
>>I'm familiar with the use of FindWindow as demonstrated in the
>>"Generic" sample that comes with Visual C++ 4.0, but this function
>>doesn't seem appropriate when using a dialog based application.
>
> There are many ways to do this. One quite good way is to use a named
> kernel object such as a semaphore. In your initialization, call
> "OpenSemaphore" to attempt to open the object; if you succeed then you
> know you're not the first instance.

This is not strictly true - some other program could have created a semaphore
with the same name as the one you try to create. So, in fact, more correct wording
is

"if you succeed then you know you are probably not the first instance"

> If you fail, you ARE the first, so
> you then call "CreateSemaphore" to create the object for later instances
> to find.
>

Agreed.

> Chris
> --------------------------------------------------------------------------
> Chris Marriott, Warrington, UK | Author of SkyMap v3 award-winning
> ch...@chrism.demon.co.uk | shareware Win31/Win95 planetarium.
> For full info, see http://www.execpc.com/~skymap
> Author member of Association of Shareware Professionals (ASP)
> --------------------------------------------------------------------------

Steve.

-------------
Steven Youngs
NC Graphics


Jerry Coffin

unread,
Mar 14, 1996, 3:00:00 AM3/14/96
to
In article <Ksu6izAM...@chrism.demon.co.uk>, <ch...@chrism.demon.co.uk>
says...

[ determining if a previous instance of a program is active ]

> There are many ways to do this. One quite good way is to use a named
> kernel object such as a semaphore. In your initialization, call
> "OpenSemaphore" to attempt to open the object; if you succeed then you

> know you're not the first instance. If you fail, you ARE the first, so


> you then call "CreateSemaphore" to create the object for later instances
> to find.

This has the same basic problem as using FindWindow. If two instances start
at nearly the same time, both will check for a semaphore that doesn't exist
yet, find it doesn't exist yet, then both will attempt to create it.

To work correctly, you have to detect whether the prior instance exists _and_
create the semaphore if it doesn't, both as a single operation. Fortunately,
Create* will handle this for us.

Since we want only one instance to ever exist we're looking for a Mutex
semaphore here, so CreateMutex would be easier to deal with than
CreateSemaphore. CreateSemaphore would be more suitable if we wanted to allow
up to a particular number of intances to exist, but no more than that. E.g.
we might be licensed to use up to 20 copies at once, in which case we could
create a semaphore with an initial count of 20. You could, of course, use
CreateSemaphore with the initial count set to 1, but a mutex is a bit easier
to use, so when you can, it's far easier to use them.

Chris Marriott

unread,
Mar 15, 1996, 3:00:00 AM3/15/96
to
In article <Do94t...@ncgrafix.demon.co.uk>, Steven Youngs
<st...@ncgraphics.co.uk> writes

>This is not strictly true - some other program could have created a semaphore
>with the same name as the one you try to create.

That's true, but if you pick a suitably "random" name the chances of it
happening are remote! ie use a name like "glgl65fl609200skg", not
"MySem" :-).

Nick Mason

unread,
Mar 15, 1996, 3:00:00 AM3/15/96
to
In article: 37355 of comp.os.ms-windows.programmer.win32 Chris Marriott
<ch...@chrism.demon.co.uk> said:

> In article <Do94t...@ncgrafix.demon.co.uk>, Steven Youngs
> <st...@ncgraphics.co.uk> writes
> >This is not strictly true - some other program could have created a
> semaphore
> >with the same name as the one you try to create.
>
> That's true, but if you pick a suitably "random" name the chances of it
> happening are remote! ie use a name like "glgl65fl609200skg", not
> "MySem" :-).

You can get a unique name out of uuidgen, part of the RPC kit which comes
with VC 4.


Nick Mason,
Husmatt 9, CH-5405 Baden-Dättwil, Switzerland.
Telephone (41)-56-470 0650.
email nma...@cix.compulink.co.uk

Pete Albano

unread,
Mar 15, 1996, 3:00:00 AM3/15/96
to

CWinApp::m_hPrevInstance does not work under WIN32 !!

I believe there are technotes on creating a single instance application
under WIN32 in the MS Knowledgebase and MSDN.

--
Pete Albano
pe...@telechips.com, pal...@msn.com
http://www.telechips.com

Bret Pehrson

unread,
Mar 18, 1996, 3:00:00 AM3/18/96
to
Chris Marriott wrote:
>
> In article <3144567...@news.globalnet.co.uk>, Terry Ford
> <ter...@globalnet.co.uk> writes
> >I'm familiar with the use of FindWindow as demonstrated in the
> >"Generic" sample that comes with Visual C++ 4.0, but this function
> >doesn't seem appropriate when using a dialog based application.
>
> There are many ways to do this. One quite good way is to use a named
> kernel object such as a semaphore. In your initialization, call
> "OpenSemaphore" to attempt to open the object; if you succeed then you
> know you're not the first instance. If you fail, you ARE the first, so
> you then call "CreateSemaphore" to create the object for later instances
> to find.
>

Good idea, Chris. However, if you need to be absolutely certain that another
instance is not running, you will run into problems with preemptive scheduling. A
better solution would be to create a named semaphore and see if it fails (and
GetLastError is ERROR_ALREADY_EXISTS). This way, you will not run into problems
with first trying to open and then create.

--
Bret Pehrson Br...@strata3d.com
*Please respond to newsgroup unless specified otherwise
--

Jon Machtynger

unread,
Mar 20, 1996, 3:00:00 AM3/20/96
to
Chris Marriott <ch...@chrism.demon.co.uk> wrote:

>In article <Do94t...@ncgrafix.demon.co.uk>, Steven Youngs
><st...@ncgraphics.co.uk> writes
>>This is not strictly true - some other program could have created a semaphore
>>with the same name as the one you try to create.

>That's true, but if you pick a suitably "random" name the chances of it
>happening are remote! ie use a name like "glgl65fl609200skg", not
>"MySem" :-).

Unix has a great routine called ftok() This determines a key
for a semaphore/shared memory based on the inode of
a file. Is there a similar routine under NT? If so, then
there's your solution.

Jon


0 new messages