Solved

Multiple Instances of an Application

Posted on 1998-07-15
25
291 Views
Last Modified: 2010-04-10
I know how to use FindWindow to look for an instance of
an application, but I need to keep track of how many
instances of the application are running.  I tried using an
INI file to do this but it is unreliable.  If the program terminates
abnormally the counters do not decrement and the INI will show
that the an instance is still running.
How can I tell how many instances are running.
I am Using Windows 95 and VC++5.0
0
Comment
Question by:jrmcg
  • 9
  • 9
  • 7
25 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 1167858
If this is only for windows 95 (not NT)  you can use CreateToolhelp32Snapshot() to create a snapshot of all the processes currently ruinning.  Then use Process32First() and Processs32Next() to obtai ifo about the processes.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167859
Gaaaaak!!!

You can set up a structured exception handler (SEH) in your program to be called in case of an "abnormal termination" and make the cleanup.

I would also use shared memory and interlocked increment/decrement a counter instead of using INI files.  At least the user will not be able to screw your app by deleting or manually changing the file.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167860
Who was the Gaaaaak to?  (I like my solution, it doesn't seem deserving of a Gaaaaak, well maybe a gaak or a gaaak.)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167861
What happens when a user renames the executable?

0
 
LVL 22

Expert Comment

by:nietod
ID: 1167862
true.  Although you can get the path and name of the current executable.  When I first answered I was thinking in terms of getting the count of another program that is running, not one that you've written.  I'm not sure why, since the question makes clear this is for a program he is writting.

I guess another approach would be to use a named semaphore...and the SEH.  That way you wouldn't need the shared memory.  
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167863
I was about to suggest a semaphore myself as a way of LIMITING the the number of instances.  However it seems jrmcg wants to QUERY the number of instances.  How do you do it with a semaphore?

jrmcg, any comments?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167864
I think its just the two of us here having a chat...

You can use ReleaseSemaphore to find out the semaphore's count.  (I believe--never tried)  If you indicate too high a value to add on to the current value (so it would go past the maximum), ReleaseSemaphore() won't change the semaphore.  It will however, or should, still return the count.
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167865
The semaphore sounds good.  The ini file gives a maximum number of instances that will be allowed to run.  So if the semaphore can limit the number by initially reading an ini file for the maximum allowed it seems that this would be the best possible way to do it.  Can you give me a little more info on using the semaphore?  I've haven't used them before.

Thank you for your help -- jrmcg

0
 
LVL 11

Expert Comment

by:alexo
ID: 1167866
Easy.

Given a global definition:

    HANDLE hSem;

In the beginning of your WinMain() function you write:

    hSem = CreateSemaphore(NULL, count, count, "CounterForMyApp");
    if (WaitForSingleObject(hSem, 0) == WAIT_TIMEOUT)
    {
        // Display an error or something...
        return 0;
    }

When you're about to exit the application write:

    ReleaseSemaphore(hSem, 1, NULL);

In order to the semaphore lock count to decrease even whan the app crashes, you have to  set up a SEH (see my previous comments) and ReleaseSemaphore() from there.

Check the __try, __except and __finally statements of your compiler.

[Todd, I claim this one.  Defend it of yield.  En guarde!]
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167867
Look at the docs on CreateSemaphore() and WaitForSingleObject() first.

Then ask questions.  

But just to give you a roadmap.  You will be using a named semaphore.  That is when you create a semaphore you will give it a name.  (could come fromt he .ini file).  When the program starts up it will create a semaphore with a specified name.  The first program that creates the named sempahore with CreateSemphore() will actually create a new semaphore.  All subsequent attempts will actually just get a handle back to the existing semaphore.
The semaphore's initial count will be the number of instances allowed to run.

The programs will do a WaitForSingleObject() on the semaphore.  if the semaphore's count is positive, indicating less than the maximum instances are running, the procedure will decrement the semaphore's count and return and the program can continue.  If the maximum has been reached, the semaphore's count will be zero.  In this case one of two things will happen.  You can make the wait procedure wait indefinitly.  Thus it will wait until another instance ends and then it will return so that the new program can continue.  More likely what you want it to have the
wait time out quickly.  Then you will look at the return value of the wait procedure to see if the time ran out (indicating the maximum was reached, and if so you should abort the program)  or if you succeeded in gettng a count from the semaphore.

Make sense?  You will need to call CloseHandle() when you are done with the semaphore.  
Alex, Do you know if you need to call ReleaseSemaphore() or will CloseHandle() do that?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167868
touchee.  I'm a bit slow today.  I didn't realize I had been challenged until I had bled to death.
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167869
Excellent.  Sounds like it will do the trick.
I appreciate the help.  That was more than
enough to get me started.  I hope you both
have a great weekend.

jrmcg
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:alexo
ID: 1167870
>>  Do you know if you need to call ReleaseSemaphore() or will CloseHandle() do that?
AFAIK, ReleaseSemaphore() should be explicitly called.  One of the reason is that semaphores (as opposed to mutexes) are not "owned" by the thread that grabbed them.

>> touchee [...]
Yea, right!  The chance of beating a locked question is very slim nowadays.  New users do not seem to understand the concept of locked questions.  The mechanism needs rethinking.
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167871
How would you beat a locked question?  I do still have a question out concerning buffer space and sockets that no one seems to be able to help me with.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167872
>> How would you beat a locked question?

jrmcg, you were hit by an exchange between Todd and me.  Let me explain...

After you submit a question to EE an expert may either answer it or leave a comment.  Once a question is answered, it is considered "locked" by the answerer.  That means that there can be only one pending answer to a question.

Now, the original customer does not always realize that he has several options:

1. He can accept the answer by grading it.  The answerer gets the points.  The question is then considered "answered".  Discussion can continue but other people have to pay points (10% of the original value) to juin the discussion, so usually nobody does.

2. He can reject ("reopen") the question.  Other experts may then lock it with (hopefully) better answers.

3. He can defer the choice until later and leave the question locked until he gets more clarifications from the answerer in the form of comments.

What hapened here is that Todd answered the question.  Later I submitted my answer (in the form of comment, since the answer was locked) which I thought was a bit more suitable for your needs.  You obviously felt differently since you accepted Todd's answer.

"beating a locked question" means that the customer has to reject the pending answer, post a comment saying that he likes the comment submitted by another expert, wait for that other expert to lock the question and finally grade him.
This procedure requires some understanding of the EE model which some inexperienced users lack.  Therefore more often then not the quickest answer gets the points.

I'll look at your other question (no promises...)
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167873
I was under the impression that if you reject an answer it looks bad on the person asking the question and may keep other experts from answering questions in the future.  In other words I thought that was like giving an F grade.

-- If you can help me with the sockets problem I would more than gladly increase the points.

Thanks -- JR

0
 
LVL 22

Expert Comment

by:nietod
ID: 1167874
There are no F grades.  When you reject an answer the expert does not get a grade (F or otherwise).  It is ussually a good idea to try to work with an expert that has answered your question.  Like if you don't completely understand them, ask them for a clarification or additional information rather than rejecting the answer.  Rejecting an answer under those conditions may make an expert angry.  But, if in the end the expert who answered didn't help you and another expert did, then you should reject the useless answer and request the expert who really helped to answer.  

Its up to you to decide who provided the most help and then to see to it that they get the grade.
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167875
Thank you for the info.  I will definitely use it in the future.

Thanks - JR

0
 
LVL 11

Expert Comment

by:alexo
ID: 1167876
>> If you can help me with the sockets problem I would more than gladly increase the points.

Sorry.  Outside my area of knowledge.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167877
If the question is old, you might want to try deleting it and asking it again.  That puts it back at the top of the list.  Sometimes that helps.  I saw one answered within about an hour that way.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167878
You cannot delete a question if it has pending, accepted or rejected answers.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167879
I didn't look at the question to see.  I assumed there were no answers.  When you assume you make....
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167880
After the question sits out there long enough EE will delete it and give me my points back. I rejected the answer that was given after I added a comment asking for clarification and didn't get a reply after 3 or 4 days.  That is the second time I've asked that question.  I thought the first answer had fixed it but it started happening again.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167881
>> After the question sits out there long enough EE will delete it [...]
21 days of no activity.  Somebody adds a comments and it's another 21 days...
Better ask EE customer support to remove it.
0
 
LVL 2

Author Comment

by:jrmcg
ID: 1167882
Thanks I'll do that
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now