Solved

the command to shutdown or close active application

Posted on 2004-10-14
7
312 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 82
delphi parse string to params 3 81
Create a path if not exists 7 41
Base1 Encode/Decode 3 29
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

12 Experts available now in Live!

Get 1:1 Help Now