Hide external window

Just starting out? Need help? Post your questions and find answers here.
mx101
User
User
Posts: 73
Joined: Thu Sep 18, 2008 3:21 pm

Hide external window

Post by mx101 »

hi...

how to hide a window from a external program like notepad for example?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Hide external window

Post by RASHAD »

mx101 Hi

1- Run Notepad.exe first
2- Run the next prog. and play with show or hide

Thanks to SFSxOI

Code: Select all


#PROCESS_ALL_ACCESS_VISTA_WIN7 = $1FFFFF

Prototype.i PFNCreateToolhelp32Snapshot(dwFlags.i, th32ProcessID.i) ;
Prototype.b PFNProcess32First(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Prototype.b PFNProcess32Next(hSnapshot.i, *lppe.PROCESSENTRY32) ;

Procedure GetPidByName(p_name$)
    Protected hDLL.i, process_name$
    Protected PEntry.PROCESSENTRY32, hTool32.i
    Protected pCreateToolhelp32Snapshot.PFNCreateToolhelp32Snapshot
    Protected pProcess32First.PFNProcess32First
    Protected pProcess32Next.PFNProcess32Next
    Protected pid.i
   
    hDLL = OpenLibrary(#PB_Any,"kernel32.dll")
   
    If hDLL
        pCreateToolhelp32Snapshot = GetFunction(hDLL,"CreateToolhelp32Snapshot")
        pProcess32First = GetFunction(hDLL,"Process32First")
        pProcess32Next = GetFunction(hDLL,"Process32Next")
    Else
        ProcedureReturn 0
    EndIf
   
    PEntry\dwSize = SizeOf(PROCESSENTRY32)
    hTool32 = pCreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0)
    pProcess32First(hTool32, @PEntry)
    process_name$ = Space(#MAX_PATH)
    CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
   
    If  UCase(process_name$) = UCase(p_name$)
        ProcedureReturn PEntry\th32ProcessID
    EndIf
   
    While pProcess32Next(hTool32, @PEntry) > 0
        process_name$ = Space(#MAX_PATH)
        CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
       
        If  UCase(process_name$) = UCase(p_name$)
            ProcedureReturn PEntry\th32ProcessID
        EndIf
   
    Wend
   
    CloseLibrary(hDLL)
   
    ProcedureReturn 0
EndProcedure

Procedure EnumWindowsProc(hWnd,lParam)
   GetWindowThreadProcessId_(hWnd,@lpProc)
   If lpProc=PeekL(lParam) ; process passed to EnumWindows is process found at this hwnd
      PokeL(lParam,hWnd) ; set ptr to hwnd
      ProcedureReturn 0
   EndIf
   ProcedureReturn 1
EndProcedure

Procedure GetProcessId_(hProcess) ; for windows Vista, Windows XP SP1, Windows 7
  Protected hThread
  Protected Pid
  Protected GCPI
 
  GCPI    = GetProcAddress_(GetModuleHandle_("Kernel32"),"GetCurrentProcessId");   
  hThread = CreateRemoteThread_(hProcess,0,0,GCPI,0,0,0)
 
  If Not hThread
    ProcedureReturn 0
  EndIf
 
  WaitForSingleObject_(hThread,#INFINITE) 
  GetExitCodeThread_(hThread,@Pid)
  CloseHandle_(hThread)
 
  ProcedureReturn Pid 
EndProcedure

Procedure GetProcHwnd(lpProc)
   Protected pid = GetProcessId_(lpProc)
   ptr=pid
   Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr)
   If ptr=pid ; if no handle is returned no matching process was found
      ProcedureReturn 0
   EndIf
   ; some form of GetWindow_() here for top-level window..
   ProcedureReturn ptr
EndProcedure

#WindowWidth  = 310
#WindowHeight = 200

hwnd = OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
    
  Top = 10
  GadgetHeight = 24
  ButtonGadget(1, 223,#WindowHeight-36, 80, 24, "Hide")
  ButtonGadget(2, 120,#WindowHeight-36, 80, 24, "Show")
  ButtonGadget(8, 10, #WindowHeight-36, 80, 24, "Quit")
  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget

      Select EventGadget()
          
        Case 1 
          Pid.i = GetPidByName("notepad.exe")          
          hproc.i = OpenProcess_(#PROCESS_ALL_ACCESS,0,Pid)          
          hwnd  = GetProcHwnd(hproc)
          ShowWindow_(hwnd,#SW_HIDE)
          
        Case 2 
          ShowWindow_(hwnd,#SW_SHOW)
  
        Case 8 ; Quit...
          EventID = #PB_Event_CloseWindow           
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
End

Edit: I editted the code I posted it wrong ,Sorry
Last edited by RASHAD on Thu Jan 07, 2010 5:21 pm, edited 2 times in total.
Egypt my love
mx101
User
User
Posts: 73
Joined: Thu Sep 18, 2008 3:21 pm

Re: Hide external window

Post by mx101 »

RASHAD wrote:mx101 Hi

1- Run Notepad.exe first
2- Run the next prog. and play with show or hide

Thanks to SFSxOI

Code: Select all


#WindowWidth  = 310
#WindowHeight = 200

hwnd = OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
    
  Top = 10
  GadgetHeight = 24
  ButtonGadget(1, 223,#WindowHeight-36, 80, 24, "Hide")
  ButtonGadget(2, 120,#WindowHeight-36, 80, 24, "Show")
  ButtonGadget(8, 10, #WindowHeight-36, 80, 24, "Quit")
  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget

      Select EventGadget()
          
        Case 1 
          Pid.i = GetPidByName("notepad.exe")          
          hproc.i = OpenProcess_(#PROCESS_ALL_ACCESS,0,Pid)          
          hwnd  = GetProcHwnd(hproc)
          ShowWindow_(hwnd,#SW_HIDE)
          
        Case 2 
          ShowWindow_(hwnd,#SW_SHOW)
  
        Case 8 ; Quit...
          EventID = #PB_Event_CloseWindow
           
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
End
thanks but i have compiler error "GetPidByName is not a function, array, macro or link list"
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Hide external window

Post by RASHAD »

Sorry mx101
I posted the wrong code ,I allready updated it in the first post
This code is for 64 bit application
If you are running 32 bit OS and application just change all .i to .l
That is all

You can after successful attempt enhance the code to check for the application ( if it is 32 or 64 bit ) first
Last edited by RASHAD on Thu Jan 07, 2010 5:15 pm, edited 1 time in total.
Egypt my love
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Hide external window

Post by ts-soft »

RASHAD wrote:If you are running 32 bit OS and application just change all .i to .l
nothing to change. Changing is bad :mrgreen:
The code missing the "GetPidByName()" Function
mx101
User
User
Posts: 73
Joined: Thu Sep 18, 2008 3:21 pm

Re: Hide external window

Post by mx101 »

RASHAD wrote:Sorry mx101
I posted the wrong code ,I allready updated it in the first post
This code is for 64 bit application
If you are running 32 bit OS and application just change all .i to .l
That is all

You can after successful attempt enhance the code to check for the application ( if it is 32 or 64 bit ) first

but i have have same error on 64 bit application.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Hide external window

Post by RASHAD »

Sorry Thomas
You are very fast (give me some time man well you?)

Check it now Thomas
Last edited by RASHAD on Thu Jan 07, 2010 5:22 pm, edited 1 time in total.
Egypt my love
mx101
User
User
Posts: 73
Joined: Thu Sep 18, 2008 3:21 pm

Re: Hide external window

Post by mx101 »

RASHAD wrote:Sorry Thomas
You are very fast (give me some time man well you?)
ok work on x64

i had not read that you made the update to the code :D


thanks for help if can give work code to x86 i appreciate :oops:
mx101
User
User
Posts: 73
Joined: Thu Sep 18, 2008 3:21 pm

Re: Hide external window

Post by mx101 »

RASHAD wrote:Sorry Thomas
You are very fast (give me some time man well you?)

Check it now Thomas
work on x64 not work on x86


big thanks :D
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Hide external window

Post by Fluid Byte »

Uhmm, lol?

Code: Select all

hwndNotepad = FindWindow_("Notepad",0)
Delay(100)
ShowWindow_(hwndNotepad,#SW_HIDE)
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Hide external window

Post by Rook Zimbabwe »

:evil:

Why would you need to hide your program from "notepad"???
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Hide external window

Post by Fluid Byte »

Be happy it's just Notepad and not some system critical application :P
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Hide external window

Post by RASHAD »

@FB Hi
Unfortunately it will not always work
Try it with the Calculator prog for test
Egypt my love
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Hide external window

Post by UserOfPure »

It will work with Calculator, all you need to do is get the exact window title with FindWindow, then use the result of that with ShowWindow and #SW_HIDE.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Hide external window

Post by RASHAD »

OK I am using Windows 7 x64
Please put some code using FindWindow_() to hide Calculator prog

mx101 did not say Notepad he said like NotePad so the choices are open
Please check
Egypt my love
Post Reply