获取其他进程中ListView的文本
看到前面有朋友求相关代码,就发出来了~~~
更改其他程序ListView控件中某个Item的内容(原码) http://www.vbgood.com/viewthread.php?tid=72897
1 Option Explicit
2
3 Private Const LVM_FIRST = &H1000
4 Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)
5
6 Private Const LVM_GETITEM = (LVM_FIRST + 5)
7 Private Const LVM_GETSTRINGWIDTH = (LVM_FIRST + 17)
8 Private Const LVM_GETCOLUMN = (LVM_FIRST + 25)
9 Private Const LVM_GETITEMTEXT = (LVM_FIRST + 45)
10 Private Const LVM_GETHEADER = LVM_FIRST + 31
11 Private Const WC_HEADERA = "SysHeader32"
12 Private Const WC_HEADER = WC_HEADERA
13 Private Const HDM_FIRST = &H1200 '// Header messages
14 Private Const HDM_GETITEMCOUNT = (HDM_FIRST + 0)
15 Private Const HDM_ORDERTOINDEX = (HDM_FIRST + 15)
16
17 Private Const PROCESS_QUERY_INFORMATION = 1024
18 Private Const PROCESS_VM_OPERATION = &H8
19 Private Const PROCESS_VM_READ = &H10
20 Private Const PROCESS_VM_WRITE = &H20
21 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
22 Private Const MAX_LVMSTRING As Long = 255 '可根椐读取数据长度设置适当的数值
23 Private Const MEM_COMMIT = &H1000
24 Private Const MEM_RELEASE = &H8000
25 Private Const PAGE_READWRITE = &H4
26 Private Const LVIF_TEXT As Long = &H1
27
28 Private Type LV_ITEMA
29 mask As Long
30 iItem As Long
31 iSubItem As Long
32 state As Long
33 stateMask As Long
34 pszText As Long
35 cchTextMax As Long
36 iImage As Long
37 lParam As Long
38 iIndent As Long
39 End Type
40
41 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
42 Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
43 Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
44 Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
45 Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
46 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
47 Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
48 Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
49 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
50 Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
51
52 Private Sub Command1_Click()
53 Dim lngHwnd As Long
54 Dim lngHwnd1 As Long
55 Dim lngHeaderHwnd As Long
56 Dim lngPId As Long
57 Dim lngRows As Long
58 Dim lngCols As Long
59 Dim lngRow As Long
60 Dim lngCol As Long
61 Dim strItem As String
62
63 lngHwnd1 = FindWindow(vbNullString, "Windows 任务管理器") '获取任务管理器窗口句柄
64 lngHwnd1 = FindWindowEx(lngHwnd1, 0, "#32770", "") '获取选项卡句柄
65 lngHwnd = FindWindowEx(lngHwnd1, 0, "SysListView32", "进程") '获取进程列表框句柄
66 Debug.Print lngHwnd
67
68 lngHeaderHwnd = SendMessage(lngHwnd, LVM_GETHEADER, 0, 0) '获取ListView表头句柄
69 lngRows = SendMessage(lngHwnd, LVM_GETITEMCOUNT, 0, 0) '获取ListView项目数
70
71 If lngHeaderHwnd > 0 Then
72 lngCols = SendMessage(lngHeaderHwnd, HDM_GETITEMCOUNT, 0, 0) '获取ListView表头项目数
73 Else
74 lngCols = 1
75 End If
76 GetWindowThreadProcessId lngHwnd, lngPId '获取与指定窗口关联在一起的一个进程和线程标识符
77 For lngRow = 0 To lngRows - 1
78 strItem = ""
79 For lngCol = 0 To lngCols - 1
80 strItem = strItem & vbTab & GetListviewItem(lngHwnd, lngPId, lngCol, lngRow)
81 Next
82 Debug.Print strItem
83 Next
84 End Sub
85
86 Public Function GetListviewItem(ByVal hWindow As Long, ByVal ProcessID As Long, ByVal pColumn As Long, ByVal pRow As Long) As String
87 Dim Result As Long
88 Dim myItem As LV_ITEMA
89 Dim pHandle As Long
90 Dim pStrBufferMemory As Long
91 Dim pMyItemMemory As Long
92 Dim strBuffer() As Byte
93 Dim Index As Long
94 Dim tmpString As String
95 Dim strLength As Long
96
97 '******************************
98 '为动态数组变量重新分配存储空间
99 '******************************
100 ReDim strBuffer(MAX_LVMSTRING)
101
102 '*****************************************************************************************************
103 '打开一个现有进程的句柄,返回值Long,如执行成功,返回进程句柄;零表示失败。会设置GetLastError
104 'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
105 '参数 类型及说明
106 'dwDesiredAccess Long,指定这个句柄要求的访问方法。指定API32.TXT文件中以PROCESS_???开头的一个或多个常数
107 'bInheritHandle Long,如句柄能够由子进程继承,则为TRUE
108 'dwProcessId Long,要打开那个进程的进程标识符
109 '*****************************************************************************************************
110 pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
111
112 '*****************************************************************************************************
113 'VirtualAllocEx(目标进程的句柄,0,内存区域的大小,分配类型,新分配内存的存取保护类型)返回所分配页面的基址
114 '*****************************************************************************************************
115 pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
116
117 '*************************************************
118 '初始化LV_ITEM 结构
119 'MyItem.iSubItem 列的索引号
120 'myItem.pszText 数据内容(此处是一个分配的内存地址)
121 '*************************************************
122 myItem.mask = LVIF_TEXT
123 myItem.iSubItem = pColumn
124 myItem.pszText = pStrBufferMemory
125 myItem.cchTextMax = MAX_LVMSTRING
126
127 '***********************************************************
128 '把这个结构写入远程进程process's 存储量
129 'WriteProcessMemory(目标进程的句柄,地址,写入的数据,字节数,0)
130 '***********************************************************
131 pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
132 Result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
133
134 '********************************
135 '发送消息,得到项目信息和写入内存
136 '********************************
137 Result = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, ByVal pMyItemMemory)
138 Result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
139 Result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
140
141 '************************
142 '把字节列阵变成串和送回它
143 '************************
144
145 tmpString = StrConv(strBuffer, vbUnicode)
146 If InStr(tmpString, Chr$(0)) > 0 Then
147 tmpString = Left$(tmpString, InStr(tmpString, Chr$(0)) - 1)
148 End If
149
150 tmpString = Trim$(tmpString)
151
152 '****************************
153 '释放分配的内存和关闭进程句柄
154 '****************************
155 Result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
156 Result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
157
158 Result = CloseHandle(pHandle)
159
160 If Len(tmpString) > 0 Then GetListviewItem = tmpString
161
162 End Function
163
2
3 Private Const LVM_FIRST = &H1000
4 Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)
5
6 Private Const LVM_GETITEM = (LVM_FIRST + 5)
7 Private Const LVM_GETSTRINGWIDTH = (LVM_FIRST + 17)
8 Private Const LVM_GETCOLUMN = (LVM_FIRST + 25)
9 Private Const LVM_GETITEMTEXT = (LVM_FIRST + 45)
10 Private Const LVM_GETHEADER = LVM_FIRST + 31
11 Private Const WC_HEADERA = "SysHeader32"
12 Private Const WC_HEADER = WC_HEADERA
13 Private Const HDM_FIRST = &H1200 '// Header messages
14 Private Const HDM_GETITEMCOUNT = (HDM_FIRST + 0)
15 Private Const HDM_ORDERTOINDEX = (HDM_FIRST + 15)
16
17 Private Const PROCESS_QUERY_INFORMATION = 1024
18 Private Const PROCESS_VM_OPERATION = &H8
19 Private Const PROCESS_VM_READ = &H10
20 Private Const PROCESS_VM_WRITE = &H20
21 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
22 Private Const MAX_LVMSTRING As Long = 255 '可根椐读取数据长度设置适当的数值
23 Private Const MEM_COMMIT = &H1000
24 Private Const MEM_RELEASE = &H8000
25 Private Const PAGE_READWRITE = &H4
26 Private Const LVIF_TEXT As Long = &H1
27
28 Private Type LV_ITEMA
29 mask As Long
30 iItem As Long
31 iSubItem As Long
32 state As Long
33 stateMask As Long
34 pszText As Long
35 cchTextMax As Long
36 iImage As Long
37 lParam As Long
38 iIndent As Long
39 End Type
40
41 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
42 Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
43 Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
44 Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
45 Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
46 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
47 Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
48 Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
49 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
50 Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
51
52 Private Sub Command1_Click()
53 Dim lngHwnd As Long
54 Dim lngHwnd1 As Long
55 Dim lngHeaderHwnd As Long
56 Dim lngPId As Long
57 Dim lngRows As Long
58 Dim lngCols As Long
59 Dim lngRow As Long
60 Dim lngCol As Long
61 Dim strItem As String
62
63 lngHwnd1 = FindWindow(vbNullString, "Windows 任务管理器") '获取任务管理器窗口句柄
64 lngHwnd1 = FindWindowEx(lngHwnd1, 0, "#32770", "") '获取选项卡句柄
65 lngHwnd = FindWindowEx(lngHwnd1, 0, "SysListView32", "进程") '获取进程列表框句柄
66 Debug.Print lngHwnd
67
68 lngHeaderHwnd = SendMessage(lngHwnd, LVM_GETHEADER, 0, 0) '获取ListView表头句柄
69 lngRows = SendMessage(lngHwnd, LVM_GETITEMCOUNT, 0, 0) '获取ListView项目数
70
71 If lngHeaderHwnd > 0 Then
72 lngCols = SendMessage(lngHeaderHwnd, HDM_GETITEMCOUNT, 0, 0) '获取ListView表头项目数
73 Else
74 lngCols = 1
75 End If
76 GetWindowThreadProcessId lngHwnd, lngPId '获取与指定窗口关联在一起的一个进程和线程标识符
77 For lngRow = 0 To lngRows - 1
78 strItem = ""
79 For lngCol = 0 To lngCols - 1
80 strItem = strItem & vbTab & GetListviewItem(lngHwnd, lngPId, lngCol, lngRow)
81 Next
82 Debug.Print strItem
83 Next
84 End Sub
85
86 Public Function GetListviewItem(ByVal hWindow As Long, ByVal ProcessID As Long, ByVal pColumn As Long, ByVal pRow As Long) As String
87 Dim Result As Long
88 Dim myItem As LV_ITEMA
89 Dim pHandle As Long
90 Dim pStrBufferMemory As Long
91 Dim pMyItemMemory As Long
92 Dim strBuffer() As Byte
93 Dim Index As Long
94 Dim tmpString As String
95 Dim strLength As Long
96
97 '******************************
98 '为动态数组变量重新分配存储空间
99 '******************************
100 ReDim strBuffer(MAX_LVMSTRING)
101
102 '*****************************************************************************************************
103 '打开一个现有进程的句柄,返回值Long,如执行成功,返回进程句柄;零表示失败。会设置GetLastError
104 'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
105 '参数 类型及说明
106 'dwDesiredAccess Long,指定这个句柄要求的访问方法。指定API32.TXT文件中以PROCESS_???开头的一个或多个常数
107 'bInheritHandle Long,如句柄能够由子进程继承,则为TRUE
108 'dwProcessId Long,要打开那个进程的进程标识符
109 '*****************************************************************************************************
110 pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
111
112 '*****************************************************************************************************
113 'VirtualAllocEx(目标进程的句柄,0,内存区域的大小,分配类型,新分配内存的存取保护类型)返回所分配页面的基址
114 '*****************************************************************************************************
115 pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
116
117 '*************************************************
118 '初始化LV_ITEM 结构
119 'MyItem.iSubItem 列的索引号
120 'myItem.pszText 数据内容(此处是一个分配的内存地址)
121 '*************************************************
122 myItem.mask = LVIF_TEXT
123 myItem.iSubItem = pColumn
124 myItem.pszText = pStrBufferMemory
125 myItem.cchTextMax = MAX_LVMSTRING
126
127 '***********************************************************
128 '把这个结构写入远程进程process's 存储量
129 'WriteProcessMemory(目标进程的句柄,地址,写入的数据,字节数,0)
130 '***********************************************************
131 pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
132 Result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
133
134 '********************************
135 '发送消息,得到项目信息和写入内存
136 '********************************
137 Result = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, ByVal pMyItemMemory)
138 Result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
139 Result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
140
141 '************************
142 '把字节列阵变成串和送回它
143 '************************
144
145 tmpString = StrConv(strBuffer, vbUnicode)
146 If InStr(tmpString, Chr$(0)) > 0 Then
147 tmpString = Left$(tmpString, InStr(tmpString, Chr$(0)) - 1)
148 End If
149
150 tmpString = Trim$(tmpString)
151
152 '****************************
153 '释放分配的内存和关闭进程句柄
154 '****************************
155 Result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
156 Result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
157
158 Result = CloseHandle(pHandle)
159
160 If Len(tmpString) > 0 Then GetListviewItem = tmpString
161
162 End Function
163
说明下,如果是频繁读取数据或数据项比较多时,请把内存分配和释放的相关代码分离出来,以免引起被读取数据的程序出错和占用内存不断加大。如果哪位朋友有修改其它程序ListView数据的代码也请发出来!