Solved

Closing a program with another program :)

Posted on 2004-10-28
169 Views
Last Modified: 2010-04-05
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
Question by:wildzero
    5 Comments
     
    LVL 5

    Accepted Solution

    by:
    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
     
    LVL 5

    Expert Comment

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

    killtask(programA.exe);

    Hypoviax
    0
     
    LVL 17

    Assisted Solution

    by:TheRealLoki
    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
     
    LVL 5

    Expert Comment

    by:Hypoviax
    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
     
    LVL 5

    Expert Comment

    by:Hypoviax
    Thanks,

    Hypoviax
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now