Terminating Process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GTS

    Terminating Process

    Hi All,

    I am spawning a process from a service. The spawned process hungs for
    various reasons, (corrupted data, deadlock). I am expecting the process has
    to complete the task with in the expected time limit, if it exceeds the
    limit I want to terminate the process (no mercy or graceful, just terminate
    the process).

    The spawnng of the process, counting the time and termination of process all
    done in C++. Some time I am not able to terminate the process, the error
    message was "Access denied". I dont want this also happens. The program has
    to terminate the process even, the process in any state. How to do that?
    What security permissions I have to give?

    Here is the code snippet of the teminate application,
    =============== =============== =============== ============
    static DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
    {
    HANDLE hProc ;
    DWORD dwRet ;

    // If we can't open the process with PROCESS_TERMINA TE rights,
    // then we give up immediately.
    hProc = OpenProcess(SYN CHRONIZE|PROCES S_TERMINATE, FALSE,
    dwPID);

    if(hProc == NULL)
    {
    return TA_FAILED ;
    }

    // TerminateAppEnu m() posts WM_CLOSE to all windows whose PID
    // matches your process's.
    EnumWindows((WN DENUMPROC)Termi nateAppEnum, (LPARAM) dwPID) ;

    // Wait on the handle. If it signals, great. If it times out,
    // then you kill it.
    if(WaitForSingl eObject(hProc, dwTimeout)!=WAI T_OBJECT_0)
    dwRet=(Terminat eProcess(hProc, 0)?TA_SUCCESS_K ILL:TA_FAILED);
    else
    dwRet = TA_SUCCESS_CLEA N ;

    CloseHandle(hPr oc) ;

    return dwRet ;
    }


    static BOOL CALLBACK TerminateAppEnu m( HWND hwnd, LPARAM lParam )
    {
    DWORD dwID ;

    GetWindowThread ProcessId(hwnd, &dwID) ;

    if(dwID == (DWORD)lParam)
    {
    PostMessage(hwn d, WM_CLOSE, 0, 0) ;
    }

    return TRUE ;
    }

    =============== =============== =============== ============

    All suggestion appreciated.

    Thanks

    GTS


  • Vinayak Raghuvamshi

    #2
    Re: Terminating Process

    "GTS" <gtskaran@hotma il.com> wrote in message news:<O1f21IvcE HA.1672@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
    > Hi All,
    >
    > I am spawning a process from a service. The spawned process hungs for
    > various reasons, (corrupted data, deadlock). I am expecting the process has
    > to complete the task with in the expected time limit, if it exceeds the
    > limit I want to terminate the process (no mercy or graceful, just terminate
    > the process).
    > ...[/color]

    Try this.

    HANDLE h = OpenProcess(PRO CESS_TERMINATE, FALSE,dwId);
    TerminateProces s(h,0);

    sending a wm_close to a "hung" process does not really help much....

    hth.
    -Vinayak

    Comment

    • JJ

      #3
      Re: Terminating Process

      Or a better idea would be to send WM_CLOSE, wait some time, then check to
      see if the process still exists. If it does, then do TerminateProces s() as
      a last resort.


      "Vinayak Raghuvamshi" <vs_raghuvamshi @hotmail.com> wrote in message
      news:9afa978c.0 407261322.4236b 7e6@posting.goo gle.com...[color=blue]
      > "GTS" <gtskaran@hotma il.com> wrote in message[/color]
      news:<O1f21IvcE HA.1672@TK2MSFT NGP12.phx.gbl>. ..[color=blue][color=green]
      > > Hi All,
      > >
      > > I am spawning a process from a service. The spawned process hungs for
      > > various reasons, (corrupted data, deadlock). I am expecting the process[/color][/color]
      has[color=blue][color=green]
      > > to complete the task with in the expected time limit, if it exceeds the
      > > limit I want to terminate the process (no mercy or graceful, just[/color][/color]
      terminate[color=blue][color=green]
      > > the process).
      > > ...[/color]
      >
      > Try this.
      >
      > HANDLE h = OpenProcess(PRO CESS_TERMINATE, FALSE,dwId);
      > TerminateProces s(h,0);
      >
      > sending a wm_close to a "hung" process does not really help much....
      >
      > hth.
      > -Vinayak[/color]


      Comment

      • Adam Clauss

        #4
        Re: Terminating Process

        "JJ" <jjjj@nospam.co m> wrote in message news:OhKPNs3cEH A.384@TK2MSFTNG P10.phx.gbl...[color=blue]
        > Or a better idea would be to send WM_CLOSE, wait some time, then check to
        > see if the process still exists. If it does, then do TerminateProces s() as
        > a last resort.[/color]

        Isn't that what the original poster did?
        He sends the windows of the process the WM_CLOSE, then does WaitForSingleOb ject on the process. If it times-out rather than
        signals, he calls TerminateProces s.

        --
        Adam Clauss
        cabadam@tamu.ed u

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Terminating Process

          Could you check the process identity the service runs with?
          If it's:
          "local system" AND
          "Allow service to interact with desktop" is enabled,
          AND the spawned process has a windows message loop (message pump).
          then posting ( WM_CLOSE ) should work.

          However, this won't work If "Allow service to interact with desktop" is not
          set, as you can't post/send messages from the interactive desktop your
          "terminate" program runs in to the sandboxed services desktop/winstation
          your spawned process runs in.
          "TerminateProce ss" will not work either, as the spawned process runs as
          "local system" it can only be terminated by a process running as "local
          system", even an administrator can't terminate such process.

          Willy.


          "GTS" <gtskaran@hotma il.com> wrote in message
          news:O1f21IvcEH A.1672@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi All,
          >
          > I am spawning a process from a service. The spawned process hungs for
          > various reasons, (corrupted data, deadlock). I am expecting the process
          > has
          > to complete the task with in the expected time limit, if it exceeds the
          > limit I want to terminate the process (no mercy or graceful, just
          > terminate
          > the process).
          >
          > The spawnng of the process, counting the time and termination of process
          > all
          > done in C++. Some time I am not able to terminate the process, the error
          > message was "Access denied". I dont want this also happens. The program
          > has
          > to terminate the process even, the process in any state. How to do that?
          > What security permissions I have to give?
          >
          > Here is the code snippet of the teminate application,
          > =============== =============== =============== ============
          > static DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
          > {
          > HANDLE hProc ;
          > DWORD dwRet ;
          >
          > // If we can't open the process with PROCESS_TERMINA TE rights,
          > // then we give up immediately.
          > hProc = OpenProcess(SYN CHRONIZE|PROCES S_TERMINATE, FALSE,
          > dwPID);
          >
          > if(hProc == NULL)
          > {
          > return TA_FAILED ;
          > }
          >
          > // TerminateAppEnu m() posts WM_CLOSE to all windows whose PID
          > // matches your process's.
          > EnumWindows((WN DENUMPROC)Termi nateAppEnum, (LPARAM) dwPID) ;
          >
          > // Wait on the handle. If it signals, great. If it times out,
          > // then you kill it.
          > if(WaitForSingl eObject(hProc, dwTimeout)!=WAI T_OBJECT_0)
          > dwRet=(Terminat eProcess(hProc, 0)?TA_SUCCESS_K ILL:TA_FAILED);
          > else
          > dwRet = TA_SUCCESS_CLEA N ;
          >
          > CloseHandle(hPr oc) ;
          >
          > return dwRet ;
          > }
          >
          >
          > static BOOL CALLBACK TerminateAppEnu m( HWND hwnd, LPARAM lParam )
          > {
          > DWORD dwID ;
          >
          > GetWindowThread ProcessId(hwnd, &dwID) ;
          >
          > if(dwID == (DWORD)lParam)
          > {
          > PostMessage(hwn d, WM_CLOSE, 0, 0) ;
          > }
          >
          > return TRUE ;
          > }
          >
          > =============== =============== =============== ============
          >
          > All suggestion appreciated.
          >
          > Thanks
          >
          > GTS
          >
          >[/color]


          Comment

          • JJ

            #6
            Re: Terminating Process

            Sorry - I missed that.

            "Adam Clauss" <cabadam@tamu.e du> wrote in message
            news:O1wJqh4cEH A.996@TK2MSFTNG P12.phx.gbl...[color=blue]
            > "JJ" <jjjj@nospam.co m> wrote in message[/color]
            news:OhKPNs3cEH A.384@TK2MSFTNG P10.phx.gbl...[color=blue][color=green]
            > > Or a better idea would be to send WM_CLOSE, wait some time, then check[/color][/color]
            to[color=blue][color=green]
            > > see if the process still exists. If it does, then do TerminateProces s()[/color][/color]
            as[color=blue][color=green]
            > > a last resort.[/color]
            >
            > Isn't that what the original poster did?
            > He sends the windows of the process the WM_CLOSE, then does[/color]
            WaitForSingleOb ject on the process. If it times-out rather than[color=blue]
            > signals, he calls TerminateProces s.
            >
            > --
            > Adam Clauss
            > cabadam@tamu.ed u
            >[/color]


            Comment

            Working...