Solved

I want to Terminate NT process unconditionally

Posted on 2002-07-04
6
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

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.

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…
The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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