• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Closing a program with another program :)

Hey there,

I have a program that i've made, I want to make another program which will close that first program.
So program A - main program
program B - second program

Program A is opened - whenever, at system startup user startup....
They can then open program B at any stage and terminate program A.

Make sense?

0
wildzero
Asked:
wildzero
  • 4
2 Solutions
 
HypoviaxCommented:
This code can kill a program by exename (programA.exe):

function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(OpenProcess(
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0));
ContinueLoop := Process32Next(FSnapshotHandle,
FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

If you don't know the name of the program or need more detail have a look at my solution to a similar question:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21164286.html

Regards,

Hypoviax
0
 
HypoviaxCommented:
i.e you would use it like this in program b:

killtask(programA.exe);

Hypoviax
0
 
TheRealLokiSenior DeveloperCommented:
var
              ahandle := FindWindow('Notepad', 'readme.txt - Notepad');
              if aHandle <> 0 then
                sendmessage(aHandle,wm_Close,0,0);

this will find the window (classname, windowname)
and send it a close message....
nice simple way of closing

e.g.

0
 
HypoviaxCommented:
or you could do this:

postmessage(findwindow(nil,'WINDOWCAPTION'),wm_close,0,0);

which is even simpler

But the killtask procedure is perhaps more useful if you want to terminate by exename

Regards,

Hypoviax
0
 
HypoviaxCommented:
Thanks,

Hypoviax
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now