Only run if dont find a process

This is a discussion on Only run if dont find a process within the C++ Programming forums, part of the General Programming Boards category; Alright i want my program to run as normally but i woud like to have it searching for an certain ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    Only run if dont find a process

    Alright i want my program to run as normally but i woud like to have it searching for an certain process forexample "myprocess.exe" and IF it finds it then it will not run BUT if it dont find it, then it will run the program code further down on the source code, could anyone show me how this done, and where to put the code it will run if it dont find the process on the code witch will result the code to only run one instance of itself i only need to know this to finish a project, any help is greatly appreciated.
    Last edited by tom1; 03-08-2010 at 01:33 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Searching for processes is prone to errors because the user can easily manipulate it (for example by renaming the executable).

    If you want a behaviour like MSOutlook where you cannot start a new instance of the application when one is already running, you could look into mutexes. Grab one. If you get it, great. If not, another process already got it and you can terminate. That's way (way!) faster than scanning processes.

    I don't think scanning processes or registering mutexes is standard C/C++, so you might want to specify your operating system.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    ive seen mutex

    yes ive looked at mutex, but this source starts another program witch can only be runned once or it will screw the whole thing up, i didnt make the program it start so therefor i can not add mutex to it, or can i ? can i bind a mutex application the program and it will do the same trick ?

    But if searching for process name is the ONLY way to do it, then i have to use that alltough it can be manipulated, however how woud this look like ?




    yes ive looked at mutex, but this source starts another program witch can only be runned once or it will screw the whole thing up, i didnt make the program it start so therefor i can not add mutex to it, or can i ? can i bind a mutex application the program and it will do the same trick ?

    But if searching for process name is the ONLY way to do it, then i have to use that alltough it can be manipulated, however how woud this look like ?


    You can even do this with cmd and vbs so if there is no way to do this with c++ witch is a much bether i woud be disapointed.




    i know this question may not has any connections to this post but what woud be the code to just do this :

    //run everything hidden somehow
    begin
    end.

    i woud like to see if there is possible to just bind mutex to that program if you get what i mean.






    or is there somewhere to search for id`? like taskmgr.exe run on id 3740, notepad 2176, explorer.exe, 1612, so if they do change name id will not change so can you make the code search for that instead of name ?
    Last edited by tom1; 03-09-2010 at 01:28 AM.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    The process id is just a way to separate running processes. It changes every time you run a process so you can't use it the way you want.
    What you could do is something like

    Code:
    mydll.dll
      PROCESS_ATTACH:
        create a mutex
      PROCESS_DETACH:
        release mutex
    
    myprocesslauncher.exe
      if mutex doesn't exist
        launch myprocess.exe
        inject mydll.dll in to myprocess.exe
      else
        don't launch the process
    But that will only help if the user always uses your launcher to start myprocess.exe. It won't help if the user simply double-clicks the myprocess.exe
    If you really want to make sure there's always only max 1 process running at the same time you have to do a global hook on NtCreateProcess I think.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    yes my launcher is the only thing that will run this exe file, if the user itself run the exefile he will just get confused and dont get what it is, the exefile witch will run, runs a script, so yes this woud help alot but also,


    if it could search for window title name and then if it find it make sure it dont run duplicated times, the program do not got an windowname thats hidden, but it got a window title name.... so if a solution is to search for that then, how could this be done ?





    I am not familiar with mutex, i have learned how to implement mutex into the running source only, thats it, and it is not what i need.




    global hook on NtCreateProcess tells me nothing but i think i get what you mean do you know how to do this ? if so please show me
    Last edited by tom1; 03-09-2010 at 01:29 AM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could write a batch file to launch the .exe and do something like this:
    Code:
    IF EXISTS <path>\something.exe.lock (
        ECHO "something.exe is already running!"
    ) ELSE (
        DATE /T >  <path>\something.exe.lock
        <path>\something.exe.lock
        DEL <path>\something.exe.lock
    )
    Although you'd have the ugly Cmd window running the whole time that something.exe is running and it doesn't handle crashes gracefully.
    You can also do something similar in C++ and maybe update the time written to the lock file at certain intervals. Then if the time is off by more than that interval, the program probably crashed, so launch it again, otherwise it's running so don't launch it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    I know that.

    you can also do it like this

    tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > a7.txt

    FOR /F %%A IN (a7.txt) DO IF %%~zA EQU 0 GOTO end

    start notepad.exe

    :end

    del a7.txt

    and i also know 2 methods to hide the cmd window trough bat.

    well however this is not what i want, i want it to be implemented in the c++ source code, also it wont work if not because then it will be slowed down as the source contain one code to open script in the program and one code to run it, so then it woud start slower and thats not an option.
    Last edited by tom1; 03-08-2010 at 07:52 AM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You mention notepad. Can we assume that you are running Windows, then?
    This is not possible using standard C++. To achieve what you want, you need to look into platform specific APIs, hence we need to know what operating system you intend to code for.

    Also, there is something called an edit button. Posting thrice or even 4 times in a row is considered bad, so use the edit button if you need to add something.
    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.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    yes i know sorry...

    yes i tought i mentioned i was running windows xp i forgot about that sorry, also i guess i could try to use the edit button more.

    and i woud prefer if the command also did work with vista but its not a must.
    Last edited by tom1; 03-09-2010 at 01:08 AM.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Here is an example that enumerate all running processes on the machine: Enumerating All Processes (Windows)
    You can use this to determine if a certain process is running.
    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.

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    This thread has the distinct odor of malware.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    The OP, I believe, already explained this need:
    Quote Originally Posted by tom1 View Post
    yes ive looked at mutex, but this source starts another program witch can only be runned once or it will screw the whole thing up, i didnt make the program it start so therefor i can not add mutex to it, or can i ? can i bind a mutex application the program and it will do the same trick ?
    So you think this claim is illegitimate?
    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.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by tom1 View Post
    if it could search for window title name and then if it find it make sure it dont run duplicated times, the program do not got an windowname thats hidden, but it got a window title name.... so if a solution is to search for that then, how could this be done ?
    FindWindow Function ()

    Quote Originally Posted by tom1 View Post
    global hook on NtCreateProcess tells me nothing but i think i get what you mean do you know how to do this ? if so please show me
    Detours - Microsoft Research

    Quote Originally Posted by rags_to_riches View Post
    This thread has the distinct odor of malware.
    I can think of several reasons to only run 1 instance of an application to avoid conflicts. Besides, if it were malware why would the OP come here asking about it instead of a forum dedicated to that kind of stuff.

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    This post:
    i know this question may not has any connections to this post but what woud be the code to just do this :

    //run everything hidden somehow
    begin
    end.

    i woud like to see if there is possible to just bind mutex to that program if you get what i mean.
    seems suspicious to me. Someone looking at process level stuff with what appears to be no knowledge -- and providing no code -- added with that post, makes me uneasy.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Hmm yeah the running hidden comment makes it a bit shadey. I guess I missed that post when scanning through all the double and tripple posting.

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

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Killing A Process
    By tommyb05 in forum C Programming
    Replies: 8
    Last Post: 06-01-2009, 06:41 AM
  3. find the process id
    By nevali in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 08:33 AM
  4. Round Robin Scheduling using c.
    By eclipt in forum C Programming
    Replies: 8
    Last Post: 12-28-2005, 03:58 PM
  5. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM

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