Check For Open Programs

This is a discussion on Check For Open Programs within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Can anyone tell me how to check for open programs? For example, at the start of my program I ...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Check For Open Programs

    Hi,
    Can anyone tell me how to check for open programs? For example, at the start of my program I would like to check if there is another instance of my program running. Does anyone know how to do that? Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    You could probably use findwindow and set the code up before window creation, though there's probably better methods of doing it.

    Code:
                 HWND find;
                find = FindWindow(NULL, "MyWindow name");
               
                if (find) // or try find !=NULL
                {
                MessageBox(NULL,"hey, you're already using this program!",
                "Error",MB_TOPMOST);
                return 0; 
                }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Put this at the start of your program.

    Code:
    CreateMutex(NULL, FALSE, "MYPROGRAMNAME");
    if (GetLastError() == ERROR_ALREADY_EXISTS)
       return 1;
    That will work even if there is a different program running that has the same window name as you.

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thanks for the help. However, would this work even if the program was hidden? (For example, if it was doing work in the background). By hidden I mean in the taskbar, but with no display window.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    The one i posted will. It can even be a console program (doesnt have to have any window at all).

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As 39ster has said, create a mutex -- See MSDN (http://msdn2.microsoft.com/en-us/lib...11(VS.85).aspx)
    Don't forget to free the mutex when you're done, not that it matters -- it is good practice.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    I only want to add that you should not restrict users to run your program several times unless absolutely necessary.
    Many, I think (including me), get incredibly annoyed just because I can't open another instance because the program says I can't. Who is to decide here? Me or the application?
    I always use an approach that gives the user full control, but perhaps warns a bit if it might be dangerous.

    As a programmer, it seems cool to do things like everyone else does. Stop a program from running multiple instances, a nice splash screen, credits, etc. But the user does not enjoy those.
    Ponder on that one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    You think you can run several windows Messagers on the same comp?

    Or several programs that should start listening on one "well-known" port?
    Or several programs that should open the same COM-port to configure the external device?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    You think you can run several windows Messagers on the same comp?

    Or several programs that should start listening on one "well-known" port?
    Or several programs that should open the same COM-port to configure the external device?
    But that should, really, be restricted by the single resource that those two apps would require to use. I could for example run a terminal program using COM1, and another one using COM2. Or two different messenger applications using different ports, two mail servers using individual ports, etc, etc. Obviously, if I try to use COM1 from two different apps, I definitely would like to know about it - but that should be the case even if I try to use both teraterm and hyperterminal on COM1, not only if I try to use two instances of teraterm or hyperterminal. And the OS is quite competent on telling you if the resource is busy in these cases, so it shouldn't be a problem.

    There are a few instances where there is no resource that prevents the app from running, where you still want to ensure that the same app is not running - in which case an "invented" resource such as a Mutex or other named object could be used (lock-files are the common way to do this in a portable way - instance 1 creates a file, instance 2 tries to creat the same file, and fails because instance 1 got there first).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by vart View Post
    You think you can run several windows Messagers on the same comp?
    Yes, it's possible with "Messenger Plus!".
    And there's no saying you have to lock it to the same port.

    And btw, even if it can't use the resource such as the COM port, it can still, if reasonable, allow its other features to be used.
    Last edited by Elysia; 02-27-2008 at 06:45 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by Elysia View Post
    I only want to add that you should not restrict users to run your program several times unless absolutely necessary.
    Many, I think (including me), get incredibly annoyed just because I can't open another instance because the program says I can't. Who is to decide here? Me or the application?
    I always use an approach that gives the user full control, but perhaps warns a bit if it might be dangerous.

    As a programmer, it seems cool to do things like everyone else does. Stop a program from running multiple instances, a nice splash screen, credits, etc. But the user does not enjoy those.
    Ponder on that one.
    I agree, however if the application was doing some large task it would be bad to have more than one open.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    But that's not up to the program to decide; it's up to the user to decide.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    No, that's partly the responsibility of the programmer to decide if running multiple instances of the program would cause woes to the end-user. Who knows the software better than the author? If there are valid reasons to prevent multiple instances from being launched, by all means, preventing it is smart programming.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    I have a hard time finding a reason to ever do that. And if you do, a nice error message describing why to the user might also be nice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  4. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 04:13 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21