|
-
June 7th, 2010, 07:08 PM
#5
Re: Get text of the item out of listview with LVS_OWNERDRAWFIXED
{msdn}"The iItemData member of the DRAWITEMSTRUCT structure contains the item data for the specified list-view item. "
I have not ever used LVS_OWNERDRAWFIXED, but this code may be of use to at least help identify the state along the text.
'Get the text of the selected Task Manager process item.
Code:
Option Explicit
Const LVIF_TEXT As Long = 1
Const LVIS_FOCUSED As Long = 1
Const LVIS_SELECTED As Long = 2
Const LVM_FIRST As Long = 4096
Const LVM_GETITEMTEXT As Long = LVM_FIRST + 45
Const LVM_GETITEMCOUNT As Long = LVM_FIRST + 4
Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44)
Const LVM_GETSELECTEDCOUNT As Long = (LVM_FIRST + 50)
Const MAX_LVMSTRING As Long = 260
Const MEM_COMMIT As Long = 4096
Const MEM_DECOMMIT As Long = 16384
Const MEM_RELEASE As Long = 32768
Const PAGE_READWRITE As Long = 4
Const PROCESS_VM_OPERATION As Long = 8
Const PROCESS_VM_READ As Long = 16
Const PROCESS_VM_WRITE As Long = 32
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
Private Declare Function apiFindWindowEx 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 apiGetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function apiOpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function apiVirtualAllocEx Lib "kernel32" Alias "VirtualAllocEx" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function apiVirtualFreeEx Lib "kernel32" Alias "VirtualFreeEx" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function apiWriteProcessMemory Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function apiReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function apiReadProcessMemoryStr Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As String, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function apiCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
Private Declare Function apiSendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function apiSendMessageByVal Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Command1_Click()
Me.Caption = GetSelectedListViewItem
End Sub
Public Function GetSelectedListViewItem() As String
Dim hwnd As Long
hwnd = GetProcessListHandle()
If hwnd <> 0 Then
Dim itemCount As Integer
Dim i As Integer
itemCount = apiSendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0)
For i = 0 To itemCount - 1
If GetListItemState(hwnd, i) <> 0 Then ' LVIS_SELECTED or LVIS_FOCUSED or both
GetSelectedListViewItem = GetListViewText(hwnd, i, 0): Exit For
End If
Next
End If
End Function
Private Function GetProcessListHandle() As Long
Dim lhWndParent As Long
Dim lhWndDialog As Long
Dim lhWndProcessList As Long
Dim i As Long
lhWndParent = apiFindWindowEx(0, 0, vbNullString, "Windows Task Manager") 'get handle to the task manager
For i = 1 To 7
lhWndDialog = apiFindWindowEx(lhWndParent, lhWndDialog, vbNullString, vbNullString)
If GetProcessListHandle = 0 Then GetProcessListHandle = apiFindWindowEx(lhWndDialog, 0, "SysListView32", "Processes")
Next
End Function
Public Function GetListItemState(ByVal hwnd As Long, ByVal i As Long) As Long
GetListItemState = apiSendMessageByVal(hwnd, LVM_GETITEMSTATE, i, LVIS_SELECTED)
End Function
Private Function GetListViewText(ByVal hwnd As Long, ByVal iItem As Long, ByVal subItem As Long) As String
Dim pId As Long
Dim pHandle As Long
Dim pStrBufferMemory As Long
Dim pMyItemMemory As Long
Dim strLength As Long
Dim strBuffer As String
Dim LV As LV_ITEM
Call apiGetWindowThreadProcessId(hwnd, pId)
pHandle = apiOpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, pId)
pStrBufferMemory = apiVirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
LV.mask = LVIF_TEXT
LV.iSubItem = subItem
LV.pszText = pStrBufferMemory
LV.cchTextMax = MAX_LVMSTRING
pMyItemMemory = apiVirtualAllocEx(pHandle, 0, LenB(LV), MEM_COMMIT, PAGE_READWRITE)
Call apiWriteProcessMemory(pHandle, pMyItemMemory, LV, Len(LV), 0)
strLength = apiSendMessage(hwnd, LVM_GETITEMTEXT, iItem, ByVal pMyItemMemory)
strBuffer = Space(strLength)
Call apiReadProcessMemoryStr(pHandle, pStrBufferMemory, strBuffer, strLength, 0)
Call apiVirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_DECOMMIT) 'important for complete release!!!
Call apiVirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_DECOMMIT)
Call apiCloseHandle(pHandle)
GetListViewText = strBuffer
End Function
Public Function GetSelectedListItems(ByVal hwnd As Long) As Long
GetSelectedListItems = apiSendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, ByVal 0)
End Function
You must be an administrator to access listviews of other apps, and explorer, and task magager, especially on Vista/7.
You may need to enable the SE_DEBUG_PRIVILEGE too.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|