Article View: borland.public.cppbuilder.nativeapi
Article #695Re: How to terminate another program from my application?
From: Mads Mølgaard Andersen
Date: Tue, 26 Feb 2008 09:17
Date: Tue, 26 Feb 2008 09:17
222 lines
7612 bytes
7612 bytes
Hi
I'm writing a similar application where I want to kill another specific
application from my application.
I get the processID through the enumerattion, I get the process handle,
I call GetExitProcessCode to get the process' exitcode but when I call
TerminateProcess with the handle and the exitcode it always return FALSE.
Then to my surprise I see that you've hardcoded "-9" as exitcode. Is
there something I should know here ?
Best regards
Mads M Andersen
Colin B Maharaj skrev:
> 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:
<47c3d946$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!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> <46d6076d$1@newsgroups.borland.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: Tue, 26 Feb 2008 10:17:58 +0100 From: Mads Mølgaard Andersen <helle.mads@gmail.com> User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) 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> <46d6076d$1@newsgroups.borland.com> In-Reply-To: <46d6076d$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 212.88.71.2 Message-ID: <47c3d946$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1204017478 212.88.71.2 (26 Feb 2008 01:17:58 -0700) Lines: 222 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail