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?

LVL 10
wildzeroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.