Page 1 of 1

Does RunProgram retrieve ThreadID ?

Posted: Fri Jun 27, 2003 3:20 am
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.

Re: Does RunProgram retrieve ThreadID ?

Posted: Fri Jun 27, 2003 8:20 am
by PB
See Thorsten's tip at viewtopic.php?t=6442

Posted: Fri Jun 27, 2003 12:48 pm
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

Posted: Fri Jun 27, 2003 4:31 pm
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 

Posted: Sat Jun 28, 2003 12:42 am
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

Posted: Sat Jun 28, 2003 12:49 am
by eddy
Ok :?

Posted: Sat Jun 28, 2003 1:21 am
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

Posted: Sat Jun 28, 2003 8:20 pm
by eddy
thanks

I can write a correct tip now. :D