Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

the command to shutdown or close active application

Posted on 2004-10-14
7
Medium Priority
?
339 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 80 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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