Option
Explicit
'常數(shù)申明
Public
Const
LVM_FIRST
As
Long
= &H1000
Public
Const
LVM_GETHEADER
As
Long
= LVM_FIRST + 31
Public
Const
LVM_GETITEMCOUNT
As
Long
= LVM_FIRST + 4
Public
Const
LVM_GETITEMTEXT
As
Long
= LVM_FIRST + 45
Public
Const
HDM_FIRST
As
Long
= &H1200
Public
Const
HDM_GETITEMCOUNT
As
Long
= (HDM_FIRST + 0)
Public
Const
PROCESS_VM_OPERATION
As
Long
= &H8
Public
Const
PROCESS_VM_READ
As
Long
= &H10
Public
Const
PROCESS_VM_WRITE
As
Long
= &H20
Public
Const
MAX_LVMSTRING
As
Long
= 255
Public
Const
MEM_COMMIT
As
Long
= &H1000
Public
Const
MEM_RELEASE
As
Long
= &H8000&
Public
Const
PAGE_READWRITE
As
Long
= &H4
Public
Const
LVIF_TEXT
As
Long
= &H1
'類型申明
Public
Type LV_ITEMA
mask
As
Long
iItem
As
Long
iSubItem
As
Long
State
As
Long
stateMask
As
Long
pszText
As
Long
cchTextMax
As
Long
End
Type
'API申明
Public
Declare
Function
OpenProcess
Lib
"kernel32"
(
ByVal
dwDesiredAccess
As
Long
,
ByVal
bInheritHandle
As
Long
,
ByVal
dwProcId
As
Long
)
As
Long
'打開進程
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
'獲取內存空間
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
WriteProcessMemory
Lib
"kernel32"
(
ByVal
hProcess
As
Long
,
ByVal
lpBaseAddress
As
Long
,
ByRef
lpBuffer
As
LV_ITEMA,
ByVal
nSize
As
Long
, _
lpNumberOfBytesWritten
As
Long
)
As
Long
'向內存寫數(shù)據(jù)
Public
Declare
Function
ReadProcessMemory
Lib
"kernel32"
(
ByVal
hProcess
As
Long
,
ByVal
lpBaseAddress
As
Long
,
ByRef
lpBuffer
As
Any,
ByVal
nSize
As
Long
, _
lpNumberOfBytesWritten
As
Long
)
As
Long
'向內存讀數(shù)據(jù)
Public
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
'發(fā)送消息
Public
Declare
Function
CloseHandle
Lib
"kernel32"
(
ByVal
hObject
As
Long
)
As
Long
'關閉進程
Public
Declare
Function
GetWindowThreadProcessId
Lib
"user32"
(
ByVal
hwnd
As
Long
, lpdwProcessId
As
Long
)
As
Long
'獲得進程ID
Public
Function
GetListViewTextArray(
ByVal
hWindow
As
Long
)
As
String
()
Dim
myItem()
As
LV_ITEMA
Dim
PHandle
As
Long
Dim
ProcessId
As
Long
Dim
PStrBufferMemory
As
Long
Dim
PMyItemMemory
As
Long
Dim
StrBuffer(MAX_LVMSTRING)
As
Byte
Dim
TmpString
As
String
Dim
Ih
As
Long
, J
As
Long
, HCount
As
Long
Dim
StrArr()
As
String
, ItemString
As
String
Dim
Ji
As
Long
, MyItemLength()
As
Long
GetWindowThreadProcessId hWindow, ProcessId
HCount = SendMessage(hWindow, LVM_GETHEADER, 0, 0)
'獲取列數(shù)
If
HCount > 0
Then
HCount = SendMessage(HCount, HDM_GETITEMCOUNT, 0, 0) - 1
Else
'NOT HCOUNT...
HCount = 0
End
If
PHandle = OpenProcess(PROCESS_VM_OPERATION
Or
PROCESS_VM_READ
Or
PROCESS_VM_WRITE, 0, ProcessId)
ReDim
myItem(HCount)
ReDim
MyItemLength(HCount)
PStrBufferMemory = VirtualAllocEx(PHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
PMyItemMemory = VirtualAllocEx(PHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
Ji = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0) - 1
On
Error
GoTo
err1
ReDim
StrArr(Ji)
For
Ih = 0
To
HCount
myItem(Ih).mask = LVIF_TEXT
myItem(Ih).iSubItem = Ih
myItem(Ih).pszText = PStrBufferMemory
myItem(Ih).cchTextMax = MAX_LVMSTRING
MyItemLength(Ih) = Len(myItem(Ih))
Next
Ih
For
J = 0
To
Ji
ItemString =
""
For
Ih = 0
To
HCount
WriteProcessMemory PHandle, PMyItemMemory, myItem(Ih), MyItemLength(Ih), 0
If
SendMessage(hWindow, LVM_GETITEMTEXT, J,
ByVal
PMyItemMemory) > 0
Then
ReadProcessMemory PHandle, PStrBufferMemory, StrBuffer(0), MAX_LVMSTRING, 0
TmpString = StrConv(StrBuffer, vbUnicode)
TmpString = Left(TmpString, InStr(TmpString, vbNullChar) - 1)
ItemString = ItemString & TmpString & Chr(9)
' Chr$(32)
End
If
Next
Ih
If
ItemString <>
""
Then
StrArr(J) = Left(ItemString, Len(ItemString) - 1)
End
If
Next
J
VirtualFreeEx PHandle, PMyItemMemory, 0, MEM_RELEASE
VirtualFreeEx PHandle, PStrBufferMemory, 0, MEM_RELEASE
CloseHandle (PHandle)
ItemString =
""
GetListViewTextArray = StrArr
Exit
Function
err1:
MsgBox
"不是Listview類吧?"
, vbInformation
End
Function