Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to close the Opened process.

How to close the Opened process.

Scheduled Pinned Locked Moved C / C++ / MFC
comhelptutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GauranG Shah
    wrote on last edited by
    #1

    I am using following code to create the new process. Now I want to Close the process that I have created. I am using CloseHandle() to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.

    PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
    {
    TCHAR str[100] ={};
    _tcscpy_s( str, 100, CmdLine );

    STARTUPINFO si;
    PROCESS\_INFORMATION pi;
    
    BOOL ProcCreated;
    ZeroMemory(&si,sizeof(si));
    si.cb			= sizeof(si);
    si.dwFlags		= STARTF\_USESHOWWINDOW;
    si.wShowWindow	= SW\_SHOWMAXIMIZED;
    ZeroMemory(&pi,sizeof(pi));
    
    //si.lpDesktop	= \_T("NewDesktop");
    si.lpDesktop	=	NULL;
    ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi );          
    
    return pi;
    

    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    char ch;
    PROCESS_INFORMATION pi;
    DWORD dError;.
    do
    {
    scanf("%c",&ch);
    if(ch == 'c')
    pi=OpenProc(L"MSPAINT");
    if(ch == 't')
    {
    CloseHandle(pi.hProcess);
    dError = GetLastError(); //Error = 0
    CloseHandle(pi.hThread);
    dError = GetLastError(); //dError =0
    }

    } while(ch != 'E');
    

    }

    [ Screen Capture ][ Tool Tip ]

    H N D J 4 Replies Last reply
    0
    • G GauranG Shah

      I am using following code to create the new process. Now I want to Close the process that I have created. I am using CloseHandle() to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.

      PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
      {
      TCHAR str[100] ={};
      _tcscpy_s( str, 100, CmdLine );

      STARTUPINFO si;
      PROCESS\_INFORMATION pi;
      
      BOOL ProcCreated;
      ZeroMemory(&si,sizeof(si));
      si.cb			= sizeof(si);
      si.dwFlags		= STARTF\_USESHOWWINDOW;
      si.wShowWindow	= SW\_SHOWMAXIMIZED;
      ZeroMemory(&pi,sizeof(pi));
      
      //si.lpDesktop	= \_T("NewDesktop");
      si.lpDesktop	=	NULL;
      ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi );          
      
      return pi;
      

      }

      int _tmain(int argc, _TCHAR* argv[])
      {
      char ch;
      PROCESS_INFORMATION pi;
      DWORD dError;.
      do
      {
      scanf("%c",&ch);
      if(ch == 'c')
      pi=OpenProc(L"MSPAINT");
      if(ch == 't')
      {
      CloseHandle(pi.hProcess);
      dError = GetLastError(); //Error = 0
      CloseHandle(pi.hThread);
      dError = GetLastError(); //dError =0
      }

      } while(ch != 'E');
      

      }

      [ Screen Capture ][ Tool Tip ]

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Why you dont use of TerminateProcess?

      G 1 Reply Last reply
      0
      • G GauranG Shah

        I am using following code to create the new process. Now I want to Close the process that I have created. I am using CloseHandle() to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.

        PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
        {
        TCHAR str[100] ={};
        _tcscpy_s( str, 100, CmdLine );

        STARTUPINFO si;
        PROCESS\_INFORMATION pi;
        
        BOOL ProcCreated;
        ZeroMemory(&si,sizeof(si));
        si.cb			= sizeof(si);
        si.dwFlags		= STARTF\_USESHOWWINDOW;
        si.wShowWindow	= SW\_SHOWMAXIMIZED;
        ZeroMemory(&pi,sizeof(pi));
        
        //si.lpDesktop	= \_T("NewDesktop");
        si.lpDesktop	=	NULL;
        ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi );          
        
        return pi;
        

        }

        int _tmain(int argc, _TCHAR* argv[])
        {
        char ch;
        PROCESS_INFORMATION pi;
        DWORD dError;.
        do
        {
        scanf("%c",&ch);
        if(ch == 'c')
        pi=OpenProc(L"MSPAINT");
        if(ch == 't')
        {
        CloseHandle(pi.hProcess);
        dError = GetLastError(); //Error = 0
        CloseHandle(pi.hThread);
        dError = GetLastError(); //dError =0
        }

        } while(ch != 'E');
        

        }

        [ Screen Capture ][ Tool Tip ]

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        MSDN clearly says that "Closing a thread handle does not terminate the associated thread"

        - NS -

        1 Reply Last reply
        0
        • H Hamid Taebi

          Why you dont use of TerminateProcess?

          G Offline
          G Offline
          GauranG Shah
          wrote on last edited by
          #4

          I used the Code Like this to terminate the process. The code works fine when the Created Process is in the same desktop. But It doesnn't success when the Process created is in **another Desktop.**It doesn't show any Error but it doesn't close the opened process.

          //dwExitCode = 259 Always. if process is in another Desktop
          GetExitCodeProcess(pi.hProcess,&dwExitCode);
          TerminateProcess(pi.hProcess,dwExitCode);

          [ Screen Capture ][ Tool Tip ]

          H 1 Reply Last reply
          0
          • G GauranG Shah

            I am using following code to create the new process. Now I want to Close the process that I have created. I am using CloseHandle() to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.

            PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
            {
            TCHAR str[100] ={};
            _tcscpy_s( str, 100, CmdLine );

            STARTUPINFO si;
            PROCESS\_INFORMATION pi;
            
            BOOL ProcCreated;
            ZeroMemory(&si,sizeof(si));
            si.cb			= sizeof(si);
            si.dwFlags		= STARTF\_USESHOWWINDOW;
            si.wShowWindow	= SW\_SHOWMAXIMIZED;
            ZeroMemory(&pi,sizeof(pi));
            
            //si.lpDesktop	= \_T("NewDesktop");
            si.lpDesktop	=	NULL;
            ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi );          
            
            return pi;
            

            }

            int _tmain(int argc, _TCHAR* argv[])
            {
            char ch;
            PROCESS_INFORMATION pi;
            DWORD dError;.
            do
            {
            scanf("%c",&ch);
            if(ch == 'c')
            pi=OpenProc(L"MSPAINT");
            if(ch == 't')
            {
            CloseHandle(pi.hProcess);
            dError = GetLastError(); //Error = 0
            CloseHandle(pi.hThread);
            dError = GetLastError(); //dError =0
            }

            } while(ch != 'E');
            

            }

            [ Screen Capture ][ Tool Tip ]

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            GauranG33 wrote:

            TCHAR str[100] ={}; ... DWORD dError;.

            These two statements are in error. That has no bearing on your problem, however, but it does make it somewhat difficult to debug something with syntax errors.


            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            • G GauranG Shah

              I am using following code to create the new process. Now I want to Close the process that I have created. I am using CloseHandle() to close the Process with process handle as argument. But its not working. The following code Opens the MSPAINT success fully when I press the 'c' But it doesn't get closed when I press the 't'.

              PROCESS_INFORMATION OpenProc( TCHAR *CmdLine)
              {
              TCHAR str[100] ={};
              _tcscpy_s( str, 100, CmdLine );

              STARTUPINFO si;
              PROCESS\_INFORMATION pi;
              
              BOOL ProcCreated;
              ZeroMemory(&si,sizeof(si));
              si.cb			= sizeof(si);
              si.dwFlags		= STARTF\_USESHOWWINDOW;
              si.wShowWindow	= SW\_SHOWMAXIMIZED;
              ZeroMemory(&pi,sizeof(pi));
              
              //si.lpDesktop	= \_T("NewDesktop");
              si.lpDesktop	=	NULL;
              ProcCreated =CreateProcess( NULL,str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi );          
              
              return pi;
              

              }

              int _tmain(int argc, _TCHAR* argv[])
              {
              char ch;
              PROCESS_INFORMATION pi;
              DWORD dError;.
              do
              {
              scanf("%c",&ch);
              if(ch == 'c')
              pi=OpenProc(L"MSPAINT");
              if(ch == 't')
              {
              CloseHandle(pi.hProcess);
              dError = GetLastError(); //Error = 0
              CloseHandle(pi.hThread);
              dError = GetLastError(); //dError =0
              }

              } while(ch != 'E');
              

              }

              [ Screen Capture ][ Tool Tip ]

              J Offline
              J Offline
              JudyL_MD
              wrote on last edited by
              #6

              Send the opened process a WM_CLOSE message. Just closing your handle to it does not close the process. You've received some suggestions to use TerminateProcess - that is the last way you should try if you can't close a process nicely. Using TP can causes memory leaks and issues if the process is using any kind of inter-process communication. It is a function-of-last-resort. Judy

              1 Reply Last reply
              0
              • G GauranG Shah

                I used the Code Like this to terminate the process. The code works fine when the Created Process is in the same desktop. But It doesnn't success when the Process created is in **another Desktop.**It doesn't show any Error but it doesn't close the opened process.

                //dwExitCode = 259 Always. if process is in another Desktop
                GetExitCodeProcess(pi.hProcess,&dwExitCode);
                TerminateProcess(pi.hProcess,dwExitCode);

                [ Screen Capture ][ Tool Tip ]

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                Can you get handle to this window and then send WM_CLOSE to it?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups