Esempio n. 1
0
        public void CheckListViewItem(IntPtr handle, int index)
        {
            int    vItemCount;
            int    ColumnCount;
            IntPtr vHandle            = handle;
            uint   vNumberOfBytesRead = 0;

            if (vHandle == IntPtr.Zero)
            {
                return;
            }

            //选中Listview的某个Item 测试已工作
            vItemCount = NativeMethods.SendMessageW(vHandle, NativeMethods.LVM_GETITEMCOUNT, 0, 0);
            IntPtr handleHeader = new IntPtr(NativeMethods.SendMessageW(vHandle, NativeMethods.LVM_GETHEADER, 0, 0));

            ColumnCount = NativeMethods.SendMessageW(handleHeader, NativeMethods.HDM_GETITEMCOUNT, 0, 0);
            uint vProcessId;

            NativeMethods.GetWindowThreadProcessId(vHandle, out vProcessId);
            IntPtr vProcess = NativeMethods.OpenProcess(NativeMethods.PROCESS_VM_OPERATION | NativeMethods.PROCESS_VM_READ | NativeMethods.PROCESS_VM_WRITE, false, vProcessId);
            IntPtr vPointer = NativeMethods.VirtualAllocEx(vProcess, IntPtr.Zero, 4096, NativeMethods.MEM_RESERVE | NativeMethods.MEM_COMMIT, NativeMethods.PAGE_READWRITE);

            NativeMethods.LVITEM[] vItem = new NativeMethods.LVITEM[1];
            //vItem[0].state = NativeMethods.LVIS_SELECTED | NativeMethods.LVIS_FOCUSED | NativeMethods.LVIS_ACTIVATING;
            vItem[0].state     = 0;
            vItem[0].stateMask = NativeMethods.LVIS_SELECTED | NativeMethods.LVIS_FOCUSED;

            NativeMethods.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(NativeMethods.LVITEM)), ref vNumberOfBytesRead);

            for (int i = 0; i < vItemCount; i++)
            {
                NativeMethods.SendMessageW(handle, NativeMethods.LVM_SETITEMSTATE, (uint)i, vPointer.ToInt32());
            }
            NativeMethods.VirtualFreeEx(vProcess, vPointer, 0, NativeMethods.MEM_RELEASE);

            vItem[0].state     = NativeMethods.LVIS_SELECTED | NativeMethods.LVIS_FOCUSED | NativeMethods.LVIS_ACTIVATING;
            vItem[0].stateMask = NativeMethods.LVIS_SELECTED | NativeMethods.LVIS_FOCUSED | NativeMethods.LVIS_ACTIVATING;
            vPointer           = NativeMethods.VirtualAllocEx(vProcess, IntPtr.Zero, 4096, NativeMethods.MEM_RESERVE | NativeMethods.MEM_COMMIT, NativeMethods.PAGE_READWRITE);
            NativeMethods.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(NativeMethods.LVITEM)), ref vNumberOfBytesRead);
            NativeMethods.SendMessageW(handle, NativeMethods.LVM_SETITEMSTATE, (uint)index, vPointer.ToInt32());
        }
Esempio n. 2
0
        /// <summary>
        /// 获取指定句柄的listview内容
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public DataTable GetListViewContent(IntPtr handle)
        {
            AccessWin aw = new AccessWin();
            int       vItemCount;
            int       ColumnCount;

            if (handle == IntPtr.Zero)
            {
                return(null);
            }
            vItemCount = NativeMethods.SendMessageW(handle, NativeMethods.LVM_GETITEMCOUNT, 0, 0);
            IntPtr handleHeader = new IntPtr(NativeMethods.SendMessageW(handle, NativeMethods.LVM_GETHEADER, 0, 0));

            ColumnCount = NativeMethods.SendMessageW(handleHeader, NativeMethods.HDM_GETITEMCOUNT, 0, 0);
            uint vProcessId;

            NativeMethods.GetWindowThreadProcessId(handle, out vProcessId);
            IntPtr    vProcess = NativeMethods.OpenProcess(NativeMethods.PROCESS_VM_OPERATION | NativeMethods.PROCESS_VM_READ | NativeMethods.PROCESS_VM_WRITE, false, vProcessId);
            IntPtr    vPointer = NativeMethods.VirtualAllocEx(vProcess, IntPtr.Zero, 4096, NativeMethods.MEM_RESERVE | NativeMethods.MEM_COMMIT, NativeMethods.PAGE_READWRITE);
            int       ttt      = Marshal.SizeOf(typeof(NativeMethods.NMHDR));
            DataTable dt       = new DataTable();

            try
            {
                for (int j = 0; j < ColumnCount; j++)
                {
                    byte[] vBuffer = new byte[256];
                    NativeMethods.LVHEADER[] vHeader = new NativeMethods.LVHEADER[1];
                    vHeader[0]            = new NativeMethods.LVHEADER();
                    vHeader[0].mask       = NativeMethods.LVCF_TEXT;
                    vHeader[0].cchTextMax = 256;
                    vHeader[0].iSubItem   = 0;
                    vHeader[0].pszText    = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(NativeMethods.LVHEADER)));
                    uint vNumberOfBytesRead = 0;
                    NativeMethods.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vHeader, 0), Marshal.SizeOf(typeof(NativeMethods.LVHEADER)), ref vNumberOfBytesRead);
                    NativeMethods.SendMessageW(handle, NativeMethods.LVM_GETCOLUMN, (uint)j, vPointer.ToInt32());
                    NativeMethods.ReadProcessMemory(vProcess, (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(NativeMethods.LVHEADER))), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0), vBuffer.Length, ref vNumberOfBytesRead);
                    string vText = Marshal.PtrToStringAnsi(Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0));
                    dt.Columns.Add(vText);
                }

                for (int j = 0; j < ColumnCount; j++)
                {
                }

                for (int i = 0; i < vItemCount; i++)
                {
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < ColumnCount; j++)
                    {
                        byte[] vBuffer = new byte[256];

                        NativeMethods.LVITEM[] vItem = new NativeMethods.LVITEM[1];
                        vItem[0]            = new NativeMethods.LVITEM();
                        vItem[0].mask       = NativeMethods.LVIF_TEXT;
                        vItem[0].iItem      = i;
                        vItem[0].iSubItem   = j;
                        vItem[0].cchTextMax = vBuffer.Length;
                        vItem[0].pszText    = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(NativeMethods.LVITEM)));
                        uint vNumberOfBytesRead = 0;
                        NativeMethods.WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(NativeMethods.LVITEM)), ref vNumberOfBytesRead);
                        NativeMethods.SendMessageW(handle, NativeMethods.LVM_GETITEMW, (uint)i, vPointer.ToInt32());
                        NativeMethods.ReadProcessMemory(vProcess, (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(NativeMethods.LVITEM))), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0), vBuffer.Length, ref vNumberOfBytesRead);
                        string vText = Marshal.PtrToStringUni(Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0));
                        dr[j] = vText;
                    }
                    dt.Rows.Add(dr);
                }
            }
            finally
            {
                NativeMethods.VirtualFreeEx(vProcess, vPointer, 0, NativeMethods.MEM_RELEASE);
                NativeMethods.CloseHandle(vProcess);
            }
            return(dt);
        }