I'm making a program and in one of the part of the program there is this button and for it to do main function it needs to check if the notepad is running. So how can i check if the process is running?

Recommended Answers

All 26 Replies

I'm using windows form application!

You can use the Windows API with C++/CLI normally, unless you intend to consume functions/classes that are purely native code with other .NET languages, in which case you will need a managed wrapper around the pure native functions/classes to use them with other .NET languages.

If your project will only use C++ or C++/CLI & C++ then it is my guess you can use the Windows API functions with no problem.

There may be a managed solution to this problem, but I am unaware of the details of it. By all means feel free to research a managed solution to this problem on MSDN.

CAn you tell me the code?

As for solving the problem, my idea is to enumerate the running processes, and search for ones named "notepad", and ones with a path to the executable that point to the location of "notepad.exe", and if required (normally it probably would be, otherwise this faulty mechanism for detecting if notepad is open won't always work), I would attempt to compare checksums of running executables to a list of checksums for "notepad.exe", depending on which Windows version you are running the checksum may be different.

Now, that raises the question what if the user is running another text editor? Should that also be detected? I think the entire idea is faulty, or maybe I just don't have a database of checksums for known text editors.

In the end, the problem is yours to solve, unless you intend to pay someone else to do it for you.

If you do manage to find a very elegant managed solution to the problem, then please do tell.

This solution seems to be highly dependent upon the requirements.

what i want is so easy i just couldnt explain!
Now!

I created this program it runs other programs...
While it's running progress bar starts and when the program is open thats when the process going to be check. When the process is open then the progresss bar is going to stop!

Oh ok, so did you use the "Process" class to start the process?
Process Class: http://msdn.microsoft.com/en-us/library/ccf1tfx0.aspx

If so, then this property will be what you want to check: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx

Alternatively, you may want to use this event: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx

Honestly It's best if you take your pick, because there is more than one way to do it. So, do some research for a managed solution to the problem: http://msdn.microsoft.com/en-us/library/ccf1tfx0.aspx
then decide what you want to do, and give it a try.

this is what i used to start the process:

System::Diagnostics::Process::Start("File Destination");

I don't want to START any process!!!
I want to check if the notepad is running!
If it's running then the progress bar stops!

Now you've confused me, would you please clarify if your application starts the process or not?

I actually wrote some code to show you one way to do it.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
	using namespace System::Diagnostics;
	String ^notepad_path = "C:\\Windows\\System32\\notepad.exe";//<-- path to .exe
	ProcessStartInfo ^notepad_procinfo = gcnew ProcessStartInfo(notepad_path);//<-- process start info,
	//^^ required to associate an instance of the process class with the process it creates.
	Process ^notepad_proc = gcnew Process();//<-- Process class instance.
	
	notepad_proc->StartInfo = notepad_procinfo;//<-- Set this property to the notepad_procinfo.

	if(notepad_proc->Start())//<-- parameterless start method called, it uses the previously established ProcessStartInfo
		//^^ This method is NOT static, unlike the version you were previously using.  This version is unique to the class instance.
	{
		Console::WriteLine("Notepad running.");
	}
	else
	{
		Console::WriteLine("PROCESS CREATION FAILED!");
		return 1;
	}
	while( !notepad_proc->HasExited )
	{
		Console::WriteLine("Still running...");
		Threading::Thread::Sleep(1000);
	}
    return 0;
}

Honestly, if you had read the links I have given you, I think you could have done this. If not, then learn a bit so you aren't asking someone for code. Problem solving is a part of programming, you know?

- My app doesn't start the application
- It checks if the application is running
- if its running progress bar stops
- if its not running progress bar continues

Application = E.g: Notepad, firefox, safari, 7-zip....

And....

There is one button that button starts the program (Dont worry about this part)
and same button also checks if the programs is started. <---- This is the important line!

Then you have to decide how you want to check to see if it's running. There is more than one way to do it, and like I said before if this is supposed to be reliable in any way it will require more work.

Take a look at the examples for C++ on this page: http://msdn.microsoft.com/en-us/library/x8b2hzk8.aspx

CAn't you just write the code for me its really important!

Not unless you pay me for it.

Problem Solving skills are valuable, and this isn't a place to beg people to write code for you.

Would this code work?

array<Process^>^localByName = Process::GetProcessesByName( "notepad" );

I done it! Yes! i Have used if to only 2 lines of code and you was asking me to pay you lol!

actually i realised now it don't work

Fixed it now i need to add couple more features then done!

kk

Would this code work?

I done it! Yes! i Have used if to only 2 lines of code and you was asking me to pay you lol!

Still thanks.

actually i realised now it don't work

Fixed it now i need to add couple more features then done!

Thanks for tweeting :icon_rolleyes:

lol I got couple more ideas im trying figure out fixing it!

int isRunning(char *pProcessName)
{
    HANDLE hSnap = INVALID_HANDLE_VALUE;
    HANDLE hProcess = INVALID_HANDLE_VALUE;
    PROCESSENTRY32 ProcessStruct;
    ProcessStruct.dwSize = sizeof(PROCESSENTRY32);
    hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if(hSnap == INVALID_HANDLE_VALUE)
        return -1;
    if(Process32First(hSnap, &ProcessStruct) == FALSE)
        return -1;
    do
    {
        if(stricmp(strupr(ProcessStruct.szExeFile), pProcessName)==0)
        {
            CloseHandle( hSnap );
            return  ProcessStruct.th32ProcessID;
            break;
        }
    }
    while( Process32Next( hSnap, &ProcessStruct ) );
    CloseHandle( hSnap );
    return -1;
}

Taken from another site.. not sure if im allowed to link the source.. but thats how I went about it when I had to do it.. just modified it a bit.. cuz the original was segfaulting.. :(

I think this should work...
HWND hwnd=FindWindow("notepad",NULL);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.