Automatic closing of application

Just starting out? Need help? Post your questions and find answers here.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Automatic closing of application

Post by Klonk »

Hi everyone,

I wrote an application starter (starts other programs using RunProgram).

I wanted to include a feature that all opened applications will ber closed when closing the launcher.

The only way I found is the following (using the windowhandle)

The procedure starting a program:

Code: Select all

Procedure RunProgramEx(filename$,parameter$,directory$)
  If Left(parameter$,1)<>" "
    parameter$=" "+parameter$
  EndIf
  Info.STARTUPINFO
  Info\cb=SizeOf(STARTUPINFO)
  Info\dwFlags=1
  Info\wShowWindow=#SW_SHOWNORMAL
  ProcessInfo.PROCESS_INFORMATION
  If UCase(GetExtensionPart(filename$))="EXE"
    If CreateProcess_(@filename$,@parameter$,0,0,0,#NORMAL_PRIORITY_CLASS ,0,@directory$,@Info,@ProcessInfo)
      ProcessID=ProcessInfo\dwProcessId
      Delay(100)
      WaitForInputIdle_(ProcessInfo\hProcess, 30000) ;wait for application to accept input, wait maximum 30 seconds
      count = 0
      Repeat
        Delay(100) ;avoid hanging of windows during start of application while searching for the window
        count=count + 1 ;security counter to avoid hanging
        win=FindWindow_(0,0)
        While win<>0
          GetWindowThreadProcessId_(win,@pid.l)
          If pid=ProcessID
            If GetParent_(win)=0 ;save handle only if not a child window
              WinHandle=win
              Break
            EndIf
          EndIf
          win=GetWindow_(win,#GW_HWNDNEXT) ;check next window
        Wend
      Until WinHandle Or (count=100) ;wait up to 10 seconds for window to occur
    EndIf
  Else
    RunProgram(filename$,parameter$,directory$)
    WinHandle = 0 ;just for security
  EndIf
  ProcedureReturn WinHandle
EndProcedure
Winhandle is defined as long

For closing the started application I do:

Code: Select all

               PostMessage_(WinHandle,#WM_CLOSE,0,0); close Window
The problem is: Sometimes it work, most of the time it does not.

Is anyone aware of some other possibility? Any ideas.

Thanks a million.
Bye Karl
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Automatic closing of application

Post by Dude »

Try this instead -- it's what I use for apps that don't obey #WM_CLOSE:

Code: Select all

PostMessage_(hWnd,#WM_SYSCOMMAND,#SC_CLOSE,0)
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Automatic closing of application

Post by Bisonte »

Winhandle is defined as long
Use Integer for OS handles ! Ever. Also in x86 programs.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Re: Automatic closing of application

Post by Klonk »

@Dude:

The result of Postmessage can be checked, right?

So the following should work?

Code: Select all

               result=PostMessage_(LaunchApp()\hWnd,#WM_CLOSE,0,0); close Window
               If result = false then
                 PostMessage_(LaunchApp()\hWnd,#WM_SYSCOMMAND,#SC_CLOSE,0); close window, 2nd try
               End If
Bye Karl
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Automatic closing of application

Post by RSBasic »

Please use SendMessage_().
Image
Image
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Re: Automatic closing of application

Post by Klonk »

Sorry for the maybe dumb question, but why sendmessage?

Sendmessage waits for the result, Postmessage not. Is this the reason?

If it does not wait I cannot look at the result?
Bye Karl
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Automatic closing of application

Post by RSBasic »

Klonk wrote:Sendmessage waits for the result, Postmessage not. Is this the reason?
Yes
Image
Image
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Automatic closing of application

Post by Lunasole »

Well can also close with built-in function, but it is wrapper over TerminateProcess API. Closing this way works always (except very rare cases if process protected), but it also causes loss of unsaved data in launched programs.

Code: Select all

EnableExplicit

; run & kill process after 10s
Define hProgram = RunProgram("calc.exe", "", "", #PB_Program_Open)

Repeat
	Delay(10 * 1000)
	KillProgram(hProgram)
	CloseProgram(hProgram)
	Break
ForEver
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Automatic closing of application

Post by Dude »

Hi Klonk! I meant that I personally just use the #WM_SYSCOMMAND and #SC_CLOSE version instead of the other #WM_CLOSE version, as it seems to work every time compared to #WM_CLOSE not working with all windows. I haven't used #WM_CLOSE to close a window in 10 years, and never had a problem with #SC_CLOSE doing it. It's still an official way to send a close message.

From https://stackoverflow.com/questions/101 ... d-sc-close :

"WM_CLOSE is sent as a window message whenever the window is requested to be closed, by any means. SC_CLOSE is sent as a parameter of a WM_SYSCOMMAND message, when the user presses the Close button (or selects Close from the control menu of the window). Which one you listen for is determined by which action(s) you attempting to intercept/deal with."

So I've been using #SC_CLOSE without issue. For example, sending it to an unsaved Notepad window will still make Notepad prompt the user to save the file first, etc.

Here's some actual code from one of my apps, with my commentary on why I started using it:

Code: Select all

;PostMessage_(hWnd,#WM_CLOSE,0,0) ; Fails TOTALLY on admin windows. Avoid!
If PostMessage_(hWnd,#WM_SYSCOMMAND,#SC_CLOSE,0) ; Works on MOST admin windows.
  ; ...
EndIf
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Re: Automatic closing of application

Post by Klonk »

Ah, ok, many thanks...
Bye Karl
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Automatic closing of application

Post by Michael Vogel »

Lunasole wrote:Well can also close with built-in function, but it is wrapper over TerminateProcess API. Closing this way works always (except very rare cases if process protected), but it also causes loss of unsaved data in launched programs.
I have some cases where I can't close a running program window, here's an example...

Code: Select all

Procedure RunCommand(command.s)

	Protected handle
	Protected timeout

	timeout=ElapsedMilliseconds()+1000

	handle=RunProgram("cmd.exe","/c "+command,GetTemporaryDirectory(),#PB_Program_Open)
	;handle=RunProgram("cmd.exe","/c "+command,GetTemporaryDirectory(),#PB_Program_Open|#PB_Program_Read)

	If handle
		While ProgramRunning(handle) And timeout>ElapsedMilliseconds()
			Debug "*"
			Delay(100)
		Wend
		Debug "TRY TO KILL"
		Debug IsProgram(handle)
		Debug KillProgram(handle)
		Debug IsProgram(handle)
		Debug CloseProgram(handle)
		Debug IsProgram(handle)

	EndIf

	Debug "done"

EndProcedure

;RunCommand("net start bthserv")
RunCommand("net stop bthserv")
Debug "..."
Delay(3000)
Debug "QUIT"
Post Reply