Solved

I want to Terminate NT process unconditionally

Posted on 2002-07-04
6
153 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote Desktop Software 6 449
windows explorer 21 177
testing the trust relationship between two domain 1 80
Windows 2000 unable to connect to the internet by ethernet cable 14 88
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…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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