OpenProcess_

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 720
Joined: Thu Dec 29, 2011 5:03 pm

OpenProcess_

Post 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
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: OpenProcess_

Post by PeDe »

I suspect the OpenProcess handle is not a valid window handle. You probably have to determine the window handle first.

Peter
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: OpenProcess_

Post by Axolotl »

Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
wombats
Enthusiast
Enthusiast
Posts: 720
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenProcess_

Post 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.
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: OpenProcess_

Post 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
wombats
Enthusiast
Enthusiast
Posts: 720
Joined: Thu Dec 29, 2011 5:03 pm

Re: OpenProcess_

Post by wombats »

Thank you!
Post Reply