Dr_FarFar

[+] Hide Process From TaskManager [VB.NET] | By Dr.FarFar |

Aug 7th, 2013
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 15.94 KB | None | 0 0
  1. #Region " Imports "
  2. Imports System.IO
  3. Imports Microsoft.Win32.SafeHandles
  4. Imports System.Runtime.InteropServices
  5. Imports System.Text
  6. Imports System.ComponentModel
  7. #End Region
  8. #Region " TMListViewDelete "
  9.  
  10. Module TMListViewDelete
  11. #Region " Declarations/Functions/Consts "
  12.  
  13.     Private Const LVM_FIRST = &H1000
  14.     Private Const LVM_DELETECOLUMN = LVM_FIRST + 28
  15.  
  16.     Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)
  17.     Private Const LVM_SORTITEMS = (LVM_FIRST + 48)
  18.     Private Const LVM_DELETEITEM = (LVM_FIRST + 8)
  19.     Private Const LVM_GETNEXTITEM = (LVM_FIRST + 12)
  20.     Private Const LVM_GETITEM = (LVM_FIRST + 75)
  21.  
  22.     Private Delegate Function EnumDelegate(ByVal lngHwnd As IntPtr, ByVal lngLParam As Integer) As Integer
  23.     Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  24.     Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
  25.     Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumDelegate, ByVal lParam As Integer) As Integer
  26.     Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  27.     Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
  28.     Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
  29.     Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As IntPtr) As Integer
  30.     Dim t As New Timer
  31.  
  32.     Dim hwnd As IntPtr
  33.     Dim controls As String
  34.     Public MyProc As String
  35.  
  36.     Dim ProcLV As IntPtr = IntPtr.Zero
  37. #End Region
  38.  
  39. #Region " Timer's Tick "
  40.     Private Sub t_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  41.         If ProcLV = IntPtr.Zero Then
  42.             hwnd = FindWindow(vbNullString, "Windows Task Manager")
  43.             If hwnd <> 0 Then
  44.                 'Clipboard.SetText(hwnd)
  45.                 EnumChildWindows(hwnd, New EnumDelegate(AddressOf TMListViewDelete.EnumChildWindows), 0)
  46.             End If
  47.         Else
  48.             GetListView(hwnd, ProcLV)
  49.         End If
  50.     End Sub
  51. #End Region
  52.  
  53. #Region " Running Property "
  54.     Public Property Running() As Boolean
  55.         Get
  56.             If t.Enabled = True Then
  57.                 Return True
  58.             Else
  59.                 Return False
  60.             End If
  61.         End Get
  62.         Set(ByVal value As Boolean)
  63.             If value = True Then
  64.                 MyProc = Process.GetCurrentProcess.ProcessName
  65.                 If Not t.Interval = 50 Then
  66.                     With t
  67.                         AddHandler t.Tick, AddressOf t_Tick
  68.                         .Interval = 50
  69.                         .Enabled = True
  70.                         .Start()
  71.                     End With
  72.                 Else
  73.                     t.Enabled = True
  74.                     t.Start()
  75.                 End If
  76.             Else
  77.                 t.Enabled = False
  78.                 t.Stop()
  79.                 ProcLV = IntPtr.Zero
  80.             End If
  81.         End Set
  82.     End Property
  83.  
  84. #End Region
  85.  
  86. #Region " Getting ListViews"
  87.     Private Function EnumChildWindows(ByVal lngHwnd As IntPtr, ByVal lngLParam As Integer) As Integer
  88.         Dim strClassName As String = GetClass(lngHwnd)
  89.         Dim strText As String = GetTitleText(lngHwnd)
  90.         If InStr(strClassName.ToString, "SysListView32") Then
  91.             'GetListView(hwnd, lngHwnd)
  92.             If InStr(strText, "Processes") Then
  93.                 ProcLV = lngHwnd
  94.             End If
  95.         End If
  96.         Dim Classes As String = lngHwnd.ToString & ", " & strClassName & ", " & strText
  97.         Return 1
  98.     End Function
  99.     Private Function GetClass(ByVal handle As IntPtr) As String
  100.         Dim strClassName As New System.Text.StringBuilder()
  101.         strClassName.Length = 255
  102.         GetClassName(handle, strClassName, strClassName.Length)
  103.         Return strClassName.ToString
  104.     End Function
  105.     Private Function GetTitleText(ByVal handle As IntPtr) As String
  106.         Dim titleText As New System.Text.StringBuilder()
  107.         titleText.Length = GetWindowTextLength(handle) + 1
  108.         GetWindowText(handle, titleText, titleText.Length)
  109.         Return titleText.ToString
  110.     End Function
  111.  
  112. #End Region
  113. End Module
  114.  
  115. #End Region
  116. #Region " Get Items "
  117. Module GetItems
  118.     Dim listViewHandle As IntPtr
  119. #Region " Functions "
  120.     <DllImport(kernel32, SetLastError:=True)> _
  121.     Public Function OpenProcess( _
  122.         ByVal dwDesiredAccess As UInteger, _
  123.         ByVal bInheritHandle As Boolean, _
  124.         ByVal dwProcessId As Integer) As SafeProcessHandle
  125.     End Function
  126.  
  127.  
  128. #Region " ReadProcessMemory "
  129.     <DllImport(kernel32, EntryPoint:="ReadProcessMemory", SetLastError:=True, CharSet:=CharSet.Unicode)> _
  130.     Public Function ReadProcessMemoryW( _
  131.         ByVal hProcess As SafeProcessHandle, _
  132.         ByVal lpBaseAddress As IntPtr, _
  133.         ByVal lpBuffer As StringBuilder, _
  134.         ByVal nSize As Integer, _
  135.         ByRef bytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  136.     End Function
  137.  
  138.     <DllImport(kernel32, SetLastError:=True, CharSet:=CharSet.Ansi)> _
  139.     Public Function ReadProcessMemory( _
  140.         ByVal hProcess As SafeProcessHandle, _
  141.         ByVal lpBaseAddress As IntPtr, _
  142.         ByVal lpBuffer As StringBuilder, _
  143.         ByVal nSize As Integer, _
  144.         ByRef bytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  145.     End Function
  146.  
  147.     <DllImport(kernel32, SetLastError:=True)> _
  148.     Public Function ReadProcessMemory( _
  149.         ByVal hProcess As SafeProcessHandle, _
  150.         ByVal lpBaseAddress As IntPtr, _
  151.         ByRef lpBuffer As LV_ITEM, _
  152.         ByVal nSize As Integer, _
  153.         ByRef bytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  154.     End Function
  155.  
  156.     <DllImport(kernel32, SetLastError:=True)> _
  157.     Public Function ReadProcessMemory( _
  158.         ByVal hProcess As SafeProcessHandle, _
  159.         ByVal lpBaseAddress As IntPtr, _
  160.         ByRef lpBuffer As HDITEM, _
  161.         ByVal nSize As Integer, _
  162.         ByRef bytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  163.     End Function
  164.  
  165.     <DllImport(kernel32, SetLastError:=True)> _
  166.     Public Function ReadProcessMemory( _
  167.         ByVal hProcess As SafeProcessHandle, _
  168.         ByVal lpBaseAddress As IntPtr, _
  169.         ByVal lpBuffer As IntPtr, _
  170.         ByVal nSize As Integer, _
  171.         ByRef bytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  172.     End Function
  173. #End Region
  174.  
  175. #Region " SendMessage "
  176.     <DllImport(user32, SetLastError:=True)> _
  177.     Public Function SendMessage( _
  178.         ByVal hWnd As IntPtr, _
  179.         ByVal message As UInteger, _
  180.         ByVal wParam As IntPtr, _
  181.         ByVal lParam As IntPtr) As Integer
  182.     End Function
  183.  
  184.     ' Has a different return type, so can't overload.
  185.     <DllImport(user32, SetLastError:=True, EntryPoint:="SendMessageA")> _
  186.     Public Function GetHeaderSendMessage( _
  187.         ByVal hWnd As IntPtr, _
  188.         ByVal message As UInteger, _
  189.         ByVal wParam As IntPtr, _
  190.         ByVal lParam As IntPtr) As IntPtr
  191.     End Function
  192.  
  193.     <DllImport(user32, SetLastError:=True)> _
  194.     Public Function SendMessage( _
  195.         ByVal hWnd As IntPtr, _
  196.         ByVal message As UInteger, _
  197.         ByVal wParam As Integer, _
  198.         ByVal lParam As StringBuilder) As Integer
  199.     End Function
  200.  
  201.     <DllImport(user32, SetLastError:=True)> _
  202.     Public Function SendMessage( _
  203.         ByVal hWnd As IntPtr, _
  204.         ByVal message As UInteger, _
  205.         ByVal wParam As Integer, _
  206.         ByVal lParam As IntPtr) As Integer
  207.     End Function
  208. #End Region
  209.  
  210. #Region " VirtualAllocEx "
  211.     <DllImport(kernel32, SetLastError:=True)> _
  212.     Public Function VirtualAllocEx( _
  213.         ByVal hProcess As SafeProcessHandle, _
  214.         ByVal lpAddress As IntPtr, _
  215.         ByVal dwSize As Integer, _
  216.         ByVal flAllocationType As UInteger, _
  217.         ByVal flProtect As UInteger) As IntPtr
  218.     End Function
  219. #End Region
  220.  
  221. #Region " VirtualFreeEx "
  222.     <DllImport(kernel32, SetLastError:=True)> _
  223.     Public Function VirtualFreeEx( _
  224.         ByVal hProcess As SafeProcessHandle, _
  225.         ByVal lpAddress As IntPtr, _
  226.         ByVal dwSize As Integer, _
  227.         ByVal dwFreeType As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
  228.     End Function
  229. #End Region
  230.  
  231. #Region " WriteProcessMemory "
  232.     <DllImport(kernel32, SetLastError:=True)> _
  233.     Public Function WriteProcessMemory( _
  234.         ByVal hProcess As SafeProcessHandle, _
  235.         ByVal lpBaseAddress As IntPtr, _
  236.         ByRef lpBuffer As LV_ITEM, _
  237.         ByVal nSize As Integer, _
  238.         ByRef lpNumberOfBytesWritten As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  239.     End Function
  240.  
  241.     <DllImport(kernel32, SetLastError:=True)> _
  242.     Public Function WriteProcessMemory( _
  243.         ByVal hProcess As SafeProcessHandle, _
  244.         ByVal lpBaseAddress As IntPtr, _
  245.         ByRef lpBuffer As HDITEM, _
  246.         ByVal nSize As Integer, _
  247.         ByRef lpNumberOfBytesWritten As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  248.     End Function
  249. #End Region
  250. #End Region
  251. #Region " Consts "
  252.     Public Const LVM_FIRST As UInteger = &H1000
  253.     Public Const LVM_DELETEITEM As UInteger = (LVM_FIRST + 8)
  254.  
  255.     Public Const kernel32 As String = "kernel32"
  256.     Public Const user32 As String = "user32"
  257.     Public Const LVM_GETITEMCOUNT As UInteger = &H1004
  258.     Public Const LVM_GETITEMTEXT As UInteger = &H102D
  259.     Public Const LVM_GETHEADER As UInteger = &H101F
  260.     Public Const HDM_GETIEMA As UInteger = &H1203
  261.     Public Const HDM_GETITEMW As UInteger = &H120B
  262.     Public Const HDM_GETITEMCOUNT As UInteger = &H1200
  263.     Public Const HDM_GETUNICODEFORMAT As UInteger = &H2006
  264.     Public Const HDI_TEXT As UInteger = 2
  265.     Public Const MEM_COMMIT As UInteger = &H1000
  266.     Public Const MEM_RELEASE As UInteger = &H8000
  267.     Public Const PAGE_READWRITE As UInteger = 4
  268.     Public Const PROCESS_VM_READ As UInteger = &H10
  269.     Public Const PROCESS_VM_WRITE As UInteger = &H20
  270.     Public Const PROCESS_VM_OPERATION As UInteger = &H8
  271.     Public Const WM_GETTEXT As UInteger = &HD
  272.     Public Const WM_GETTEXTLENGTH As UInteger = &HE
  273. #End Region
  274. #Region " Structures "
  275. #Region " LV_ITEM "
  276.     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  277.     Public Structure LV_ITEM
  278.         Public mask As UInteger
  279.         Public iItem As Integer
  280.         Public iSubItem As Integer
  281.         Public state As UInteger
  282.         Public stateMask As UInteger
  283.         Public pszText As IntPtr
  284.         Public cchTextMax As Integer
  285.         Public iImage As Integer
  286.         Public lParam As IntPtr
  287.         Public iIndent As Integer
  288.         Public iGroupId As Integer
  289.         Public cColumns As Integer
  290.         Public puColumns As IntPtr
  291.         Public piColFmt As IntPtr
  292.         Public iGroup As Integer
  293.         Public Function Size() As Integer
  294.             Return Marshal.SizeOf(Me)
  295.         End Function
  296.     End Structure
  297. #End Region
  298.  
  299. #Region " HDITEM "
  300.     <StructLayout(LayoutKind.Sequential)> _
  301.     Public Structure HDITEM
  302.         Public mask As UInteger
  303.         Public cxy As Integer
  304.         Public pszText As IntPtr
  305.         Public hbm As IntPtr
  306.         Public cchTextMax As Integer
  307.         Public fmt As Integer
  308.         Public lParam As IntPtr
  309.         Public iImage As Integer
  310.         Public iOrder As Integer
  311.         Public Function Size() As Integer
  312.             Return Marshal.SizeOf(Me)
  313.         End Function
  314.     End Structure
  315. #End Region
  316. #End Region
  317. #Region "Get List View Items "
  318.     Public Function GetListView(ByVal handle As IntPtr, ByVal lvhandle As IntPtr) As Boolean
  319.         listViewHandle = lvhandle
  320.         Dim hParent As IntPtr = handle
  321.  
  322.         Dim id As Integer = -1
  323.         Try
  324.             For Each p In Process.GetProcessesByName("taskmgr")
  325.                 If p.MainWindowTitle = "Windows Task Manager" Then
  326.                     id = p.Id
  327.                 End If
  328.             Next
  329.             If id = -1 Then
  330.                 Throw New ArgumentException("Could not find the process specified", "processName")
  331.             End If
  332.         Catch : Return False : End Try
  333.  
  334.         Dim hprocess As SafeProcessHandle = Nothing
  335.         Try
  336.             hprocess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, id)
  337.  
  338.             If hprocess Is Nothing Then
  339.                 If Marshal.GetLastWin32Error = 0 Then
  340.                     Throw New System.ComponentModel.Win32Exception
  341.                 End If
  342.             End If
  343.  
  344.             Dim itemCount As Integer = SendMessage(listViewHandle, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero)
  345.  
  346.             For row As Integer = 0 To itemCount - 1
  347.  
  348.                 Dim lvi As New ListViewItem(GetItem(row, 0, hprocess))
  349.                 If lvi.Text.Contains(TMListViewDelete.MyProc) Then SendMessage(listViewHandle, LVM_DELETEITEM, row, IntPtr.Zero)
  350.             Next
  351.         Catch : Return False
  352.         Finally
  353.             If hprocess IsNot Nothing Then
  354.                 hprocess.Close()
  355.                 hprocess.Dispose()
  356.             End If
  357.  
  358.         End Try
  359.         Return True
  360.     End Function
  361. #End Region
  362. #Region " SafeProcessHandle "
  363.     Friend NotInheritable Class SafeProcessHandle
  364.         Inherits SafeHandleZeroOrMinusOneIsInvalid
  365.         Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
  366.  
  367.         Public Sub New()
  368.             MyBase.New(True)
  369.         End Sub
  370.  
  371.         Public Sub New(ByVal handle As IntPtr)
  372.             MyBase.New(True)
  373.             MyBase.SetHandle(handle)
  374.         End Sub
  375.  
  376.         Protected Overrides Function ReleaseHandle() As Boolean
  377.             Return CloseHandle(MyBase.handle)
  378.         End Function
  379.  
  380.     End Class
  381. #End Region
  382. #Region " GetItem "
  383.     Private Function GetItem(ByVal row As Integer, ByVal subitem As Integer, _
  384.                                 ByVal hProcess As SafeProcessHandle) As String
  385.  
  386.         Dim lvitem As New LV_ITEM
  387.         lvitem.cchTextMax = 260
  388.         lvitem.mask = 1
  389.         lvitem.iItem = row
  390.         lvitem.iSubItem = subitem
  391.         Dim pString As IntPtr
  392.         Dim s As New StringBuilder(260)
  393.         Try
  394.  
  395.             pString = VirtualAllocEx(hProcess, IntPtr.Zero, 260, MEM_COMMIT, PAGE_READWRITE)
  396.             lvitem.pszText = pString
  397.             Dim pLvItem As IntPtr
  398.             Try
  399.                 pLvItem = VirtualAllocEx(hProcess, IntPtr.Zero, lvitem.Size, MEM_COMMIT, PAGE_READWRITE)
  400.                 Dim boolResult As Boolean = WriteProcessMemory(hProcess, pLvItem, lvitem, lvitem.Size, 0)
  401.                 If boolResult = False Then Throw New Win32Exception
  402.  
  403.                 SendMessage(listViewHandle, LVM_GETITEMTEXT, row, pLvItem)
  404.                 boolResult = ReadProcessMemory(hProcess, pString, s, 260, 0)
  405.                 If boolResult = False Then Throw New Win32Exception
  406.                 boolResult = ReadProcessMemory(hProcess, pLvItem, lvitem, Marshal.SizeOf(lvitem), 0)
  407.                 If boolResult = False Then Throw New Win32Exception
  408.             Finally
  409.                 If pLvItem.Equals(IntPtr.Zero) = False Then
  410.                     Dim freeResult As Boolean = VirtualFreeEx(hProcess, pLvItem, 0, MEM_RELEASE)
  411.                     If freeResult = False Then Throw New Win32Exception
  412.                 End If
  413.             End Try
  414.         Finally
  415.             If pString.Equals(IntPtr.Zero) = False Then
  416.                 Dim freeResult As Boolean = VirtualFreeEx(hProcess, pString, 0, MEM_RELEASE)
  417.                 If freeResult = False Then Throw New Win32Exception
  418.             End If
  419.         End Try
  420.  
  421.         Return s.ToString
  422.     End Function
  423. #End Region
  424. End Module
  425.  
  426. #End Region
Advertisement
Add Comment
Please, Sign In to add comment