CreateProcess only once.

In my application I'm using CreateProcess() function in order to invoke the "character map" execution.
By clicking a command from the toolbar, the user opens the characters map.
The problem is that I dont want to allow this operation if the "character map" is already running.

how can I do that ?
Who is Participating?
ZoppoConnect With a Mentor Commented:
Hi yoavo,

I agree with VinExpert, but I fear it's not enough for your purpose, because the ( hProcess != NULL ) will even be TRUE if the processe is terminated by a user. But you can use this given handle to find out if the process is still running, i.e.

class CMyApp : ...
 HANDLE m_hProcess;

CMyApp::CMyApp ()
 m_hProcess = NULL;

CMyApp::OnOpenCharMap()//message handler for the button
 if ( m_hProcess != NULL )
   DWORD exitCode;
   GetExitCodeProcess( m_hProcess, &exitCode );
   if ( exitCode == STILL_ACTIVE )
    return; // charmap is still running
 if ( CreateProcess( ..., &pi ) ) // create the process if not running
  m_hProcess = pi.hProcess;
  m_hProcess = NULL;

hope that helps,

Vinayak KumbarSr Program ManagerCommented:

CreateProcess() uses PROCESS_INFORMATION variable.
In that hProcess member will contain the handle to newly created process U can check for that

if(...hProcess != NULL)
     //Open character map

For that U may have to make PROCESS_INFORMATION's variable as the member function of that class. Pls see to it that by default and whenever U kill that process or if process not created successfully assign it to NULL.

Try it out.
All Courses

From novice to tech pro — start learning today.