Solved

the command to shutdown or close active application

Posted on 2004-10-14
7
327 Views
Last Modified: 2010-04-05

Please show me the command to shutdow window. that command for shutdown depends on version of window ?

Or the command to close the active application on window.

Because i want something to appear when they use my program but not register
0
Comment
Question by:namcit99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 20 total points
ID: 12316664
send the window a WM_QUIT or WM_Close message... e.g.

PostMessage(GetActiveWindow, WM_QUIT, 0, 0);
0
 

Author Comment

by:namcit99
ID: 12316761

It is Ok.

I just read the document about this command at Windows SDK in Delphi 7 Document.

Is it enough ?  I do not see the 'GetactiveWindow' . Where could i read more about this command
0
 
LVL 3

Expert Comment

by:smot
ID: 12317609
The GetActiveWindow function retrieves the window handle to the active window attached
to the calling thread's message queue.

For further information go to:
http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/GetActiveWindow.asp

- Or the command to close the active application on window.

procedure KillProcess(hWindowHandle: HWND);
var
  hprocessID: INTEGER;
  processHandle: THandle;
  DWResult: DWORD;
begin
  SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,
    SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);

  if isWindow(hWindowHandle) then
  begin
    // PostMessage(hWindowHandle, WM_QUIT, 0, 0);

    { Get the process identifier for the window}
    GetWindowThreadProcessID(hWindowHandle, @hprocessID);
    if hprocessID <> 0 then
    begin
      { Get the process handle }
      processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,
        False, hprocessID);
      if processHandle <> 0 then
      begin
        { Terminate the process }
        TerminateProcess(processHandle, 0);
        CloseHandle(ProcessHandle);
      end;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  KillProcess(FindWindow('notepad',nil));
end;

- Please show me the command to shutdow window.

Do u mean shutdown Windows??

function MyExitWindows(RebootParam: Longword): Boolean;
var
  TTokenHd: THandle;
  TTokenPvg: TTokenPrivileges;
  cbtpPrevious: DWORD;
  rTTokenPvg: TTokenPrivileges;
  pcbtpPreviousRequired: DWORD;
  tpResult: Boolean;
const
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    tpResult := OpenProcessToken(GetCurrentProcess(),
      TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
      TTokenHd);
    if tpResult then
    begin
      tpResult := LookupPrivilegeValue(nil,
                                       SE_SHUTDOWN_NAME,
                                       TTokenPvg.Privileges[0].Luid);
      TTokenPvg.PrivilegeCount := 1;
      TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      cbtpPrevious := SizeOf(rTTokenPvg);
      pcbtpPreviousRequired := 0;
      if tpResult then
        Windows.AdjustTokenPrivileges(TTokenHd,
                                      False,
                                      TTokenPvg,
                                      cbtpPrevious,
                                      rTTokenPvg,
                                      pcbtpPreviousRequired);
    end;
  end;
  Result := ExitWindowsEx(RebootParam, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyExitWindows(EWX_POWEROFF or EWX_FORCE);
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12318085
procedure TForm1.Button1Click(Sender: TObject);
var
  AA_Handle    : HWND;  // Active Application Handle
begin
  // get the handle
  AA_Handle := GetActiveWindow;
  // close it
  SendMessage(AA_Handle, WM_CLOSE, 0, 0);
end;
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12318104
If you want to know when the system is being rebooted/shut down, you should handle WM_QUERYENDSESSION and WM_ENDSESSION which are sent to all running applications...
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12331029
Haha,

Are we talking about ahutting down windows, closing windows (as in program windows) or closing apps?

For Closing programwindows:

          postmessage(findwindow(nil,'The Window Caption here'),wm_close,0,0);

For Shutting down windows:

          windows.InitiateSystemShutdown(pchar('127.0.0.1'),pchar('Windows in shutting down'),1,false,false);

For Closing applications:

           function KillTask(ExeFileName: string): integer;
 const  //Kills a process by exename
 PROCESS_TERMINATE=$0001;
 var
 ContinueLoop: BOOLEAN;
 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;

Example:

killtask('notepad.exe');

Best Regards,

Hypoviax
0
 

Author Comment

by:namcit99
ID: 12336079

Thank for all consider
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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