16,719
社区成员
发帖
与我相关
我的任务
分享tmpString = StrConv(System.Text.UnicodeEncoding.Unicode.GetString(strBuffer), vbUnicode)'vb6的写法
'程序目的,调用 s=GetSysListView32()可以得到任务管理器中的内容
Option Strict Off
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
'Option Explicit On
Module Module1
'Option Explicit
Public Const LVM_FIRST As Integer = &H1000
Public Const LVM_GETITEMCOUNT As Decimal = (LVM_FIRST + 4)
Public Const LVM_GETITEM As Decimal = (LVM_FIRST + 5)
Public Const LVM_GETSTRINGWIDTH As Decimal = (LVM_FIRST + 17)
Public Const LVM_GETCOLUMN As Decimal = (LVM_FIRST + 25)
Public Const LVM_GETITEMTEXT As Decimal = (LVM_FIRST + 45)
Public Const LVM_GETHEADER As Decimal = LVM_FIRST + 31
Public Const WC_HEADERA As String = "SysHeader32"
Public Const WC_HEADER As String = WC_HEADERA
Public Const HDM_FIRST As Integer = &H1200 '// Header messages
Public Const HDM_GETITEMCOUNT As Decimal = (HDM_FIRST + 0)
Public Const HDM_ORDERTOINDEX As Decimal = (HDM_FIRST + 15)
Public Const PROCESS_QUERY_INFORMATION As Short = 1024
Public Const PROCESS_VM_OPERATION As Integer = &H8
Public Const PROCESS_VM_READ As Integer = &H10
Public Const PROCESS_VM_WRITE As Integer = &H20
Public Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Public Const MAX_LVMSTRING As Integer = 255 '可根椐读取数据长度设置适当的数值
Public Const MEM_COMMIT As Integer = &H1000
Public Const MEM_RELEASE As Integer = &H8000
Public Const PAGE_READWRITE As Integer = &H4
Public Const LVIF_TEXT As Integer = &H1
Public Structure LV_ITEMA
Dim mask As Integer
Dim iItem As Integer
Dim iSubItem As Integer
Dim state As Integer
Dim stateMask As Integer
Dim pszText As Integer
Dim cchTextMax As Integer
Dim iImage As Integer
Dim lParam As Integer
Dim iIndent As Integer
End Structure
'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcId As Integer) As Integer
' Public 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'vb6
Public Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As Integer 'vb.net 要用integer
'Public Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Public Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal lpBaseAddress As Object, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer '要用byVal而不是vb6下的byRef,,然后long要调用integer
Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal lpBaseAddress As Object, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByVal lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer '要用byVal而不是vb6下的byRef,,然后long要调用integer
' Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByVal lpBuffer() As Byte, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
' Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.AsAny)> ByRef lParam As Object) As Long'vb6
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer 'vb.net
Private Const WM_GETTEXT As Integer = &HD
Private Const WM_GETTEXTLENGTH As Integer = &HE
'Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
'Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, ByVal lpdwProcessId As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByVal lpdwProcessId As Integer) As Integer
' Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long’vb6
Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer 'vb.net
' Public 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'vb6
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer 'vb.net
Public Function GetSysListView32() As String
Dim lngHwnd As Integer
Dim lngHwnd1 As Integer
Dim lngHeaderHwnd As Integer
Dim lngPId As Integer
Dim lngRows As Integer
Dim lngCols As Integer
Dim lngRow As Integer
Dim lngCol As Integer
'Dim strItem As String
lngHwnd1 = FindWindow(vbNullString, "Windows 任务管理器") '获取任务管理器窗口句柄
lngHwnd1 = FindWindowEx(lngHwnd1, 0, "#32770", vbNullString)
lngHwnd = FindWindowEx(lngHwnd1, 0, "SysListView32", vbNullString) '获取进程列表框句柄
lngHeaderHwnd = SendMessage(lngHwnd, LVM_GETHEADER, 0, 0) '获取ListView表头句柄
lngRows = SendMessage(lngHwnd, LVM_GETITEMCOUNT, 0, 0) '获取ListView项目数
'MsgBox lngRows
If lngHeaderHwnd > 0 Then
lngCols = SendMessage(lngHeaderHwnd, HDM_GETITEMCOUNT, 0, 0) '获取ListView表头项目数
Else
lngCols = 1
End If
GetWindowThreadProcessId(lngHwnd, lngPId) '获取与指定窗口关联在一起的一个进程和线程标识符
Dim s1, s, s11 As Object
s = ""
For lngRow = 0 To lngRows - 1
s1 = ""
For lngCol = 0 To lngCols - 1
s11 = GetListviewItem(lngHwnd, lngPId, lngCol, lngRow)
s1 = s1 & "[td]" & s11
Next
s = s & "[tr]" & s1
Next
GetSysListView32 = s
MsgBox("得到的内容是:" & s)
'GetSysListView32 = GetListviewItem(lngHwnd, lngPId, 4, 2) '取得具体需要的内容,上面的注释掉的为全部内容
End Function
Public Function GetListviewItem(ByVal hWindow As Integer, ByVal ProcessID As Integer, ByVal pColumn As Integer, ByVal pRow As Integer) As String
Dim Result As Integer
Dim myItem As LV_ITEMA
Dim pHandle As Integer
Dim pStrBufferMemory As Integer
Dim pMyItemMemory As Integer
Dim strBuffer() As Byte
Dim tmpString As String
'为动态数组变量重新分配存储空间
ReDim strBuffer(MAX_LVMSTRING)
'打开一个现有进程的句柄,返回值Long,如执行成功,返回进程句柄;零表示失败。会设置GetLastError
'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
'参数 类型及说明
'dwDesiredAccess Long,指定这个句柄要求的访问方法。指定API32.TXT文件中以PROCESS_???开头的一个或多个常数
'bInheritHandle Long,如句柄能够由子进程继承,则为TRUE
'dwProcessId Long,要打开那个进程的进程标识符
pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
'VirtualAllocEx(目标进程的句柄,0,内存区域的大小,分配类型,新分配内存的存取保护类型)返回所分配页面的基址
pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
'初始化LV_ITEM 结构
'MyItem.iSubItem 列的索引号
'myItem.pszText 数据内容(此处是一个分配的内存地址)
myItem.mask = LVIF_TEXT
myItem.iSubItem = pColumn
myItem.pszText = pStrBufferMemory
myItem.cchTextMax = MAX_LVMSTRING
'把这个结构写入远程进程process's 存储量
'WriteProcessMemory(目标进程的句柄,地址,写入的数据,字节数,0)
pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
Result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
'发送消息,得到项目信息和写入内存
Result = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, pMyItemMemory)
Result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
Result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
'把字节列阵变成串和送回它
tmpString = System.Text.Encoding.Default.GetString(strBuffer)
'tmpString = BitConverter.ToString(strBuffer)
If InStr(tmpString, Chr(0)) > 0 Then
tmpString = Left(tmpString, InStr(tmpString, Chr(0)) - 1)
End If
tmpString = Trim(tmpString)
'释放分配的内存和关闭进程句柄
Result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
Result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
Result = CloseHandle(pHandle)
If Len(tmpString) > 0 Then
GetListviewItem = tmpString
End If
End Function
End Module
以上是代码是从vb6修改来的,运行结果取不到值