CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Checking if process exists

    I'm trying to find a method that will return true if a process exists, and false if it does not. I came across this one, but can't seem to get it to work properly.
    Code:
    using System.Diagnostics;
    
    int main() {
    	if IsProcessOpen(firefox) == true {
    		MessageBox(NULL, "Firefox is running", NULL, NULL);
    	}
    
    }
    public bool IsProcessOpen(string name)
    {
    	foreach (Process clsProcess in Process.GetProcesses()) {
    		if (clsProcess.ProcessName.Contains(name))
    		{
    			return true;
    		}
    	}
    	return false;
    }
    
    }
    How can I change this so it works correctly o.o?

    any help would be appriciated,

    thanks,

    Coukapecker

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Checking if process exists

    That's managed code / c#. So of course it wont compile as c++ code

    I suggest you look at win api / msdn documents for process enumeration.

    here: http://msdn.microsoft.com/en-us/libr...89(VS.85).aspx is probably a good start.

  3. #3
    Join Date
    Oct 2009
    Location
    Holland
    Posts
    201

    Re: Checking if process exists

    You can use;

    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured