Page 1 of 1

OpenProcess_

Posted: Thu Aug 15, 2024 12:37 pm
by wombats
Am I doing something wrong? I can't set the window text or minimise the opened program.

Code: Select all

EnableExplicit

Define prog = RunProgram("test.exe", "", "", #PB_Program_Open)

Define error
Define errorMsg.s = Space(255)

If IsProgram(prog)
  Define progHandle = OpenProcess_(#PROCESS_ALL_ACCESS, #False, ProgramID(prog))
  Define error = GetLastError_()
  FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, #Null, error, 0, @errorMsg, 255, #Null)
  SetWindowText_(progHandle, "testing")
  ShowWindow_(progHandle, #SW_HIDE)
EndIf

CloseProgram(prog)
test.exe is a simple PB executable:

Code: Select all

OpenWindow(0, 100, 100, 640, 480, "", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_MinimizeGadget)

Global quit

Repeat
  
  event = WaitWindowEvent()
  
  If event = #PB_Event_CloseWindow
    quit = #True
  EndIf
  
Until quit

Re: OpenProcess_

Posted: Thu Aug 15, 2024 1:47 pm
by PeDe
I suspect the OpenProcess handle is not a valid window handle. You probably have to determine the window handle first.

Peter

Re: OpenProcess_

Posted: Thu Aug 15, 2024 1:51 pm
by Axolotl

Re: OpenProcess_

Posted: Fri Aug 16, 2024 4:34 pm
by wombats
Thanks for the replies. I couldn't get it to work, unfortunately. I may have to just send the stuff I need to change through a command line parameter or something.

Re: OpenProcess_

Posted: Fri Aug 16, 2024 6:08 pm
by PeDe
I have pasted the code from Axolot's link into your example.

Peter

Code: Select all

EnableExplicit

Structure RunProgramExtendedStructure
	ProgramID.i
	WindowHandle.i
EndStructure

Procedure.i EnumWindowsCallBack(hwnd.i, param.i)
	Protected Result.i, PID.l, Length.i, *RunProgramExtended.RunProgramExtendedStructure
	
	Result = #True
	*RunProgramExtended = param
	If GetWindowThreadProcessId_(hwnd, @PID)
		If PID = *RunProgramExtended\ProgramID
			*RunProgramExtended\WindowHandle = hwnd
			Result = #False
		EndIf
	EndIf

	ProcedureReturn Result
EndProcedure

Define RunProgramExtended.RunProgramExtendedStructure

Define prog.i = RunProgram("test.exe", "", "", #PB_Program_Open)
Delay(1000)

If prog
	
	RunProgramExtended\ProgramID = ProgramID(prog)
	EnumWindows_(@EnumWindowsCallBack(), @RunProgramExtended)
	If RunProgramExtended\WindowHandle
		SetWindowText_(RunProgramExtended\WindowHandle, "testing")
		Delay(1000)
		ShowWindow_(RunProgramExtended\WindowHandle, #SW_HIDE)
		Delay(1000)
		ShowWindow_(RunProgramExtended\WindowHandle, #SW_SHOW)
	EndIf
	
	CloseProgram(prog)
EndIf

Re: OpenProcess_

Posted: Sat Aug 17, 2024 6:34 pm
by wombats
Thank you!