找回密码
 加入
搜索
查看: 184|回复: 6

[GUI管理] 【已解决】求助,退出代码: 3221226525,这个是什么报错呀?

[复制链接]
发表于 2024-9-11 11:22:39 | 显示全部楼层 |阅读模式
本帖最后由 Cyydcc039485 于 2024-9-14 09:04 编辑

我用autoit创建了一个listview,然后用C#程序获取listview中的项内容,程序执行到getitemtext时,autoit运行的程序自动退出了,退出代码:3221226525。请问下这个具体时什么报错呀?
发表于 2024-9-11 16:24:18 | 显示全部楼层
源代码呢???


二○二四年九月十一日
发表于 2024-9-11 23:52:36 | 显示全部楼层
给出代码,让错误复现,才容易分析
发表于 2024-9-12 20:50:07 | 显示全部楼层

你这个写法,获取当前进程的应该没有问题,获取其它程序的控件内容,你要在目标进程内申请空间,并把结构体和缓冲区写进去,发送消息后,在目标进程获取内容。
发表于 2024-9-12 21:36:52 | 显示全部楼层
用下面的代码试试
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Program
{
    class Program
    {
        [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("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint processId);

        [DllImport("user32.dll", SetLastError = true)]
        static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out int lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool CloseHandle(IntPtr hObject);

        const uint PROCESS_VM_OPERATION = 0x0008;
        const uint PROCESS_VM_READ = 0x0010;
        const uint PROCESS_VM_WRITE = 0x0020;
        const uint MEM_COMMIT = 0x1000;
        const uint MEM_RELEASE = 0x8000;
        const uint PAGE_READWRITE = 0x04;

        const uint LVIF_TEXT = 1;
        const uint LVM_FIRST = 0x1000;
        const uint LVM_GETITEMCOUNT = 0x1004;
        const uint LVM_GETITEMTEXT = LVM_FIRST + 115;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct LVITEM
        {
            public uint mask;
            public int iItem;
            public int iSubItem;
            public uint state;
            public uint stateMask;
            public IntPtr pszText;
            public int cchTextMax;
            public int iImage;
            public IntPtr lParam;
            public int iIndent;
            public int iGroupId;
            public uint cColumns;
            public IntPtr puColumns;
            public IntPtr piColFmt;
            public int iGroup;
        }

        static string GetListViewItemText(IntPtr hWndListView, int itemIndex, int subItemIndex)
        {
            GetWindowThreadProcessId(hWndListView, out uint processId);

            IntPtr hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, (int)processId);
            if (hProcess == IntPtr.Zero)
            {
                Console.WriteLine("无法打开目标进程");
                return string.Empty;
            }

            int textBufferSize = 4096;
            int totalSize = Marshal.SizeOf(typeof(LVITEM)) + textBufferSize;

            IntPtr memory = VirtualAllocEx(hProcess, IntPtr.Zero, totalSize, MEM_COMMIT, PAGE_READWRITE);
            if (memory == IntPtr.Zero)
            {
                Console.WriteLine("无法分配内存");
                CloseHandle(hProcess);
                return string.Empty;
            }

            IntPtr textMemory = IntPtr.Add(memory, Marshal.SizeOf(typeof(LVITEM)));

            LVITEM lvItem = new LVITEM
            {
                mask = LVIF_TEXT,
                iItem = itemIndex,
                iSubItem = subItemIndex,
                pszText = textMemory,
                cchTextMax = textBufferSize / 2
            };

            byte[] lvItemBytes = StructureToByteArray(lvItem);
            if (!WriteProcessMemory(hProcess, memory, lvItemBytes, lvItemBytes.Length, out _))
            {
                Console.WriteLine("写入内存失败");
                VirtualFreeEx(hProcess, memory, 0, MEM_RELEASE);
                CloseHandle(hProcess);
                return string.Empty;
            }

            SendMessage(hWndListView, LVM_GETITEMTEXT, itemIndex, memory);

            byte[] buffer = new byte[textBufferSize];
            if (!ReadProcessMemory(hProcess, textMemory, buffer, buffer.Length, out _))
            {
                Console.WriteLine("读取内存失败");
                VirtualFreeEx(hProcess, memory, 0, MEM_RELEASE);
                CloseHandle(hProcess);
                return string.Empty;
            }

            string itemText = Encoding.Unicode.GetString(buffer).TrimEnd('\0');

            VirtualFreeEx(hProcess, memory, 0, MEM_RELEASE);
            CloseHandle(hProcess);

            return itemText;
        }

        static void Main(string[] args)
        {
            IntPtr hWnd = FindWindow(null, "ListView示例");
            if (hWnd == IntPtr.Zero)
            {
                Console.WriteLine("未找到目标窗口.");
                return;
            }

            IntPtr hWndListView = FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", null);
            if (hWndListView == IntPtr.Zero)
            {
                Console.WriteLine("未找到ListView.");
                return;
            }

            int itemCount = SendMessage(hWndListView, LVM_GETITEMCOUNT, 0, (IntPtr)0);
            Console.WriteLine("Item count: " + itemCount);

            for (int i = 0; i < itemCount; i++)
            {
                string sztext = GetListViewItemText(hWndListView, i, 0);
                Console.WriteLine(sztext);
            }
        }

        static byte[] StructureToByteArray(object obj)
        {
            int len = Marshal.SizeOf(obj);
            byte[] arr = new byte[len];
            IntPtr ptr = Marshal.AllocHGlobal(len);
            Marshal.StructureToPtr(obj, ptr, true);
            Marshal.Copy(ptr, arr, 0, len);
            Marshal.FreeHGlobal(ptr);
            return arr;
        }
    }
}

 楼主| 发表于 2024-9-13 16:23:50 | 显示全部楼层
haijie1223 发表于 2024-9-12 21:36
用下面的代码试试

好的呢,我试试看
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-21 08:50 , Processed in 0.080425 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表