vb.net WIN32API 獲取listview的值

Public Class Form1
    Public Const LVM_FIRST As Short = &H1000S
    Public Const LVM_GETITEMCOUNT As Integer = (LVM_FIRST + 4)
    Public Const LVM_GETITEM As Integer = (LVM_FIRST + 5)
    Public Const LVM_GETSTRINGWIDTH As Integer = (LVM_FIRST + 17)
    Public Const LVM_GETCOLUMN As Integer = (LVM_FIRST + 25)
    Public Const LVM_GETITEMTEXT As Integer = (LVM_FIRST + 45)
    Public Const LVM_GETHEADER As Integer = LVM_FIRST + 31
    Public Const WC_HEADERA As String = "SysHeader32"
    Public Const WC_HEADER As String = WC_HEADERA
    Public Const HDM_FIRST As Short = &H1200S '// Header messages
    Public Const HDM_GETITEMCOUNT As Integer = (HDM_FIRST + 0)
    Public Const HDM_ORDERTOINDEX As Integer = (HDM_FIRST + 15)
    Public Const PROCESS_QUERY_INFORMATION As Short = 1024
    Public Const PROCESS_VM_OPERATION As Short = &H8S
    Public Const PROCESS_VM_READ As Short = &H10S
    Public Const PROCESS_VM_WRITE As Short = &H20S
    Public Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
    Public Const MAX_LVMSTRING As Integer = 255 '可根椐讀取數據長度設置適當的數值
    Public Const MEM_COMMIT As Short = &H1000S
    Public Const MEM_RELEASE As Short = &H8000S
    Public Const PAGE_READWRITE As Short = &H4S
    Public Const LVIF_TEXT As Integer = &H1S
    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

    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcId As Integer) As Integer
    Private 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
    Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer


    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer


    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer




    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    Private Declare Function GetCurrentProcessId Lib "kernel32" () As Integer
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    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
    Public Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer

   


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lngHwnd As Integer
        Dim lngHwnd0 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

        lngHwnd = FindWindow("#32770", "Windows 任務管理器")
        lngHwnd0 = FindWindowEx(lngHwnd, 0, "#32770", "Applications") '獲取任務管理器窗口句柄

        lngHwnd1 = FindWindowEx(lngHwnd0, 0, "SysListView32", "任務") '獲取進程列表框句柄

        'lngHwnd0 = FindWindowEx(lngHwnd, 0, "WTWindow", "巔峯批量卡iPhone新版 V1.8")
        'lngHwnd1 = FindWindowEx(lngHwnd0, 0, "SysListView32", "")
        '第二個參數是 LVM_GETHEADER,得到LISTVIEW的HEADER句柄
        lngHeaderHwnd = SendMessage(lngHwnd1, LVM_GETHEADER, 0, 0)

        lngRows = SendMessage(lngHwnd1, LVM_GETITEMCOUNT, 0, 0) '獲取ListView項目數
        Debug.Print("總行數是:" & lngRows)

        If lngHeaderHwnd > 0 Then
            lngCols = SendMessage(lngHeaderHwnd, HDM_GETITEMCOUNT, 0, 0) '獲取ListView表頭項目數
            Debug.Print("總列數是:" & lngCols)

        Else
            lngCols = 1
        End If

        GetWindowThreadProcessId(lngHwnd, lngPId) '獲取與指定窗口關聯在一塊兒的一個進程和線程標識符ID

        Dim count As Integer



        Dim strBuffer() As Byte, pHandle As Integer, myItem() As LV_ITEMA, i As Integer, j As Integer
        Dim pStrBufferMemory As Integer, pMyItemMemory As Integer, result As Integer, tmpString As String
        Dim strLength As Integer



        ReDim strBuffer(MAX_LVMSTRING)

        pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, lngPId)

        ReDim myItem(lngCols)



        For i = 0 To lngRows - 1
            For j = 0 To lngCols
                pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
                myItem(j).mask = LVIF_TEXT
                myItem(j).iSubItem = j
                myItem(j).pszText = pStrBufferMemory
                myItem(j).cchTextMax = MAX_LVMSTRING
                pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem(j)), MEM_COMMIT, PAGE_READWRITE)
                result = WriteProcessMemory(pHandle, pMyItemMemory, myItem(j), Len(myItem(j)), 0&)

                strLength = SendMessage(lngHwnd1, LVM_GETITEMTEXT, i, pMyItemMemory)
                If result = 0 Then
                    result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
                    result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
                    Exit For
                End If
                result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
                result = ReadProcessMemory(pHandle, pMyItemMemory, myItem(j), Len(myItem(j)), 0)
                tmpString = StrConv(System.Text.Encoding.Default.GetString(strBuffer), 0)
              
                result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
                result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
 
            Next


            
        Next

        result = CloseHandle(pHandle)
  
    End Sub


 






    
End Class
相關文章
相關標籤/搜索