PB wrote:The return type is Long, because PureBasic uses longs by default if the user
doesn't set the type. So because it uses just "WinHandle" instead of another
type like "WinHandle.b" etc, it's a long.
A very valuable information, didn't found it in the help. Maybe it would make sense to state with Runprogram etc. that the parameter program is of type long?
I took your code and modified it to get a universal code snippet with least use of API functions (works great BTW:):
Code: Select all
;{ ==========================================================================
;| EndProgram(processID.l)
;| Closes the application that was started with RunProgram. In difference
;| to KillProgram it allows the program to shut down properly and save e.g.
;| settings. The parameter "processID" is obtained by ProgramID(<result of
;| RunProgram(app$,option$,path$, #PB_Program_Open)>)
;} ==========================================================================
Procedure EndProgram(processID.l)
;***** End a program started with Runprogram, but give chance to save settings!
If OpenProcess_(#PROCESS_ALL_ACCESS,0,processID)<>0
CloseHandle_(processHandle)
Repeat ;searcht through all windows
win=FindWindow_(0,0)
While win<>0
GetWindowThreadProcessId_(win,@pid.l) ; get processID of found window
If pid=processID : WinHandle=win : Break : EndIf
win=GetWindow_(win,#GW_HWNDNEXT)
Wend
Until WinHandle
PostMessage_(WinHandle,#WM_CLOSE,0,0) ; Now close the started application
EndIf
EndProcedure
app$="c:\winnt\notepad.exe" ; Full path needed!
handle.l=RunProgram(app$,"",GetPathPart(app$),#PB_Program_Open)
id.l = ProgramID(handle)
CloeProgram(handle)
Sleep_(2000) ; Give it a couple of seconds to open.
EndProgram(id);End started program
With that I a code snippet that I can store for later use.
BTW is there a database for storing those useful codesnippets? I know there is the codearchive which is updated more or less frequently (IMHO less frequently).