Does RunProgram retrieve ThreadID ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Does RunProgram retrieve ThreadID ?

Post by eddy »

when I open a calculator (calc.exe) in main window, I want to bring to front it
and I want to kill this calculator when I close the main window.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Does RunProgram retrieve ThreadID ?

Post by PB »

See Thorsten's tip at viewtopic.php?t=6442
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The result of RunProgram is a Process HANDLE, you can use it to read
the processes Memory, or to terminate it, and so on.

On XP, there are several functions to get ThreadID and ProcessID of a
handle, but not on the other Versions of windows.

Timo
quidquid Latine dictum sit altum videtur
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

I don't why this solution does not work... :(

Code: Select all

   hProcess=RunProgram(GetSystemDir()+"\calc.exe")
   app=GetRunProgramHandle(hProcess)  
   ShowWindow_(app, #SW_MINIMIZE)

Code: Select all

Procedure.l GetRunProgramHandle(hProcessProgram.l)
   win = FindWindow_(0,0)
   While win<>0 And quit=0
      If GetParent_(win)=WindowID()
         GetWindowThreadProcessId_(win, @pid.l) 
         hProcess=OpenProcess_(#PROCESS_CREATE_PROCESS,0,pid)
         If hProcess = hProcessProgram 
            app=win 
            quit=1 
         EndIf  
      EndIf 
      win = GetWindow_(win, #GW_HWNDNEXT)          
   Wend 
   ProcedureReturn app
EndProcedure 
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

If you open the Process using openprocess, a new handle will be created,
you never get the same as you allready have. Each OpenProcess creates
it's own handle, which has special rights and stuff.

So the results will never match.

Timo
quidquid Latine dictum sit altum videtur
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

Ok :?
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Try this one:

Code: Select all

Procedure.l EnumWindowsProc(hWnd.l, lParam.l)
  Shared RunProgram_WindowID.l
  If GetWindowThreadProcessId_(hWnd, 0) = lParam
    RunProgram_WindowID = hWnd
    ProcedureReturn #FALSE
  Else
    ProcedureReturn #TRUE
  EndIf
EndProcedure

Procedure.l RunProgram2(Filename.s, Parameter.s, Directory.s)
  Shared RunProgram_WindowID.l
  Info.STARTUPINFO
  Info\cb = SizeOf(STARTUPINFO)
  Debug CreateProcess_(@Filename, @Parameter, 0, 0, 0, 0, 0, @Directory, @Info, @ProcessInfo.PROCESS_INFORMATION)
  Debug getlasterror_()
  RunProgram_WindowID = 0
  EnumWindows_(@EnumWindowsProc(), ProcessInfo\dwThreadId)
  ProcedureReturn RunProgram_WindowID
EndProcedure

Debug RunProgram2("C:\WINNT\notepad.exe","","C:\WINNT\")
Debug FindWindow_(0, "Untitled - Notepad")
End
This Runprogram2 procedure returns the handle to the new opened
window, if possible.

You have to specify both, full path to exe, and full working directory path
to make sure it works correct on all windows versions.

btw: The ProcessInfo Structure which is filled up with information by
OpenProcess contains ProcessID, ThreadID, ProcessHandle and
ThreadHandle of the new process (just in case you need it)

Timo
quidquid Latine dictum sit altum videtur
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

thanks

I can write a correct tip now. :D
Post Reply