RunProgram, then send it a message?
Posted: Fri Oct 23, 2020 3:10 am
Is there a cross-platform way to make a command line utility stop? I have a Windows solution, in case anyone needs it:
but I'm wondering if there is a direct way to communicate with a program started using RunProgram, rather than faking keystrokes?
If not, are there equivalents of the keystroke code above, but for Mac and Linux?
Thank you.
Code: Select all
Structure RunProgramExtendedStructure
ProgramID.i
WindowHandle.i
EndStructure
Procedure.i EnumWindowsCallBack(hwnd.i, param.i)
Protected *RunProgramExtended.RunProgramExtendedStructure
Result.i = #True
*RunProgramExtended = param
If GetWindowThreadProcessId_(hwnd, @PID.i)
If PID = *RunProgramExtended\ProgramID
*RunProgramExtended\WindowHandle = hwnd
Result = #False
EndIf
EndIf
ProcedureReturn Result
EndProcedure
prog.i = RunProgram(exe,param,folder,#PB_Program_Open)
*Buffer = AllocateMemory(#MAX_PATH * SizeOf(Character), #PB_Memory_NoClear)
RunProgramExtended.RunProgramExtendedStructure\ProgramID = ProgramID(prog)
starttime.i = Date()
While ProgramRunning(prog)
If RunProgramExtended\WindowHandle = 0
EnumWindows_(@EnumWindowsCallBack(), @RunProgramExtended)
EndIf
If (Date()-starttime)>4 ; after 4 seconds, tell it to shut down
PostMessage_(RunProgramExtended\WindowHandle,#WM_KEYDOWN,#VK_Q,0)
PostMessage_(RunProgramExtended\WindowHandle,#WM_KEYUP,#VK_Q,0)
EndIf
Delay(50)
Wend
CloseProgram(prog)If not, are there equivalents of the keystroke code above, but for Mac and Linux?
Thank you.