Unhide program launched by RunProgram with #PB_Program_Hide

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Unhide program launched by RunProgram with #PB_Program_Hide

Post by firace »

Is there a simple way to unhide an external program launched by RunProgram with the #PB_Program_Hide flag?

Code: Select all



OpenWindow(0, 100, 100, 340, 280, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Show")
  
  Repeat 
    Select WaitWindowEvent() 
        
      Case #PB_Event_Gadget: 
        If EventGadget() = 1  
          RunProgram("ping.exe","-t 127.0.0.1", "",#PB_Program_Hide) 
          SetGadgetText(1, "Running...")
          DisableGadget(1, 1)
         EndIf 
         If EventGadget() = 2
           ;;; ???
         EndIf  
         
      Case #PB_Event_CloseWindow : End
        EndSelect
    
  ForEver
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by firace »

I just came up with this (using some APIs):

Code: Select all


OpenWindow(0, 100, 100, 340, 280, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Unhide")



Repeat 
  Select WaitWindowEvent() 
      
    Case #PB_Event_Gadget: 
      If EventGadget() = 1  
        progNumber=RunProgram("cmd", "/c title SHYCONSOLE && ping.exe -t 127.0.0.1", "",  #PB_Program_Hide)
        DisableGadget(1, 1)
      EndIf 
      If EventGadget() = 2
        X.l=FindWindow_(0, "SHYCONSOLE ")
        If X : ShowWindow_(x, #SW_SHOW) : EndIf 
      EndIf  
      
    Case #PB_Event_CloseWindow : End
  EndSelect
  
ForEver
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by BarryG »

Here's another way, that works for any window without searching for it by name. Don't forget that the DOS window will probably close before you can set it show!

Code: Select all

Procedure hWndFromPID(pid)
  w=GetWindow_(GetDesktopWindow_(),#GW_CHILD)
  Repeat
    GetWindowThreadProcessId_(w,@p)
    If p=pid
      hWnd=w
      Break
    EndIf
    w=GetWindow_(w,#GW_HWNDNEXT)
  Until w=0
  ProcedureReturn hWnd
EndProcedure

OpenWindow(0, 100, 100, 240, 100, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Show")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Gadget:
      If EventGadget() = 1 
        p=RunProgram("cmd","/c ping www.google.com", "", #PB_Program_Hide | #PB_Program_Open)
        SetGadgetText(1, "Running...")
        DisableGadget(1, 1)
      EndIf
      If EventGadget() = 2
        ShowWindow_(hWndFromPID(ProgramID(p)),#SW_SHOW)
      EndIf 
    Case #PB_Event_CloseWindow : End
  EndSelect
ForEver
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by mk-soft »

It's better to redirect the outputs to PB. When the PB program is terminated, the ping still runs invisibly in the background.

But here is the code to display.

Code: Select all

;-TOP

Structure udtListWindows
  Name.s
  Class.s
  Handle.i
  Process.i
  Childs.i
  Level.i
EndStructure

Threaded NewList ListWindows.udtListWindows()

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    
    ;- Windows
    
    Procedure.s GetTitle(Handle)
      Protected Name.s
      Name.s = Space(1024)
      GetWindowText_(Handle, @Name, Len(Name))
      ProcedureReturn Left(Name, Len(Name))
    EndProcedure
    
    Procedure.s GetClassName(Handle.i)
      Protected Class.s
      Class.s = Space(1024)
      GetClassName_(Handle, @Class, Len(Class))
      ProcedureReturn Left(Class, Len(Class))
    EndProcedure

    Procedure EnumProc(Handle.i, lParam.i)
      Protected *tmp.udtListWindows
      AddElement(ListWindows())
     
      ListWindows()\Handle = Handle
      ListWindows()\Process = 0
      GetWindowThreadProcessId_(Handle, @ListWindows()\Process)
      ListWindows()\Name = GetTitle(Handle)
      ListWindows()\Class = GetClassName(Handle)
      
      If lParam
        *tmp = lParam
        *tmp\Childs + 1
        ListWindows()\Level = *tmp\Level + 1
      Else
        ListWindows()\Level = 0
      EndIf
      
      EnumChildWindows_(Handle, @EnumProc(), @ListWindows())
      
      ProcedureReturn #True
    EndProcedure
    
    Procedure GetAllWindows() ; Result = Count of Windows
      Protected r1, len
      
      ClearList(ListWindows())
      r1 = EnumWindows_(@EnumProc(), 0)
      ProcedureReturn ListSize(ListWindows())
    EndProcedure
    
    Procedure FindNamedWindow(Name.s, IsThread = #False) ; Result = Handle
      Protected cnt, len
      len = Len(Name)
      Name = LCase(name)
      
      GetAllWindows()
      ForEach ListWindows()
        If FindString(ListWindows()\Class, "CabinetWClass")
          Continue
        EndIf
        If FindString(ListWindows()\Class, "ShellTabWindowClass")
          Continue
        EndIf
        If LCase(Left(ListWindows()\Name, len)) = Name
          ProcedureReturn ListWindows()\Handle
        EndIf
      Next
      ProcedureReturn 0
    EndProcedure
    
    Procedure CloseNamedWindow(Name.s, IsThread = #False) ; Result = Count of Windows
      Protected cnt, len
      len = Len(Name)
      GetAllWindows()
      ForEach ListWindows()
        If Left(ListWindows()\Name, len) = Name
          If FindString(ListWindows()\Class, "CabinetWClass")
            Continue
          EndIf
          If FindString(ListWindows()\Class, "ShellTabWindowClass")
            Continue
          EndIf
          SendMessage_(ListWindows()\Handle, #WM_CLOSE, 0, 0)
          cnt + 1
        EndIf
      Next
      ProcedureReturn cnt
    EndProcedure
    

  CompilerCase #PB_OS_MacOS
    
    ;- MacOS
    
    Procedure GetAllWindows() ; Result = Count of Windows
      Protected RunningApps.i, RunningAppsCount.i, RunningApp.i, AppName.s, i
      
      ClearList(ListWindows())
      RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
      RunningAppsCount = CocoaMessage(0, RunningApps, "count")
      i = 0
      While i < RunningAppsCount
        RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
        AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
        AddElement(ListWindows())
        ListWindows()\Name = AppName
        ListWindows()\Handle = RunningApp
        i + 1
      Wend
      ProcedureReturn i
    EndProcedure
    
    Procedure FindNamedWindow(Name.s, IsThread = #False) ; Result = RunningApp
      Protected r1, RunningApps.i, RunningAppsCount.i, RunningApp.i, AppName.s, i, cnt, Pool
      
      If IsThread
        Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
      EndIf
      
      RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
      RunningAppsCount = CocoaMessage(0, RunningApps, "count")
      i = 0
      While i < RunningAppsCount
        RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
        AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
        If Name = AppName
          r1 = RunningApp
          Break
        EndIf
        i + 1
      Wend
      
      If IsThread
        CocoaMessage(0, Pool, "release")
      EndIf
      
      ProcedureReturn r1
    EndProcedure
    
    Procedure CloseNamedWindow(Name.s, IsThread = #False) ;  ; Result = Count of Windows
      Protected RunningApps.i, RunningAppsCount.i, RunningApp.i, AppName.s, i, cnt, Pool
      
      If IsThread
        Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
      EndIf
      
      RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
      RunningAppsCount = CocoaMessage(0, RunningApps, "count")
      i = 0
      While i < RunningAppsCount
        RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
        AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
        If Name = AppName
          CocoaMessage(0, RunningApp, "terminate")
          cnt + 1
        EndIf
        i + 1
      Wend
      
      If IsThread
        CocoaMessage(0, Pool, "release")
      EndIf
      
      ProcedureReturn cnt
    EndProcedure
      
  CompilerCase #PB_OS_Linux
    
    ;- Linux
    
    Procedure GetAllWindows()
      Protected Compiler, Output.s, Temp.s, pos
      
        ClearList(ListWindows())
        Compiler = RunProgram("wmctrl", "-l -x", "", #PB_Program_Open | #PB_Program_Read)
        Output = ""
        If Compiler
          While ProgramRunning(Compiler)
            If AvailableProgramOutput(Compiler)
              Output = ReadProgramString(Compiler)
              AddElement(ListWindows())
              temp = "$" + Mid(Output, 3, 8)
              ListWindows()\Handle = Val(temp)
              temp = Mid(Output, 15)
              pos = FindString(temp, "  ")
              ListWindows()\Class = Left(temp, pos)
              temp = Trim(Mid(temp, pos))
              pos = FindString(temp, " ")
              ListWindows()\Name = Mid(temp, pos + 1)
            Else
              Delay(10)
            EndIf
          Wend
          CloseProgram(Compiler) ; Close the connection to the program
        Else
          Output = "Error - Programm konnte nicht gestartet werden!"
        EndIf
        
    EndProcedure
    
    Procedure FindNamedWindow(Name.s, IsThread = #False)
      Protected len = Len(Name)
      GetAllWindows()
      ForEach ListWindows()
        If Left(ListWindows()\Name, len) = Name
          If Not FindString(ListWindows()\Class, "nautilus", 1, #PB_String_NoCase)
            ProcedureReturn ListWindows()\Handle
          EndIf
        EndIf
      Next
      ProcedureReturn 0
    EndProcedure
    
    Procedure CloseNamedWindow(Name.s, IsThread = #False)  ; Result = #True or #False
      RunProgram("wmctrl", "-a " + Name, "")
      Delay(100)
      RunProgram("wmctrl", "-c " + Name, "")
      Delay(100)
      ProcedureReturn #True
    EndProcedure
    
CompilerEndSelect

; ****

OpenWindow(0, 100, 100, 340, 280, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Show")
  
  Repeat 
    Select WaitWindowEvent() 
        
      Case #PB_Event_Gadget: 
        If EventGadget() = 1  
          prog = RunProgram("ping.exe","-t 127.0.0.1", "", #PB_Program_Hide) 
          SetGadgetText(1, "Running...")
          DisableGadget(1, 1)
         EndIf 
         If EventGadget() = 2
           handle = FindNamedWindow("C:\Windows\system32\ping.exe")
           If handle
            ShowWindow_(handle, #SW_SHOW)
           EndIf
         EndIf  
         
      Case #PB_Event_CloseWindow : End
        EndSelect
    
  ForEver
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by RASHAD »

Hi

Code: Select all

Procedure EnumWindowsProc(hWnd,lParam)
  GetWindowThreadProcessId_(hWnd,@lpProc)
    If lpProc=PeekL(lParam)
      PokeL(lParam,hWnd)
      ProcedureReturn 0
    EndIf
  ProcedureReturn 1
EndProcedure

Procedure GetProcHwnd(lpProc,pid)
  ptr=pid
  Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr)
    If ptr=pid 
      ProcedureReturn 0
    EndIf
  ProcedureReturn ptr
EndProcedure

OpenWindow(0, 100, 100, 340, 280, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Show")
 
Repeat
  Select WaitWindowEvent()
     
    Case #PB_Event_Gadget:
      If EventGadget() = 1 
        Prog =  RunProgram("ping.exe","-t 127.0.0.1", "",#PB_Program_Open | #PB_Program_Hide)
        pID = ProgramID(Prog)
        SetGadgetText(1, "Running...")
        DisableGadget(1, 1)
       EndIf
       If EventGadget() = 2
         hproc = OpenProcess_(#PROCESS_ALL_ACCESS,0,pid)
         hWnd = GetProcHwnd(hproc,pid)
         ShowWindow_(hwnd,#SW_SHOW	)
       EndIf 
       
    Case #PB_Event_CloseWindow : End
      EndSelect
 
ForEver
Egypt my love
gonpublic2k
User
User
Posts: 40
Joined: Mon Oct 14, 2019 1:38 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by gonpublic2k »

RASHAD wrote:Hi

Code: Select all

Procedure EnumWindowsProc(hWnd,lParam)
  GetWindowThreadProcessId_(hWnd,@lpProc)
    If lpProc=PeekL(lParam)
      PokeL(lParam,hWnd)
      ProcedureReturn 0
    EndIf
  ProcedureReturn 1
EndProcedure

Procedure GetProcHwnd(lpProc,pid)
  ptr=pid
  Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr)
    If ptr=pid 
      ProcedureReturn 0
    EndIf
  ProcedureReturn ptr
EndProcedure

OpenWindow(0, 100, 100, 340, 280, "")

ButtonGadget(1, 10, 10, 200, 30, "Run ping (hidden)")
ButtonGadget(2, 10, 50, 200, 30, "Show")
 
Repeat
  Select WaitWindowEvent()
     
    Case #PB_Event_Gadget:
      If EventGadget() = 1 
        Prog =  RunProgram("ping.exe","-t 127.0.0.1", "",#PB_Program_Open | #PB_Program_Hide)
        pID = ProgramID(Prog)
        SetGadgetText(1, "Running...")
        DisableGadget(1, 1)
       EndIf
       If EventGadget() = 2
         hproc = OpenProcess_(#PROCESS_ALL_ACCESS,0,pid)
         hWnd = GetProcHwnd(hproc,pid)
         ShowWindow_(hwnd,#SW_SHOW	)
       EndIf 
       
    Case #PB_Event_CloseWindow : End
      EndSelect
 
ForEver
Hi All,

Very interesting aswers, is there a way to do this exact thing but display the command window inside a PB Window? Be it a Panel or inside
a PB window Frame?

I'd like to see an example like that, PLEASE!!!!!!! :D
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by BarryG »

gonpublic2k wrote:is there a way to do this exact thing but display the command window inside a PB Window?
Didn't you ask this here: viewtopic.php?f=13&t=73800
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by firace »

gonpublic2k wrote:
Hi All,

Very interesting aswers, is there a way to do this exact thing but display the command window inside a PB Window? Be it a Panel or inside
a PB window Frame?

I'd like to see an example like that, PLEASE!!!!!!! :D
Something like this?

Code: Select all

OpenWindow(0, 320, 320, 422, 250, "DOS Output", #PB_Window_SystemMenu)

EditorGadget(0, 8, 8, 406, 233)

DOS = RunProgram("cmd", "/c ping.exe -n 10 127.0.0.1", "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read)

If DOS
  Output$ = ""
  While ProgramRunning(DOS)
    If AvailableProgramOutput(DOS)
      Output$ = ReadProgramString(DOS)
    AddGadgetItem(0, -1, Output$)
    UpdateWindow_(GadgetID(0))
  EndIf
  Wend
  CloseProgram(DOS)
EndIf

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Unhide program launched by RunProgram with #PB_Program_H

Post by mk-soft »

You need perhaps Threads...

Show example 2 from MiniThreadControl...

Link: viewtopic.php?f=12&t=73231

P.S.
And WriteLog to fill ListView over Threads...
Link: viewtopic.php?f=12&t=73491
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply