CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Thumbs up How to hide your program from the Task Manager

    This works for XP, VB.NET 2003/2005/2008
    This example does not work on Windows Vista.

    Add a ListView, and Timer to a form.
    {Timer must have the Elapsed vent}


    Code:
           
    Public Class Form1
        Const WM_COMMAND As Int32 = &H111
        Const MF_ENABLED As Int32 = &H0
        Const MF_GRAYED As Int32 = &H1
        Const LVM_FIRST As Int32 = &H1000
        Const LVM_DELETEITEM As Int32 = (LVM_FIRST + 8)
        Const LVM_SORTITEMS As Int32 = (LVM_FIRST + 48)
        Private Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
        Private Declare Function apiFindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
        Private Declare Function apiEnableWindow Lib "user32" Alias "EnableWindow" (ByVal hwnd As Int32, ByVal fEnable As Int32) As Boolean
        Private Declare Function apiGetMenu Lib "user32" Alias "GetMenu" (ByVal hwnd As Int32) As Int32
        Private Declare Function apiGetSubMenu Lib "user32" Alias "GetSubMenu" (ByVal hMenu As Int32, ByVal nPos As Int32) As Int32
        Private Declare Function apiGetMenuItemID Lib "user32" Alias "GetMenuItemID" (ByVal hMenu As Int32, ByVal nPos As Int32) As Int32
        Private Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Int32, ByVal wIDEnableItem As Int32, ByVal wEnable As Int32) As Int32
        Private Declare Function apiSendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
        Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Int32
        Private Declare Function apiLockWindowUpdate Lib "user32" Alias "LockWindowUpdate" (ByVal hwndLock As Int32) As Int32
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '  Me.Text = pName 'Set form name to match process name given
            Me.ShowInTaskbar = False 'hide application from taskbar
            Timer1.Interval = 700 'Set to start fast
            Timer1.Enabled = True 'Actually start the timer
            ListView1.View = View.Details
            ListView1.Columns.Add("Process name", -2, HorizontalAlignment.Left)
            ListView1.Sorting = SortOrder.Ascending 'So that the list view automatically sorts the entries for us.
            'Me.Hide()  'uncomment when ready to terminate this program elsehow.
        End Sub
        Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
            HideProcess("", False)
        End Sub
        Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
            HideProcess("explorer", True)
        End Sub
    
    
        Private Function HideProcess(ByVal pName As String, Optional ByVal pHide As Boolean = True)
            On Error Resume Next
            Dim lhWndParent As Int32 = apiFindWindow(Nothing, "Windows Task Manager") 'get handle to the task manager
            Dim lhWndDialog As Int32 = 0
            Dim lhWndProcessList As Int32 = 0
            Dim lhWndProcessHeader As Int32 = 0
            Dim hMenu As Int32 = apiGetMenu(lhWndParent) 'get it's menu handle
            Dim hSubMenu As Int32 = apiGetSubMenu(hMenu, 2) 'get it's submenu handle for "View"
            Dim hSubSubMenu As Int32 = apiGetSubMenu(hSubMenu, 1) 'get it;s subsub menu handle for "update speed"
            Dim hId1 As Int32 = apiGetMenuItemID(hSubMenu, 0) 'Get id for "refresh now" item
            Dim hId2 As Int32 = apiGetMenuItemID(hSubSubMenu, 0) 'Get id for "high update speed" item
            Dim hId3 As Int32 = apiGetMenuItemID(hSubSubMenu, 1) 'Get id for "normal update speed" item
            Dim hId4 As Int32 = apiGetMenuItemID(hSubSubMenu, 2) 'Get id for "low update speed" item
            Dim hId5 As Int32 = apiGetMenuItemID(hSubSubMenu, 3) 'Get id for "paused update speed" item
            If pHide = True Then
                Dim ProcessItemCount, ProcessItemIndex As Int32
                Dim itemString As String, p As New Process, Processes() As Process
    
                For i As Int32 = 1 To 7 'Loop through all seven child windows, for handles to the listviews, buttons, and header
                    lhWndDialog = apiFindWindowEx(lhWndParent, lhWndDialog, Nothing, Nothing)
                    If lhWndProcessList = 0 Then lhWndProcessList = apiFindWindowEx(lhWndDialog, 0, "SysListView32", "Processes")
                    If lhWndProcessHeader = 0 Then lhWndProcessHeader = apiFindWindowEx(lhWndProcessList, 0, "SysHeader32", Nothing)
                Next
    
                apiSendMessage(lhWndParent, WM_COMMAND, hId5, 0) 'Click "paused update speed", so we can do it for the taskmgr
    
                apiEnableMenuItem(hMenu, hId1, MF_GRAYED) 'disable refresh now item
                apiEnableMenuItem(hMenu, hId2, MF_GRAYED) 'disable high update speed
                apiEnableMenuItem(hMenu, hId3, MF_GRAYED) 'disable normal update speed
                apiEnableMenuItem(hMenu, hId4, MF_GRAYED) 'disable low update speed
                apiEnableMenuItem(hMenu, hId5, MF_GRAYED) 'disable paused update speed
    
                apiEnableWindow(lhWndProcessHeader, 0) 'Disable process header, so it cannot be resorted by user
    
                If Me.ListView1.Items.Count > 0 Then Me.ListView1.Items.Clear() 'clear any old data that was on the list
    
                Processes = Process.GetProcesses() 'Get processes
    
                For Each p In Processes  'Count processes, and add them to the listview.
                    ProcessItemCount += 1
                    If p.ProcessName.ToString = "Idle" Then
                        With Me.ListView1.Items.Add("System Idle Process")
                        End With
                    Else
                        With Me.ListView1.Items.Add(p.ProcessName.ToString)
                        End With
                    End If
                Next p
    
                'Look for, the index of the process matching the string name of our caption then
                For z As Int32 = 0 To ProcessItemCount - 1
                    itemString = ListView1.Items.Item(z).Text.ToString()
                    If itemString = pName Then ProcessItemIndex = z
                Next
    
                apiLockWindowUpdate(lhWndProcessList) 'Lock the window from updating, to reduce flashing.
                apiSendMessage(lhWndParent, WM_COMMAND, hId1, 0) 'AutoClick refresh to update, then immediately sort and delete. 
                apiSendMessage(lhWndProcessList, LVM_SORTITEMS, 0, Nothing) ' Sort process items alphabetically
                apiSendMessage(lhWndProcessList, LVM_DELETEITEM, ProcessItemIndex, 0) 'Delete the  process, 
                apiLockWindowUpdate(False) 'unlock that window
    
                If lhWndParent = 0 Then
                    If Timer1.Interval <> 800 Then Timer1.Interval = 800 'Set to react fast while task manager is closed.
                Else
                    If Timer1.Interval <> 2500 Then Timer1.Interval = 2500 'Set to a normal looking update speed, while the task manager remains open.
                End If
            Else
                Timer1.Enabled = False 'kill the timer
                For i As Int32 = 1 To 7
                    lhWndDialog = apiFindWindowEx(lhWndParent, lhWndDialog, Nothing, Nothing)
                    If lhWndProcessList = 0 Then lhWndProcessList = apiFindWindowEx(lhWndDialog, 0, "SysListView32", "Processes")
                    If lhWndProcessHeader = 0 Then lhWndProcessHeader = apiFindWindowEx(lhWndProcessList, 0, "SysHeader32", Nothing)
                Next
                apiEnableMenuItem(hMenu, hId1, MF_ENABLED) 're-enable refresh now
                apiEnableMenuItem(hMenu, hId2, MF_ENABLED) 're-enable high update speed
                apiEnableMenuItem(hMenu, hId3, MF_ENABLED) 're-enable normal update speed
                apiEnableMenuItem(hMenu, hId4, MF_ENABLED) 're-enable low update speed
                apiEnableMenuItem(hMenu, hId5, MF_ENABLED) 're-enable paused update speed
                apiSendMessage(lhWndParent, WM_COMMAND, hId3, 0) 'click normal update speed
                apiSendMessage(lhWndParent, WM_COMMAND, hId1, 0) 'click refresh now
                apiEnableWindow(lhWndProcessHeader, 1) 'Enable process header
            End If
            Return True
        End Function
    
    
    
    End Class
    Last edited by TT(n); August 16th, 2008 at 04:00 AM. Reason: Update

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to hide your program from the Task Manager

    Great Code TT(n)!

    It doesn't seem to like this event (in both 2003 and 2005) :
    Code:
        Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
            RefreshHide() 'Hide process
        End Sub
    So I changed Elapsed to Tick was that right ¿

  3. #3
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    I dont think Tick will work, or does it?
    I'd try setting the timer interval from 750, to something like 1200.
    It is a long set of statements i guess.

    The RefreshHide sub, has on error resume next which is maybe what your talking about?
    Should use a try catch maybe.
    But I think it occurs on the first pass only.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to hide your program from the Task Manager

    The whole problme is not with the RefreshHide sub, the problem is that it tells me that Event Elapsed cannot be found

    Don't you think we should rather create the Timer Dynamically (and add the Elapsed event through the use of the System.Timers namespace), instead of adding a Timer in design View (because by adding the Timer from the Toolbox, it doesn't give the Elapsed event - it only gives Tick and Disposed ) ¿
    Last edited by HanneSThEGreaT; November 18th, 2006 at 05:18 AM.

  5. #5
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    Ah,
    Open the toolbox, and click on the component tab, and add that timer.
    Then click the visual reference to the timer, and it adds the Elapsed event. [Edit: for 2003 only]




    \\\///
    I found a problem though.
    The list view from VB shows "System Idle", as "Idle" which could mess up the order if your process name is greater than the letter i.
    I'll have to fix this, to work with all process names.
    Last edited by TT(n); November 18th, 2006 at 09:29 AM.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to hide your program from the Task Manager

    That timer, OK - I always was probably a bit naive in thinking why there are two seperate timers in the Toolbox, the "normal" one and the one on the components tab - just glad that confusion is cleared up
    Did you edit it in your first post, BTW, just to prevent the same confusion .Nevermind, I see you did.

    On Topic:
    I've now got the very same problem you're sitting with, and I can't wait for your update
    Last edited by HanneSThEGreaT; November 18th, 2006 at 05:19 AM.

  7. #7
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Thumbs up Re: How to hide your program from the Task Manager

    The code has been updated, and fixed for both problems.
    I had forgotten to subract one, from the process count, since it's padded at zero for the first item. Ooops. No prob.

    I simply caught the p.ProcessName.ToString, and made an else if condition to add the string System Idle Process, instead of the process name VB gives it "Idle". No prob.





    This should work on 2005 now.
    I'll install it, and check, so I dont have to keep asking.
    Last edited by TT(n); November 18th, 2006 at 07:29 AM.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    To reduce the flickering of the ListView, I've updated the example with the LockWindowUpdate API. Works perfectly for 2003 and 2005!

  9. #9
    Join Date
    May 2007
    Posts
    9

    Question Re: How to hide your program from the Task Manager

    How can I integrate the Hide part of the code into my program so no timers no listview just hide.

    Thanx

  10. #10
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    I'm not sure you completely understand what the code is actually doing.

    You can however create your own sorting function, to replace the listview.
    You can also just hide the listview and or form. A timer is somewhat necessary, and should be fixed to match the task manager's refresh rate.
    This gives it the illusion of refreshing/updating at the same time as the preference set, in the task manager's main menu.

    I hope you do not missuse this code.
    Thank you

  11. #11
    Join Date
    May 2007
    Posts
    9

    Re: How to hide your program from the Task Manager

    How do I do this

  12. #12
    Join Date
    May 2007
    Posts
    9

    Re: How to hide your program from the Task Manager

    How do I do this??

  13. #13
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    Who are you again?

    If you have to ask, then I dont think you really have enough experience to implement it. What I mean is, that our conversation will be and endless cycle of "how do I do that then...", since alot of things are inter-related.

    I'm sorry, but I'd encourage you to learn about the basics first.
    There is a great set of tutorial links at the top of the forum here.
    Microsoft press books, are also extremley helpful at becoming a novice-intermediate programmer. Then you can master anything particular you like!

  14. #14
    Join Date
    May 2007
    Posts
    9

    Re: How to hide your program from the Task Manager

    I just dont understand this code
    Im quite good actually

  15. #15
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: How to hide your program from the Task Manager

    Well okay so maybe you know a little bit then.
    However, there are explainations on just about every line, which are pretty straight forward.

    You can simply uncomment the Me.Hide, so that the listview and form is not visible. Be sure you are ready to terminate the process in some other way, because it will now be invisible to the task manager.
    If you insist on dropping the listview, you'll need to hold the information in a string, and perform a simple alphabetic sorting of the process names.

    What exactly are you using it for?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured