Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use API to close a program?

Posted on 1999-07-16
3
Medium Priority
?
564 Views
Last Modified: 2013-12-03
You can close a program by pressing the Ctrl-Alt-Del.
How can I close a program by Win32 API?
0
Comment
Question by:lippo
[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
3 Comments
 

Accepted Solution

by:
irishka earned 200 total points
ID: 1403072
Use function
BOOL TerminateProcess(
    HANDLE  hProcess,      // handle to the process
    UINT  uExitCode       // exit code for the process  
   );      

Example for Win95:
------------------------------------------------------------
#include <windows.h>
#include <tlhelp32.h>

struct PINFO
{
 DWORD dwPriorityClass;
 LONG  pcPriClassBase;
 DWORD pid;
 DWORD cntThreads;
 char  szFullPath[256];
 char  szModName[64];
};


BOOL KillProcess(char *);
BOOL GetProcessModule (DWORD, DWORD, LPMODULEENTRY32, DWORD);

void main(){


KillProcess("CALC.EXE");
}




BOOL KillProcess(char* ModName)
{
    HANDLE         hProcessX;
    HANDLE         hSnapshot = NULL;
    BOOL           bRet      = FALSE;
    PROCESSENTRY32 pe32      = {0};
    HANDLE         hProcessSnap;
    //  Take a snapshot of all processes currently in the system.
    //
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == (HANDLE)-1)
       return FALSE;

    //  Fill in the size of the structure before using it.
    pe32.dwSize = sizeof(PROCESSENTRY32);

    //  Walk the snapshot of the processes, and for each process, get

    //  information to display.
    if (Process32First(hProcessSnap, &pe32)) {
        BOOL          bGotModule = FALSE;
        MODULEENTRY32 me32       = {0};
        PINFO         pi         = {0};
        bRet = FALSE;
        do {
            bGotModule = GetProcessModule(pe32.th32ProcessID,
                pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));
            if (bGotModule) {
                HANDLE hProcess;

                // Get the actual priority class.

                hProcess = OpenProcess (PROCESS_ALL_ACCESS,
                    FALSE, pe32.th32ProcessID);

                pi.dwPriorityClass = GetPriorityClass (hProcess);

                if( strcmp(me32.szModule,ModName)==NULL)
                 {
                  TerminateProcess(hProcess,0);
                 }
                CloseHandle (hProcess);
            }
        }
        while (Process32Next(hProcessSnap, &pe32));
    }
    else
    bRet=FALSE;    // could not walk the list of processes

    // Do not forget to clean up the snapshot object.
    CloseHandle (hProcessSnap);
    return bRet;
}

BOOL GetProcessModule (DWORD dwPID, DWORD dwModuleID,
        LPMODULEENTRY32 lpMe32, DWORD cbMe32)
{
    BOOL          bRet        = FALSE;
    BOOL          bFound      = FALSE;
    HANDLE        hModuleSnap = NULL;
    MODULEENTRY32 me32        = {0};

    // Take a snapshot of all modules in the specified process.
    hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
    if (hModuleSnap == (HANDLE)-1)

        return (FALSE);

    // Fill the size of the structure before using it.
    me32.dwSize = sizeof(MODULEENTRY32);

    // Walk the module list of the process, and find the module of
    // interest. Then copy the information to the buffer pointed
    // to by lpMe32 so that it can be returned to the caller.
    if (Module32First(hModuleSnap, &me32)) {
        do {
            if (me32.th32ModuleID == dwModuleID) {
                CopyMemory (lpMe32, &me32, cbMe32);
                bFound = TRUE;
            }
        }
        while (!bFound && Module32Next(hModuleSnap, &me32));

        bRet = bFound;   // if this sets bRet to FALSE, dwModuleID
                         // no longer exists in specified process
    }
    else
        bRet = FALSE;           // could not walk module list

    // Do not forget to clean up the snapshot object.
    CloseHandle (hModuleSnap);

    return (bRet);
}




0
 

Expert Comment

by:caiger
ID: 1403073
There are several ways thay you can close a program.

The first legal way is to post the main window a message telling it to close down

PostMessage(hMainWindow, WM_CLOSE, 0, 0L);

or the more illegal way is to terminate the process using TerminateProcess.

However for both of these methods you either need to main window handle, or the process id
0
 

Expert Comment

by:dong9968
ID: 10064358
I ran the example on my machine, but it doesn't work. Why? My machine is windows xp.

Thank you!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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