Article View: borland.public.cppbuilder.nativeapi
Article #1446Re: How to terminate another program from my application?
From: 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] );
}
Technical Details
Message-ID:
<46d4178d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!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>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ea27fed4f668a6eb X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Maurice" <morisaab@yahoo.fr> Newsgroups: borland.public.cppbuilder.nativeapi References: <46d17858@newsgroups.borland.com> <46d19c99$1@newsgroups.borland.com> <46d1dde9@newsgroups.borland.com> <46d1deff@newsgroups.borland.com> <46d1f127$1@newsgroups.borland.com> Subject: Re: How to terminate another program from my application? Date: Tue, 28 Aug 2007 15:39:32 +0300 Lines: 95 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 193.227.176.204 Message-ID: <46d4178d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1188304781 193.227.176.204 (28 Aug 2007 05:39:41 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail