Hack Forums
[C] How to check if a program is running? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] How to check if a program is running? (/showthread.php?tid=4223145)



How to check if a program is running? - Niely - May 11, 2014

Hello

I want to check if a program is running.
Like this:
PHP Code

<?php 
    bool x
("notepad.exe" == true); {
         
system("taskkill /f /im notepad.exe >nul");
        } else {
            goto 
con;
        }
con:
... 

If notepad.exe is active, taskkill it. Else go to label con.
I know that booleans are true or false, but I don't know how to check if boolean x is valid or not.

Anyway,
Thanks for reading,
Niely


* - Masm4ever - May 11, 2014

It's very easy:
- try to find the window handle
- GetWindowThreadProcessId
- etc
- GetModuleFileNameEx
- etc

BTW why would you kill something as harmless as Notepad?


* - RocketChroma - May 11, 2014

(May 11, 2014 04:41 AM)Masm4ever Wrote:  It's very easy:
- try to find the window handle
- GetWindowThreadProcessId
- etc
- GetModuleFileNameEx
- etc

BTW why would you kill something as harmless as Notepad?

He's probably doing it for testing purposes :)


* - Niely - May 11, 2014

^It's indeed for testing. :)

I did some research and I found something about HWND, but how can I use it now and check if a program is running?

Code
bool isRunning(LPCSTR pName)
{
    HWND hwnd;
    hwnd = FindWindow(NULL, pName);
    if (hwnd != 0) {
        return true;
    } else {
        return false;
    }
}
Why the HWND hwnd; why LPCSTR; and why the program name twice?

I understand the logic about the code, but I want to know the details too.

Thanks! :)


* - olaff00_ - May 11, 2014

hWnd -> Window handle
LPCSTR, program name -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx


* - Masm4ever - May 11, 2014


OMG where did you find that lousy convoluted piece of crap???? It should be
Code
bool isRunning(LPCSTR pName)
{
    return (bool) FindWindow(NULL, pName);
}

And even that is an overkill.


* - Nichole - May 11, 2014

(May 11, 2014 02:02 PM)Masm4ever Wrote:  OMG where did you find that lousy convoluted piece of crap???? It should be
Code
bool isRunning(LPCSTR pName)
{
    return (bool) FindWindow(NULL, pName);
}

And even that is an overkill.

Code
HWND isRunning(LPCSTR pName)
{
    return FindWindow(NULL, pName);
}

Quote:If the function succeeds, the return value is a handle to the window that has the specified class name and window name.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.


So in essence, his wrapper function for FindWindow is completely redundant.


* - Grab1337 - May 13, 2014

If you want to check if a process is running use "Tl32Help" and check if a process with the name is active.