Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am developing an application in c#, I need to find index of any running process from Task Manager.

My code is working fine in listview control present in same application but as it goes for Task Manager it show -1, i.e. not found.

Here is my code :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace NonKilltest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }




        [StructLayout(LayoutKind.Sequential)]
        public unsafe struct LVFINDINFO
        {
           
            public UInt32 flags;
            [MarshalAs(UnmanagedType.LPStr)]
            public string psz;
            public Int32 lParam;
            public POINT pt;
            public UInt32 vkDirection;
        }
        [StructLayout(LayoutKind.Sequential)]
        public unsafe struct POINT
        {
            public long x;
            public long y;
            public POINT(int X, int Y)
            {
                this.x = X;
                this.y = Y;
            }
        }

       
        const Int32 LVM_FINDITEMW = 0x1000 + 13;
        const Int32 LVFI_PARAM = 1;
        const Int32 LVFI_STRING = 2;
        const Int32 LVFI_PARTIAL = 8;
        const Int32 LVFI_WRAP = 32;

        [DllImport("user32.dll")]
        public unsafe static extern int SendMessage(IntPtr inWindow, uint inMsg, int wParam, IntPtr lParam);


        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr LocalAlloc(uint uFlags, UIntPtr uBytes);


        private void button1_Click(object sender, EventArgs e)
        {
            LVFINDINFO xFindInfo = new LVFINDINFO();
            xFindInfo.flags = LVFI_STRING;
            xFindInfo.pt = new POINT(0, 0);
            xFindInfo.vkDirection = 0x25 | 0x28 |0x27 | 0x22;
            xFindInfo.psz = "firefox.exe";
            IntPtr lhWndParent = FindWindow(null, "Windows Task Manager");
            int i;
            IntPtr lhWndDialog = new IntPtr();
            IntPtr lhWndProcessList = new IntPtr();
            IntPtr lhWndProcessHeader = new IntPtr();
            for (i = 1; i <= 7; i++)
            {
                lhWndDialog = FindWindowEx(lhWndParent, lhWndDialog, null, null);
                if (lhWndProcessList.ToInt32() == 0)
                    lhWndProcessList = FindWindowEx(lhWndDialog, new IntPtr(0), "SysListView32", "Processes");
                if (lhWndProcessHeader.ToInt32() == 0)
                    lhWndProcessHeader = FindWindowEx(lhWndProcessList, new IntPtr(0), "SysHeader32", null);
            }
           IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(xFindInfo));
            Marshal.StructureToPtr(xFindInfo, ptr, false);

            // The below line works but if I replace listview1.Handle with lhWndProcessList then it returns -1.

            // Problem at this point
            int index = SendMessage(listView1.Handle, LVM_FINDITEMW, -1, ptr);


            Marshal.FreeHGlobal(ptr);
            MessageBox.Show(index.ToString());
        }
    }
}

What I am doing wrong, no idea!!!

Thanks in advance for any help.



Regards

Vikas
Posted
Updated 30-Nov-11 18:29pm
v2
Comments
[no name] 1-Dec-11 0:34am    
EDIT: added "pre" tag

1 solution

Hi friends,

First of all I would like say thanks to all of you who has given reply over this post.

I have resolved LVM_FINDITEM problem, now I am able to hide process from task manager or delete process from task manager.

I have uploaded the code written in C#.

For anyone's help I have uploaded the code :
http://www.rajtuhin.com/CodeStore/CodePages/CodePage.aspx?codeid=89

Now I have resolved LVM_FindItem issues.


Thanks

Vikas
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900