Solved

the command to shutdown or close active application

Posted on 2004-10-14
7
318 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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