Solved

I want to Terminate NT process unconditionally

Posted on 2002-07-04
6
157 Views
Last Modified: 2010-04-13
Hi Guys

Does anyone know if there is a DLL version of KILL.EXE. I want to be able to kill a process unconditionally.

Reason: I'm creating software that consists of a number of COM/DCOM objects and if one falls over, I want to ensure the rest of them will close and vanish out of the process list.

TerminateProcess doesn't kill them, either that or I'm doing something wrong. I've got the ProcessIDs of the processes and calling TerminateProcess, but they are still in the task list.

It has to be a DLL call. I can incorporate KILL into the app, but that throws up loads of Command windows (not a pretty sight).

All help gratefully received.

Cheers
0
Comment
Question by:OldBob
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7130273
'TerminateProcess()' takes a process handle, not a PID - you have to call 'OpenProcess()' to obtain it, e.g.

HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, FALSE, PID);

TerminateProcess ( hProcess, 0);

CloseHandle ( hProcess);

BTW, there are also means to call 'kill.exe' without having a command prompt pop up...
0
 
LVL 1

Author Comment

by:OldBob
ID: 7130703
Cheers jkr

I'll give it a crack. Any idea how I would call Kill.exe without the cmd box appearing, (just as a secodary option)

Bob
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 7130730
ShellExecute ( NULL, "open", "kill.exe", "<PID>", NULL, SW_HIDE);

or

BOOL ExecuteAndWaitForCompletion    (   LPSTR   pszCmd)
{
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    BOOL                bRes;

    DWORD               dwCode  =   0;

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb           =   sizeof  (   STARTUPINFO);
    si.dwFlags      =   STARTF_USESHOWWINDOW;
    si.wShowWindow  =   SW_HIDE;

    bRes    =   CreateProcess   (   NULL,
                                    pszCmd,
                                    NULL,
                                    NULL,
                                    TRUE,
                                    NORMAL_PRIORITY_CLASS,
                                    NULL,
                                    NULL,
                                    &si,
                                    &pi
                                );

    WaitForSingleObject (   pi.hProcess,    INFINITE);

    GetExitCodeProcess  (   pi.hProcess,    &dwCode);

    CloseHandle (   pi.hProcess);
    CloseHandle (   pi.hThread);

    return  (   bRes);
}

Call it like

ExecuteAndWaitForCompletion ( "kill.exe <pid>");

:o)
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 86

Expert Comment

by:jkr
ID: 7130814
Oh, BTW, you might want to ask such questions in one of the programming topic areas to get a better response...
0
 
LVL 1

Author Comment

by:OldBob
ID: 7131167
No need to ask anyone else... :) It works a treat.

Cheers Mate.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7133104
>>No need to ask anyone else...

That was meant for the "future" - you will just get quicker responses in the Programming TAs, I just "found" your Q by chance :o)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Some of the SEO trends we might expect in 2017.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question