Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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
Private Const PROCESS_VM_OPERATION = (&H8)
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_VM_WRITE = (&H20)
Private Const MEM_COMMIT = &H1000
Private Const PAGE_READWRITE = &H4
Private Const LVIF_TEXT = &H1
Private Const LVM_FIRST = &H1000
Private Const LVM_SETITEMTEXT = (LVM_FIRST + 46)
Private Type LVITEM
  mask As Long
  iItem As Long
  iSubItem As Long
  state As Long
  stateMask As Long
  pszText As String
  cchTextMax As Long
  iImage As Long
  lParam As Long
  iIndent As Long
End Type
Private Sub SetLVText(mhwnd As Long, ByVal sText As String, Optional ByVal lItemIndex As Long = 0, Optional lSubItemIndex As Long = 0)
  
    Dim i As Long, s As String
    Dim dwProcessId As Long, hProcess As Long
    Dim lpListItemRemote As Long, lpTextRemote As Long
    Dim dwBytesRead As Long, dwBytesWrite As Long
    Dim lvItemLocal As LVITEM
    Dim bWriteOK As Long, nMaxLen As Long
    Call GetWindowThreadProcessId(mhwnd, dwProcessId)    nMaxLen = 1023
    dwBytesWrite = 0
     
        hProcess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0&, dwProcessId)
    If hProcess <> 0 Then
        lpTextRemote = VirtualAllocEx(ByVal hProcess, ByVal 0&, nMaxLen + 1, MEM_COMMIT, PAGE_READWRITE)
        lpListItemRemote = VirtualAllocEx(ByVal hProcess, ByVal 0&, Len(lvItemLocal), MEM_COMMIT, PAGE_READWRITE)
        bWriteOK = WriteProcessMemory(ByVal hProcess, lpTextRemote, Str(nMaxLen) & Chr(0), ByVal nMaxLen + 1, dwBytesWrite)
        lvItemLocal.iItem = lItemIndex
        lvItemLocal.iSubItem = lSubItemIndex
        lvItemLocal.mask = LVIF_TEXT
        lvItemLocal.cchTextMax = nMaxLen
        lvItemLocal.pszText = lpTextRemote
         bWriteOK = WriteProcessMemory(ByVal hProcess, lpListItemRemote, VarPtr(lvItemLocal), ByVal Len(lvItemLocal), dwBytesWrite)
        sText = Left$(sText, nMaxLen)
        bWriteOK = WriteProcessMemory(ByVal hProcess, lpTextRemote, sText, ByVal Len(sText), dwBytesWrite)
        i = SendMessage(mhwnd, LVM_SETITEMTEXT, lItemIndex, ByVal lpListItemRemote)
         Call VirtualFreeEx(hProcess, ByVal lpListItemRemote, 0, MEM_DECOMMIT)
        Call VirtualFreeEx(hProcess, ByVal lpTextRemote, 0, MEM_DECOMMIT)
    End If
    CloseHandle hProcess
End Sub
Private Sub Form_Load()
Dim hwnd As Long
hwnd = FindWindow("#32770", "Windows 任务管理器")
hwnd = FindWindowEx(hwnd, 0, "#32770", vbNullString)
hwnd = FindWindowEx(hwnd, 0, "SysListView32", vbNullString)
SetLVText hwnd, "a", 1, 0
End Sub

解决方案 »

  1.   


    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Private Const PROCESS_VM_OPERATION = (&H8)
    Private Const PROCESS_VM_READ = &H10
    Private Const PROCESS_VM_WRITE = (&H20)
    Private Const MEM_COMMIT = &H1000
    Private Const PAGE_READWRITE = &H4
    Private Const LVIF_TEXT = &H1
    Private Const LVM_FIRST = &H1000
    Private Const LVM_SETITEMTEXT = (LVM_FIRST + 46)
    Private Type LV_ITEM
        mask As Long
        iItem As Long
        iSubItem As Long
        state As Long
        stateMask As Long
        pszText As Long
        cchTextMax As Long
        iImage As Long
        lParam As Long
        iIndent As Long
    End Type
    Sub SetLVText(ByVal mhwnd As Long, ByVal sText As String, Optional ByVal lItemIndex As Long = 0, Optional lSubItemIndex As Long = 0)
        Dim i As Long, s As String
        Dim dwProcessId As Long, hProcess As Long
        Dim dwBytesRead As Long, dwBytesWrite As Long
        Dim bSuccess As Long
        Call GetWindowThreadProcessId(mhwnd, dwProcessId)
        
        Dim lpListItemRemote As Long, lpTextRemote As Long
        Dim nMaxLen As Long
        nMaxLen = Len(sText) + 1
        
        Dim lvItemLocal As LV_ITEM
        Dim bWriteOK As Long
        
        hProcess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0&, dwProcessId)
        If hProcess <> 0 Then
            lpTextRemote = VirtualAllocEx(ByVal hProcess, ByVal 0&, nMaxLen, MEM_COMMIT, PAGE_READWRITE)
            lpListItemRemote = VirtualAllocEx(ByVal hProcess, ByVal 0&, Len(lvItemLocal), MEM_COMMIT, PAGE_READWRITE)
            bWriteOK = WriteProcessMemory(ByVal hProcess, ByVal lpTextRemote, ByVal CStr(String(nMaxLen, Chr(0))), nMaxLen, dwBytesWrite)
            lvItemLocal.iItem = lItemIndex
            lvItemLocal.iSubItem = lSubItemIndex
            lvItemLocal.mask = LVIF_TEXT
            lvItemLocal.cchTextMax = nMaxLen
            lvItemLocal.pszText = lpTextRemote
            dwBytesWrite = 0
            bWriteOK = WriteProcessMemory(ByVal hProcess, ByVal lpListItemRemote, ByVal VarPtr(lvItemLocal), Len(lvItemLocal), dwBytesWrite)
            
            bWriteOK = WriteProcessMemory(ByVal hProcess, ByVal lpTextRemote, ByVal sText, Len(sText), dwBytesWrite)
            
            i = SendMessage(mhwnd, LVM_SETITEMTEXT, lItemIndex, ByVal lpListItemRemote)
            
            Call VirtualFreeEx(hProcess, ByVal lpListItemRemote, 0, MEM_DECOMMIT)
            Call VirtualFreeEx(hProcess, ByVal lpTextRemote, 0, MEM_DECOMMIT)
        End If
        CloseHandle hProcess
    End SubPrivate Sub Form_Load()
    Dim hwnd As Long
    hwnd = FindWindow("#32770", "Windows 任务管理器")
    hwnd = FindWindowEx(hwnd, 0, "#32770", vbNullString)
    hwnd = FindWindowEx(hwnd, 0, "SysListView32", vbNullString)
    SetLVText hwnd, "dda", 1, 0
    End Sub
      

  2.   


     
    谢啦~原来 pszText As String 主要这个定义错了。