10
Reply

LVM_FINDITEM is not working for Task Manager

vikas khare

vikas khare

Dec 1 2011 12:47 AM
2.6k
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 :

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

Answers (10)