using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 读取其他软件listview控件的内容
{
public partial class Form1 : Form
{
int hwnd; //窗口句柄
int process;//进程句柄
int pointer;
private const uint LVM_FIRST = 0x1000;
private const uint LVM_GETHEADER = LVM_FIRST + ;
private const uint LVM_GETITEMCOUNT = LVM_FIRST + ;//获取列表行数
private const uint LVM_GETITEMTEXT = LVM_FIRST + ;//获取列表内的内容
private const uint LVM_GETITEMW = LVM_FIRST + ; private const uint HDM_GETITEMCOUNT = 0x1200;//获取列表列数 private const uint PROCESS_VM_OPERATION = 0x0008;//允许函数VirtualProtectEx使用此句柄修改进程的虚拟内存
private const uint PROCESS_VM_READ = 0x0010;//允许函数访问权限
private const uint PROCESS_VM_WRITE = 0x0020;//允许函数写入权限 private const uint MEM_COMMIT = 0x1000;//为特定的页面区域分配内存中或磁盘的页面文件中的物理存储
private const uint MEM_RELEASE = 0x8000;
private const uint MEM_RESERVE = 0x2000;//保留进程的虚拟地址空间,而不分配任何物理存储 private const uint PAGE_READWRITE = ; private int LVIF_TEXT = 0x0001; [DllImport("user32.dll")]//查找窗口
private static extern int FindWindow(
string strClassName, //窗口类名
string strWindowName //窗口标题
); [DllImport("user32.dll")]//在窗口列表中寻找与指定条件相符的第一个子窗口
private static extern int FindWindowEx(
int hwndParent, // handle to parent window
int hwndChildAfter, // handle to child window
string className, //窗口类名
string windowName // 窗口标题
);
[DllImport("user32.DLL")]
private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]//找出某个窗口的创建者(线程或进程),返回创建者的标志符
private static extern int GetWindowThreadProcessId(int hwnd, out int processId);
[DllImport("kernel32.dll")]//打开一个已存在的进程对象,并返回进程的句柄
private static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int processId);
[DllImport("kernel32.dll")]//为指定的进程分配内存地址:成功则返回分配内存的首地址
private static extern int VirtualAllocEx(int hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]//从指定内存中读取字节集数据
private static extern bool ReadProcessMemory(
int hProcess, //被读取者的进程句柄
int lpBaseAddress,//开始读取的内存地址
IntPtr lpBuffer, //数据存储变量
int nSize, //要写入多少字节
ref uint vNumberOfBytesRead//读取长度
);
[DllImport("kernel32.dll")]//将数据写入内存中
private static extern bool WriteProcessMemory(
int hProcess,//由OpenProcess返回的进程句柄
int lpBaseAddress, //要写的内存首地址,再写入之前,此函数将先检查目标地址是否可用,并能容纳待写入的数据
IntPtr lpBuffer, //指向要写的数据的指针
int nSize, //要写入的字节数
ref uint vNumberOfBytesRead
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int handle);
[DllImport("kernel32.dll")]//在其它进程中释放申请的虚拟内存空间
private static extern bool VirtualFreeEx(
int hProcess,//目标进程的句柄,该句柄必须拥有PROCESS_VM_OPERATION的权限
int lpAddress,//指向要释放的虚拟内存空间首地址的指针
uint dwSize,
uint dwFreeType//释放类型
);
/// <summary>
/// LVITEM结构体,是列表视图控件的一个重要的数据结构
/// 占空间:4(int)x7=28个byte
/// </summary>
private struct LVITEM //结构体
{
public int mask;//说明此结构中哪些成员是有效的
public int iItem;//项目的索引值(可以视为行号)从0开始
public int iSubItem; //子项的索引值(可以视为列号)从0开始
public int state;//子项的状态
public int stateMask; //状态有效的屏蔽位
public IntPtr pszText; //主项或子项的名称
public int cchTextMax;//pszText所指向的缓冲区大小
} public Form1()
{
InitializeComponent();
} /// <summary>
/// LV列表总行数
/// </summary>
private int ListView_GetItemRows(int handle)
{
return SendMessage(handle, LVM_GETITEMCOUNT, , );
}
/// <summary>
/// LV列表总列数
/// </summary>
private int ListView_GetItemCols(int handle)
{
return SendMessage(handle, HDM_GETITEMCOUNT, , );
} private void button1_Click(object sender, EventArgs e)
{
int headerhwnd; //listview控件的列头句柄
int rows, cols; //listview控件中的行列数
int processId; //进程pid hwnd = FindWindow("#32770", "Windows 任务管理器");
hwnd = FindWindowEx(hwnd, , "#32770", null);
hwnd = FindWindowEx(hwnd, , "SysListView32", null);//进程界面窗口的句柄,通过SPY获取
//hwnd = 0xC1CC0;
headerhwnd = SendMessage(hwnd, LVM_GETHEADER, , );//listview的列头句柄 rows = ListView_GetItemRows(hwnd);//总行数,即进程的数量
cols = ListView_GetItemCols(headerhwnd);//列表列数
//cols = 2;
GetWindowThreadProcessId(hwnd, out processId); //打开并插入进程
process = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, processId);
//申请代码的内存区,返回申请到的虚拟内存首地址
pointer = VirtualAllocEx(process, IntPtr.Zero, , MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
string[,] tempStr;//二维数组
string[] temp = new string[cols]; tempStr = GetListViewItmeValue(rows, cols);//将要读取的其他程序中的ListView控件中的文本内容保存到二维数组中 listView1.Items.Clear();//清空LV控件信息
//输出数组中保存的其他程序的LV控件信息
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
temp[j] = tempStr[i, j];
}
ListViewItem lvi = new ListViewItem(temp);
listView1.Items.Add(lvi);
}
} /// <summary>
/// 从内存中读取指定的LV控件的文本内容
/// </summary>
/// <param name="rows">要读取的LV控件的行数</param>
/// <param name="cols">要读取的LV控件的列数</param>
/// <returns>取得的LV控件信息</returns>
private string[,] GetListViewItmeValue(int rows, int cols)
{
string[,] tempStr = new string[rows, cols];//二维数组:保存LV控件的文本信息
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
byte[] vBuffer = new byte[];//定义一个临时缓冲区
LVITEM[] vItem = new LVITEM[];
vItem[].mask = LVIF_TEXT;//说明pszText是有效的
vItem[].iItem = i; //行号
vItem[].iSubItem = j; //列号
vItem[].cchTextMax = vBuffer.Length;//所能存储的最大的文本为256字节
vItem[].pszText = (IntPtr)((int)pointer + Marshal.SizeOf(typeof(LVITEM)));
uint vNumberOfBytesRead = ; //把数据写到vItem中
//pointer为申请到的内存的首地址
//UnsafeAddrOfPinnedArrayElement:获取指定数组中指定索引处的元素的地址
WriteProcessMemory(process, pointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, ), Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead); //发送LVM_GETITEMW消息给hwnd,将返回的结果写入pointer指向的内存空间
SendMessage(hwnd, LVM_GETITEMW, i, pointer); //从pointer指向的内存地址开始读取数据,写入缓冲区vBuffer中
ReadProcessMemory(process, ((int)pointer + Marshal.SizeOf(typeof(LVITEM))), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, ), vBuffer.Length, ref vNumberOfBytesRead); string vText = Encoding.Unicode.GetString(vBuffer, , (int)vNumberOfBytesRead); ;
tempStr[i, j] = vText;
}
}
VirtualFreeEx(process, pointer, , MEM_RELEASE);//在其它进程中释放申请的虚拟内存空间,MEM_RELEASE方式很彻底,完全回收
CloseHandle(process);//关闭打开的进程对象
return tempStr;
} }
}

读取其他软件listview控件的内容的更多相关文章

  1. ListView 控件与 内容

    1)由控件获取内容:ListViewItem item = Utilities.GetVisualParent<ListViewItem>(chx); if (item == null) ...

  2. 如何清空android ListView控件的内容

    第一种方法: listView.setAdapter(null); 第二种方法: listAdapter.clear(); listAdapter.notifyDataSetChanged() ; 满 ...

  3. C#跨进程读取listview控件中的数据

    http://www.cnblogs.com/Charltsing/p/slv32.html 欢迎交流:QQ564955427 读取标准的32位listview控件中的数据,网上已经有很多代码了.今天 ...

  4. 【VC版】如何获取其他进程中ListView控件中的内容

    如果需要C#版的,可以看下我之前写的:C#如何获取其他程序ListView控件中的内容 获取其他进程的数据需要使用到以下几个函数: VirtualAllocEx() VirtualFreeEx() W ...

  5. ListView控件--2016年12月9日

    ListView属性 ListView   名称 说明 AccessKey 重写 WebControl.AccessKey 属性. 不支持将此属性设置 ListView 控件.(覆盖 WebContr ...

  6. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>

    分页     在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...

  7. 【VB技巧】VB ListView 控件功能使用详解

    来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...

  8. WP8.1开发中ListView控件加载图列表的简单使用(1)

    我也是刚接触WP编程没几个月,就是在这段时间一直闲着没事,然后又比较喜欢WP这款系统,就学习了WP这方面的开发言语,自学是很困难的,掌握这方面的资料不多,很初级,就是自己在网上找资料学习过程中,看到别 ...

  9. ListView控件使用

    //ListView标头的代码创建方法. ColumnHeader title=new ColumnHeader(); //声明标头,并创建对象. title.Text="标头1名称&quo ...

随机推荐

  1. Node连接MySQL

    http://blog.fens.me/nodejs-mysql-intro/ http://czpae86.iteye.com/blog/1636302 http://www.cnblogs.com ...

  2. 2016 - 1 - 25 第三方网络框架 AFN的简单使用

    AFNetworking 底层是对NSURlSession 和对 NSURLConnect 的包装 1.具体使用方法可以参照github上的主页面,在这里只是举一个文件上传的简单列子 - (void) ...

  3. 判断是否存在哈密顿路--HDU 5424

    题意:给一张无向图,判断是否是哈密顿图. 哈密顿路:经过每个点有且仅有一次的一条通路. 方法:每次找度数最小的点作为起点,然后dfs整个图,看能遍历到的点的数目能否达到n. #include<i ...

  4. 论文ei,sci检索,JCR-SCI分区,中科院分区连接

    https://jcr.incites.thomsonreuters.com/JCRJournalHomeAction.action?SID=B1-bQgax2FJ7EsyZ9muP6O5loc77S ...

  5. appium 执行demo

    appium很早就了解了,一直没有之际操作过,最近把官网的demo搞下来执行了一遍,还是很有意思的 经过测试是可以跑起来的,不过跑得过程中,输入法需要默认为英文的,如果是中文的码输入的时候有点问题,可 ...

  6. C#基础——静态成员,static关键字

    当声明一个类成员为静态时,意味着无论创建多少个类的对象,只会有一个该静态成员的副本. 关键字static意味着只有一个该成员的实例.静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的 ...

  7. 2016年JS面试题目汇总

    1.怎样添加.移除.移动.复制.创建和查找节点? //1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元素 ...

  8. 在Visual Lisp中处理自动化错误

    Handling Automation errors in Visual LISP 翻译自原文Kean's blog:http://through-the-interface.typepad.com/ ...

  9. setAttribute,,,getAttribute,,,,

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. ASP.NET中Cookies的使用

    准备开始写后台代码了,不过刚看到cookies就傻眼了,网上搜集了一些资料,总结了一下,初学者可以看看. 创建COOKIES System.Web.HttpCookie cookie=new Http ...