- Код: Выделить всё
Option Explicit
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 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 GetWindowThreadProcessId Lib "user32" ( _
ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" ( _
ByVal dwDesiredAccessas As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, ByRef lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, ByRef lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, _
ByRef lpBaseAddress As Any, _
ByRef lpBuffer As Any, _
ByVal nSize As Long, _
ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type LVITEM
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
Private Const LVFI_PARAM As Long = &H1
Private Const LVIF_TEXT As Long = &H1
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_GETITEMCOUNT As Long = (LVM_FIRST + 4)
Private Const LVM_FINDITEM As Long = (LVM_FIRST + 13)
Private Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Private Const LVM_SORTITEMSEX As Long = (LVM_FIRST + 81)
Private Const LVM_SETITEMTEXTA As Long = (LVM_FIRST + 46)
Private Const PROCESS_VM_OPERATION As Long = (&H8)
Private Const PROCESS_VM_WRITE As Long = (&H20)
Private Const PROCESS_VM_READ As Long = (&H10)
Private Const MEM_RELEASE As Long = &H8000
Private Const MEM_COMMIT As Long = &H1000
Private Const PAGE_READWRITE As Long = &H4
Private Sub Command1_Click()
Dim tWindow As Long
Dim hListView As Long
Dim hProcess As Long
Dim pID As Long
Dim LVItemAddr As Long
Dim LVTextBufferAddr As Long
Dim p As LVITEM
Dim lResp As Long
Dim a(256) As Byte
tWindow = FindWindow(vbNullString, "Test")
hListView = FindWindowEx(tWindow, 0&, "ListView20WndClass", vbNullString)
GetWindowThreadProcessId hListView, pID
hProcess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, pID)
LVItemAddr = VirtualAllocEx(hProcess, ByVal 0&, ByVal Len(p), MEM_COMMIT, PAGE_READWRITE)
LVTextBufferAddr = VirtualAllocEx(hProcess, ByVal 0&, ByVal 256&, MEM_COMMIT, PAGE_READWRITE)
With p
.mask = LVIF_TEXT
.iItem = 1
.iSubItem = 1
.cchTextMax = 255
.pszText = LVTextBufferAddr
End With
WriteProcessMemory hProcess, LVItemAddr, VarPtr(p), Len(p), 0&
' На следующей строке происходит падение внешнего приложения
SendMessage hListView, LVM_GETITEMTEXT, 1, p
....
lResp = rpiVirtualFree(hProcess, LVTextBufferAddr)
lResp = rpiVirtualFree(hProcess, LVItemAddr)
CloseHandle hProcess
End Sub
Подскажите please, где я ошибаюсь.