Page 1 of 2

Hide external window

Posted: Thu Jan 07, 2010 12:16 pm
by mx101
hi...

how to hide a window from a external program like notepad for example?

Re: Hide external window

Posted: Thu Jan 07, 2010 4:44 pm
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

Re: Hide external window

Posted: Thu Jan 07, 2010 5:03 pm
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"

Re: Hide external window

Posted: Thu Jan 07, 2010 5:10 pm
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

Re: Hide external window

Posted: Thu Jan 07, 2010 5:14 pm
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

Re: Hide external window

Posted: Thu Jan 07, 2010 5:16 pm
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.

Re: Hide external window

Posted: Thu Jan 07, 2010 5:17 pm
by RASHAD
Sorry Thomas
You are very fast (give me some time man well you?)

Check it now Thomas

Re: Hide external window

Posted: Thu Jan 07, 2010 5:21 pm
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:

Re: Hide external window

Posted: Thu Jan 07, 2010 5:26 pm
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

Re: Hide external window

Posted: Thu Jan 07, 2010 7:44 pm
by Fluid Byte
Uhmm, lol?

Code: Select all

hwndNotepad = FindWindow_("Notepad",0)
Delay(100)
ShowWindow_(hwndNotepad,#SW_HIDE)

Re: Hide external window

Posted: Thu Jan 07, 2010 7:55 pm
by Rook Zimbabwe
:evil:

Why would you need to hide your program from "notepad"???

Re: Hide external window

Posted: Thu Jan 07, 2010 8:00 pm
by Fluid Byte
Be happy it's just Notepad and not some system critical application :P

Re: Hide external window

Posted: Thu Jan 07, 2010 8:18 pm
by RASHAD
@FB Hi
Unfortunately it will not always work
Try it with the Calculator prog for test

Re: Hide external window

Posted: Thu Jan 07, 2010 8:29 pm
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.

Re: Hide external window

Posted: Thu Jan 07, 2010 8:38 pm
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