Thread View: borland.public.cppbuilder.nativeapi
11 messages
11 total messages
Started by Bashar Termanini
Sun, 26 Aug 2007 12:55
How to terminate another program from my application?
Author: Bashar Termanini
Date: Sun, 26 Aug 2007 12:55
Date: Sun, 26 Aug 2007 12:55
9 lines
1060 bytes
1060 bytes
Hi How to terminate another program from my application? The other program has no window (no title) I only no his name like 123.exe
Re: How to terminate another program from my application?
Author: Ed Mulroy [TeamB]
Date: Sun, 26 Aug 2007 15:30
Date: Sun, 26 Aug 2007 15:30
14 lines
1528 bytes
1528 bytes
The Windows API function TerminateProcess can do that. It is a dangerous thing to use as it can leave DLLs and other things in memory and in an odd state. Look at the notes for it in the help file. . Ed > Bashar Termanini wrote in message > news:46d17858@newsgroups.borland.com... > > How to terminate another program from my application? > > The other program has no window (no title) I only no his name like > 123.exe
Re: How to terminate another program from my application?
Author: Dennis Jones
Date: Sun, 26 Aug 2007 16:05
Date: Sun, 26 Aug 2007 16:05
10 lines
1265 bytes
1265 bytes
"Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> wrote in message news:46d19c99$1@newsgroups.borland.com... > The Windows API function TerminateProcess can do that. > > It is a dangerous thing to use... Oh no, not again! :-) - Dennis
Re: How to terminate another program from my application?
Author: Bashar Termanini
Date: Sun, 26 Aug 2007 20:09
Date: Sun, 26 Aug 2007 20:09
3 lines
1166 bytes
1166 bytes
how can I get the handle of the running program? I only know hi name "123.exe"
Re: How to terminate another program from my application?
Author: Michel Corbin
Date: Sun, 26 Aug 2007 20:14
Date: Sun, 26 Aug 2007 20:14
10 lines
1225 bytes
1225 bytes
Hi!
HANDLE hd=GetCurrentProcess() ;
Bashar Termanini wrote :
> how can I get the handle of the running program?
>
> I only know hi name "123.exe"
>
>
Re: How to terminate another program from my application?
Author: Dennis Jones
Date: Sun, 26 Aug 2007 21:31
Date: Sun, 26 Aug 2007 21:31
18 lines
1685 bytes
1685 bytes
"Michel Corbin" <"Michel Corbin"> wrote in message news:46d1deff@newsgroups.borland.com... > Hi! > > HANDLE hd=GetCurrentProcess() ; That won't work for *another* application (the OP asked how to get the handle of another application given only its name). The following article on MSDN should help: http://msdn2.microsoft.com/en-us/library/ms682623.aspx It uses EnumProcesses and GetModuleBaseName (both of which are available in the process status API, psapi) to display the name of currently executing processes in the system. - Dennis
Re: How to terminate another program from my application?
Author: Maurice
Date: Tue, 28 Aug 2007 12:39
Date: Tue, 28 Aug 2007 12:39
92 lines
3837 bytes
3837 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message
news:46d1f127$1@newsgroups.borland.com...
>
> "Michel Corbin" <"Michel Corbin"> wrote in message
> news:46d1deff@newsgroups.borland.com...
>> Hi!
>>
>> HANDLE hd=GetCurrentProcess() ;
>
> That won't work for *another* application (the OP asked how to get the
> handle of another application given only its name).
>
> The following article on MSDN should help:
> http://msdn2.microsoft.com/en-us/library/ms682623.aspx
>
> It uses EnumProcesses and GetModuleBaseName (both of which are available
> in
> the process status API, psapi) to display the name of currently executing
> processes in the system.
>
> - Dennis
>
>
Hello:
Thanks for the help!!
I tried the mentionned (hereafter) program, and I tried to compile it with
the free command line tool:
bcc32 -6 -ff -N e:\my_data\prog\tc\process.cpp
I get the following error:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'EnumProcesses' referenced from
C:\COMPILER\BC55\BIN\
PROCESS.OBJ
Error: Unresolved external 'EnumProcessModules' referenced from
C:\COMPILER\BC55
\BIN\PROCESS.OBJ
Error: Unresolved external 'GetModuleBaseNameA' referenced from
C:\COMPILER\BC55
\BIN\PROCESS.OBJ
Any help?
Thank you
Maurice
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>
void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
// Print the process name and identifier.
_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for ( i = 0; i < cProcesses; i++ )
if( aProcesses[i] != 0 )
PrintProcessNameAndID( aProcesses[i] );
}
Re: How to terminate another program from my application?
Author: Bob Gonder
Date: Tue, 28 Aug 2007 14:15
Date: Tue, 28 Aug 2007 14:15
12 lines
1648 bytes
1648 bytes
Maurice wrote: >I tried the mentionned (hereafter) program, and I tried to compile it with >the free command line tool: >bcc32 -6 -ff -N e:\my_data\prog\tc\process.cpp > >I get the following error: >Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland >Error: Unresolved external 'EnumProcesses' referenced from psapi.lib is not one of the standard included libraries. You need to explicitly include it.
Re: How to terminate another program from my application?
Author: Maurice
Date: Wed, 29 Aug 2007 13:19
Date: Wed, 29 Aug 2007 13:19
46 lines
2801 bytes
2801 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message news:jcb8d3lj5oke59jhgfapojavmul4jqjapq@4ax.com... > Maurice wrote: > >>I tried the mentionned (hereafter) program, and I tried to compile it with >>the free command line tool: >>bcc32 -6 -ff -N e:\my_data\prog\tc\process.cpp >> >>I get the following error: >>Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland >>Error: Unresolved external 'EnumProcesses' referenced from > > psapi.lib is not one of the standard included libraries. > You need to explicitly include it. > > I tried the following (in the bc55\bin directory): bcc32 -6 -ff -N -c e:\my_data\prog\tc\process.cpp, to generate an obj, it's OK, then: ilink32 process.obj, ..\lib\psdk\psapi.lib I get: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Fatal: Unable to open file 'UUID.LIB' then I tried: ilink32 process.obj, ..\lib\psdk\psapi.lib, ..\lib\uuid.lib Iget: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Fatal: Unable to open file 'UUID.LIB' I verified "uuid.lib" exist in the lib directory, however I copied it to the bin directory and tried: ilink32 process.obj, ..\lib\psdk\psapi.lib, uuid.lib I get nothing, no process.exe generated, but "uuid.lib" has changed (size and date), if I typed the same commnad "ilink32 process.obj, ..\lib\psdk\psapi.lib, uuid.lib" a second time, I get: Error: 'C:\COMPILER\BC55\BIN\UUID.LIB' contains invalid OMF record, type 0x4d I'm really interested by that program, any help?? thanks Maurice
Re: How to terminate another program from my application?
Author: Bob Gonder
Date: Wed, 29 Aug 2007 15:35
Date: Wed, 29 Aug 2007 15:35
12 lines
1623 bytes
1623 bytes
Maurice wrote: >ilink32 process.obj, ..\lib\psdk\psapi.lib, ..\lib\uuid.lib > >I'm really interested by that program, any help?? You should first read the docs on how to use the linker. You also need switches to tell the linker what to do, where to find things, and many more libs. Beginner lesson here.. http://mulroy.org/howto.htm
Re: How to terminate another program from my application?
Author: 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;
}
//---------------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads