how can I stop the program execution of RunProgram("Robocopy",...) before it ends regularly.
On console it works by ctrl+c
in my test code there are three ideas - none of them are working (?)
if you want to check them out play with the comment/uncomment feature. See the lines ;' TEST-1 to ;' TEST-3
I read about killprogram() but i am not really sure about that. Mayby someone can confirm that it is a proper way to cancel the execution.
Any kind of information will be helpful.
Thanks in Advance.
Here is the code:
Code: Select all
EnableExplicit
;DebugLevel 9
;' Robocopy Constants
#ROBOCOPY_Exec$ = "c:\windows\system32\robocopy.exe"
#ROBOCOPY_ProgramFlags = #PB_Program_Open|#PB_Program_Read|#PB_Program_Write ;|#PB_Program_Hide
#ROBOCOPY_ConsoleClass$ = "ConsoleWindowClass"
;' Global Variables -- init to existing directories
Global SourceDir$ = RTrim(#PB_Compiler_Home, "\") ;' directory with a lot of files and folders
Global DestDir$ = RTrim(GetTemporaryDirectory(), "\") ;' some place to copy to
Procedure.s FormatMessage(ErrorCode) ;' returns 'message from system according to errorcode'
Protected result.s, *buf, buflen
buflen = FormatMessage_(#FORMAT_MESSAGE_ALLOCATE_BUFFER | #FORMAT_MESSAGE_FROM_SYSTEM, 0, ErrorCode, 0, @*buf, 0, 0)
If buflen
result = PeekS(*buf, buflen-2)
LocalFree_(*buf)
result + " " + Str(ErrorCode) + ", 0x" + RSet(Hex(ErrorCode), 8, "0")
EndIf
ProcedureReturn result
EndProcedure ;FormatMessage()
Procedure.s GetClassName(hWnd) ;' returns classname (string)
Protected Classname${256}
GetClassName_(hWnd, @Classname$, 256)
ProcedureReturn Classname$
EndProcedure ;()
Procedure.s GetCaption(hWnd) ;' returns classname (string)
Protected txt${256}
GetWindowText_(hWnd, @txt$, 256)
ProcedureReturn txt$
EndProcedure ;()
Procedure RunRobocopy() ;' the main procedure of this demo code
Protected PrgID, DataSize, bytes, rc, count , hWnd, value
Protected *MemBuf, MemSize = 1024
Protected param$, st$
SourceDir$ = #DQUOTE$+RTrim(SourceDir$, "\")+#DQUOTE$ :Debug "SourceDir = '"+SourceDir$+"'"
DestDir$ = #DQUOTE$+RTrim(DestDir$ , "\")+#DQUOTE$ :Debug "DestDir = '"+DestDir$+"'"
param$ = SourceDir$ + " " + DestDir$ + " *.* /E /COPY:DAT /R:1 /W:1 " ;' add some options
param$ + " /L" ;' list all files only - no copy action will take place
Debug " Parameters = '"+param$+"'"
*MemBuf = AllocateMemory(MemSize, #PB_Memory_NoClear)
If *MemBuf
PrgID = RunProgram(#ROBOCOPY_Exec$, param$, "", #ROBOCOPY_ProgramFlags)
If PrgID <> 0 :Debug #LF$+"start"
While ProgramRunning(PrgID)
DataSize = AvailableProgramOutput(PrgID)
If DataSize > 0 :Debug " datasize = "+DataSize, 9
While DataSize > MemSize
bytes = ReadProgramData(PrgID, *MemBuf, MemSize) :Debug " Bytes0:"+Str(bytes), 9
WriteConsoleData(*MemBuf, MemSize)
DataSize - MemSize
Wend
bytes = ReadProgramData(PrgID, *MemBuf, DataSize) :Debug " Bytes1:"+Str(bytes), 9
WriteConsoleData(*MemBuf, DataSize)
EndIf
Delay(10) ;' give the force to someone else
count + 1 ;' simulate the user wish
If count = 10 :Debug "Force an user requested stop (at count ="+count+")"
;' TEST-1 ;---------------------------------------------------------------------------------
rc = WriteProgramData(PrgID, #PB_Program_Eof, 0) :Debug " --> Write EOF -- written=" +rc
; ;' TEST-2 ;---------------------------------------------------------------------------------
; ; hWnd = FindWindow_(0, #ROBOCOPY_Exec$) :Debug " found hWnd = "+hWnd
; hWnd = FindWindow_(#ROBOCOPY_ConsoleClass$, 0) :Debug " found hWnd = "+hWnd
; If hWnd
; Debug " Caption = "+GetCaption(hWnd)
; Debug " Classname = "+GetClassName(hWnd)
; Debug ""
; value = 1 ;' get the ProcessID back ....
; Debug " GetWindowThreadProcessID = "+GetWindowThreadProcessId_(hWnd, @value)
; Debug " -> ProcessID = "+value
;
; ; SendMessage_(hWnd, #WM_CLOSE, 0, 0) :Debug "Send WM_CLOSE "
; EndIf
; ;' TEST-3 ;---------------------------------------------------------------------------------
; If GenerateConsoleCtrlEvent_(#CTRL_C_EVENT, ProgramID(PrgID)) :Debug " --> send Ctrl+C "
; rc = GetLastError_()
; Debug "error " + FormatMessage(rc)
; EndIf
EndIf ;' count = 10
Wend ;' ProgramRunning()
CloseProgram(PrgID)
Debug #LF$+"count = "+count ;' should be 10 if successfully
Debug "stop"
EndIf ;PrgID <> 0
FreeMemory(*MemBuf)
EndIf ; *MemBuf
ProcedureReturn 0
EndProcedure ;()
;' Main Program -- starts here
If OpenConsole()
ConsoleTitle ("PureBasic - Console Robocopy Test")
PrintN("Robocopy started ")
RunRobocopy()
PrintN("Robocopy done ")
CompilerIf Not #PB_Editor_CreateExecutable ;' at development time
PrintN(""+#LF$+"Press return to close the console window ")
Input()
CompilerEndIf
PrintN("Bye")
CloseConsole()
EndIf