Control of external program.

Everything else that doesn't fall into one of the other PB categories.
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Control of external program.

Post by kwag »

Hi guys,

I have a question, and I've searched but didn't find an answer.
I need to control an external program which I'm launching via "RunProgram", but what I need to do is to "minimize" the external program as soon as it runs.
I guess this will require some Win32 API stuff, but I haven't found how.
I also guess it will be something like sending something to the handle of the external running program. A "mimize" command :?:
But does "RunProgram" actually return the handle of the external program, or it returns a handle of what :?
I'm very confused here, so any help appreciated.

Thanks,
-Karl
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

handler=Runprogram(bla)
CloseWindow_(handler)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Henrik wrote:handler=Runprogram(bla)
CloseWindow_(handler)
definitly not!
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Damn i thought RunProgram returend the handler, and i should have known better i just had a diskussion about runprogram in another tread :oops:

Code: Select all

something=RunProgram("Program.exe")
hwnd = FindWindow_(0,"windowname")
Delay(3000)
CloseWindow_(hwnd)
@GPI why did'nt you tell him to use FindWindow_(0,"name of window")
or anyother way to get the handler ?

Best Regards
Henrik
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Post by kwag »

Henrik wrote:Damn i thought RunProgram returend the handler
That's exactly what I thought :)

-Karl
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

It does return the handler, but of the PROCESS.
here you need the Window handle.
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Post by kwag »

thefool wrote:It does return the handler, but of the PROCESS.
here you need the Window handle.
Any suggestion for getting the window handler of the external running process :?:

-Karl
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

But kwag if you know the name off the window you can do what i said

Code: Select all

hwnd = FindWindow_(0,"windowname")
Delay(3000)
CloseWindow_(hwnd) 
Best Regrads
Henrik
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Henrik wrote:Damn i thought RunProgram returend the handler, and i should have known better i just had a diskussion about runprogram in another tread :oops:

Code: Select all

something=RunProgram("Program.exe")
hwnd = FindWindow_(0,"windowname")
Delay(3000)
CloseWindow_(hwnd)
@GPI why did'nt you tell him to use FindWindow_(0,"name of window")
or anyother way to get the handler ?

Best Regards
Henrik
the FindWindow.
otherwise you might be able to list a process windows, but im not sure how. look at your api reference..
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Post by kwag »

Henrik wrote:But kwag if you know the name off the window you can do what i said

Code: Select all

hwnd = FindWindow_(0,"windowname")
Delay(3000)
CloseWindow_(hwnd) 
Best Regrads
Henrik
Hi Henrik,

The problem is that I have to wait for the external program to return, so I'm on a "blocked" state when I execute "RunProgram" :!:
I guess I could start a thread, and then use your procedure above :?:
Then I would set a flag just before I execute "RunProgram", and clear the flag when the program returns.
So the thread will be active only during the execution of the external program.
I'll give that a try with FindWindow_
It's API reading time :lol:

Thanks,
-Karl
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Uh i don't think i understand what your saying here ?
The problem is that I have to wait for the external program to return, so I'm on a "blocked" state when I execute "RunProgram"
Do you mean that you "Wait until you can see the window"??
If you dont want that you can use parameter 2 in runprogram
Now bear in mind that i'm danish and running win98
the Notepad.exe's window is called "Ikke-navngivet - Notesblok"
So i run Notepad.exe in hiddend mode, when i have found the window i minimize it and then show the window -minimized

I got a feeling thats not what you want, but here goes..

Code: Select all

RunProgram("c:\windows\Notepad.exe","", "",2) 
Repeat
hwnd = FindWindow_(0,"Ikke-navngivet - Notesblok") 
Delay(100)
Until hwnd
CloseWindow_(hwnd)  
ShowWindow_(hwnd,#SW_SHOW)
Best Regrads
Henrik.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Ohh.. i get it, i think, you want to display something (Your window) meanwhile, then yes use a tread
Best Regrads
Henrik
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Post by kwag »

No, that's perfect Henrik :)
Your approach is simpler than what I was thinking.
I don't need to do anything while the external program is running. I just have to wait until it's finished, so I can get the data it made.
So it's fine to wait in a loop with a small delay :)

Note: Just so you all know why I need this, it's an application I wrote which calls an external MPEG encoder and does file size prediction on target.
You can see a picture of the application (CQMatic) calling TMPGEnc, on my main page here: www.kvcd.net
Many users are fed up (me included :lol:) every time the external encoder pops-up while you are trying to browse and use the machine for something else. So I just want to minimize the external application every time it runs :cool:

Thanks,
-Karl
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Okay :D
You could hide/Show it with your program Ex. from a menu in tray
by using
ShowWindow_(hwnd,#SW_HIDE)
ShowWindow_(hwnd,#SW_SHOW)
Just a suggestion :)

Okay Good luck :)
Best Regrads
Henrik
kwag
User
User
Posts: 35
Joined: Thu Jul 17, 2003 10:03 pm
Contact:

Post by kwag »

Thanks Henrik :D
I got it working, and I just released an update.
Here's a cut&paste from my source.
Basically, it all boils down to this:

Code: Select all

        Result=RunProgram( encoderFilePath$, " CQMatic.tpr /Encode /Close",".", 2)
       
        ; Wait until program appears.
        Repeat
         Delay(10)
         hwnd = FindWindow_(0,"TMPGEnc") 
        Until hwnd <> 0 
        
         ; Give a chance for program to be up and running.
         ; Why? Doesn't work with delay less that 1000! 
         ; Ill behaved TMPEG?? Probably. 
         ; Without this delay, ShowWindow_(hwnd,#SW_MINIMIZE) won't work!

         Delay(3000)
         
         ; Minimize encoder if user selected minimize option.
         If GetGadgetState(#Minimized)
            ShowWindow_(hwnd,#SW_MINIMIZE)
         EndIf
        
        ; Now wait until encoder finishes and returns.
        Repeat
         Delay(100)
         hwnd = FindWindow_(0,"TMPGEnc") 
        Until hwnd = 0
       ;Program flow continues
Thank you, and thanks thefool and GPI :)
-Karl
Post Reply