Article View: borland.public.cppbuilder.nativeapi
Article #1352Re: How to terminate another program from my application?
From: Colin B Maharaj
Date: Wed, 29 Aug 2007 23:55
Date: Wed, 29 Aug 2007 23:55
197 lines
6827 bytes
6827 bytes
OK OK, I am seeing your cry for help. :)
I have some code here I did some time ago.
I am cutting and pasting it. You know the drill
no guarantees and remove global variables in
production use, not efficient etc......
Sorry for the mess but I think it works.
And be careful what you terminate!
PS: Call The last function ......
bool AttemptTerminate(char *name);
// Name is module in memory e.g. AttemptTerminate("MyProg.exe");
//---------------------------------------------------------------------
#include <psapi.h>
typedef BOOLEAN __stdcall (*PEnumProcessModules)(HANDLE , HMODULE *,
DWORD , LPDWORD );
PEnumProcessModules _EnumProcessModules;
typedef BOOLEAN __stdcall (*PGetModuleBaseName)(HANDLE , HMODULE ,
LPSTR , DWORD);
PGetModuleBaseName _GetModuleBaseName;
//---------------------------------------------------------------------
void GetTheProcessName(DWORD processID, char * PN)
{
HINSTANCE hDll=LoadLibrary("Psapi.dll");
_EnumProcessModules=(PEnumProcessModules)GetProcAddress(hDll,"EnumProcessModules");
_GetModuleBaseName=(PGetModuleBaseName)GetProcAddress(hDll,"GetModuleBaseNameA");
TCHAR szProcessName[MAX_PATH];
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( _EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
_GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
CloseHandle( hProcess );
strcpy(PN, szProcessName);
FreeLibrary(hDll);
}
//---------------------------------------------------------------------
int theProcessIDReturned;
char theProcessName[256];
//---------------------------------------------------------------------
bool __stdcall EnumProc(void * hWnd, long)
{
unsigned long* pPid; //LPDWORD
unsigned long result; //DWORD
void *hg; //HGLOBAL
unsigned long id;
if(hWnd==NULL) return false;
hg = GlobalAlloc(GMEM_SHARE,sizeof(unsigned long));
pPid = (unsigned long *)GlobalLock(hg);
result = GetWindowThreadProcessId(hWnd,pPid);
if(result)
{
char title[110];
char ProcessName[512];
char className[95];
char totalStr[256];
GetClassName(hWnd,className,95);
GetWindowText(hWnd,title,110);
id=*pPid;
GetTheProcessName(id, ProcessName);
itoa(id,totalStr,10);
strcat(totalStr,"\t");
if(title)
{
strcat(totalStr,title);
strcat(totalStr,"\t");
}
strcat(totalStr,className);
if (strcmp(className, "TApplication")==0)
{
if (strcmpi(ProcessName, theProcessName)==0)
{
theProcessIDReturned = id;
}
}
}
else
{
GlobalUnlock(hg);
GlobalFree(hg);
return false;
}
GlobalUnlock(hg);
GlobalFree(hg);
return true;
}
//---------------------------------------------------------------------
int GetProcessID(char *exe)
{
strcpy(theProcessName, exe);
long lp=0;
theProcessIDReturned = 0;
EnumWindows((WNDENUMPROC)EnumProc,lp);
return theProcessIDReturned;
}
//---------------------------------------------------------------------
bool DoYesNo(char *mes, char *confirm)
{
int button;
AnsiString A;
//if (bDeleteConfirm == false) return false;
A = (AnsiString)mes;
button = Application->MessageBox(A.c_str(), confirm,
MB_YESNO +
MB_DEFBUTTON2 +
MB_ICONEXCLAMATION
);
if (button == IDYES) return true;
if (button == IDNO) return false;
return true;
}
//---------------------------------------------------------------------
bool AttemptTerminate(char *name)
{
int pid = GetProcessID(name);
AnsiString A = "ProcessID of ";
A += name;
if (pid == 0)
{
A += " could not be found!\nProbably DCOM module";
Application->MessageBox(A.c_str(), "Process Error",
MB_OK + MB_ICONSTOP + MB_DEFBUTTON1);
return false;
}
A += " : ";
A+= IntToStr(pid);
A += "\r\nAttempt to terminate this running process";
bool r = DoYesNo(A.c_str(), "Attempt Termination");
if (r == true)
{
HANDLE ps = OpenProcess(1,false,pid);
if(ps)
{
if(!TerminateProcess(ps,-9))
{
Application->MessageBox("Could not end process specified!",
"Termination Error",
MB_OK + MB_ICONSTOP + MB_DEFBUTTON1);
}
else
{
Application->MessageBox("Successfully ended process specified!",
"Termination successful",
MB_OK + MB_ICONINFORMATION +
MB_DEFBUTTON1);
}
}
else
{
Application->MessageBox("Could not open process requested!",
"Process Error",
MB_OK + MB_ICONSTOP + MB_DEFBUTTON1);
}
}
return false;
}
//---------------------------------------------------------------------
Technical Details
Message-ID:
<46d6076d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<46d17858@newsgroups.borland.com> <46d19c99$1@newsgroups.borland.com> <46d1dde9@newsgroups.borland.com> <46d1deff@newsgroups.borland.com> <46d1f127$1@newsgroups.borland.com> <46d4178d@newsgroups.borland.com> <jcb8d3lj5oke59jhgfapojavmul4jqjapq@4ax.com> <46d572d9@newsgroups.borland.com> <u24bd3drqmfa3llhja42hun4l5u3qvut5f@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ea27fed4f668a6eb X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Wed, 29 Aug 2007 19:55:26 -0400 From: Colin B Maharaj <noreply@myhost.com> User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: How to terminate another program from my application? References: <46d17858@newsgroups.borland.com> <46d19c99$1@newsgroups.borland.com> <46d1dde9@newsgroups.borland.com> <46d1deff@newsgroups.borland.com> <46d1f127$1@newsgroups.borland.com> <46d4178d@newsgroups.borland.com> <jcb8d3lj5oke59jhgfapojavmul4jqjapq@4ax.com> <46d572d9@newsgroups.borland.com> <u24bd3drqmfa3llhja42hun4l5u3qvut5f@4ax.com> In-Reply-To: <u24bd3drqmfa3llhja42hun4l5u3qvut5f@4ax.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 000769-2, 08/29/2007), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 201.238.90.4 Message-ID: <46d6076d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1188431725 201.238.90.4 (29 Aug 2007 16:55:25 -0700) Lines: 203 Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail