delphi - How to get the highlighted item from the desktop or Windows Explorer with syslistview32? -
how might i, using delphi , syslistview32, highlighted item desktop or windows explorer?
i'm not sure mean item, posted here how focused item text. it's more complicated task because list view messages can't fill buffer between processes, need pass through virtual memory pages allocated in foreign process. principle same kind of manipulation foreign items directly. i'm not familiar process memory allocation; useful edit appreciated.
note: code below has been tested on 32 bit windows xp
function getfocuseditemtext(const lvhandle: hwnd): string; var size: cardinal; process: thandle; processid: dword; memlocal: pointer; memremote: pointer; numbytes: cardinal; itemindex: integer; begin result := ''; itemindex := sendmessage(lvhandle, lvm_getnextitem, -1, lvni_selected); getwindowthreadprocessid(lvhandle, @processid); process := openprocess(process_vm_operation or process_vm_read or process_vm_write, false, processid); if (itemindex <> -1) , (process <> 0) try size := sizeof(tlvitem) + sizeof(char) * max_path + 1; memlocal := virtualalloc(nil, size, mem_reserve or mem_commit, page_readwrite); memremote := virtualallocex(process, nil, size, mem_reserve or mem_commit, page_readwrite); if assigned(memlocal) , assigned(memremote) try zeromemory(memlocal, sizeof(tlvitem)); plvitem(memlocal)^ begin mask := lvif_text; iitem := itemindex; psztext := lptstr(cardinal(memremote) + cardinal(sizeof(tlvitem))); cchtextmax := max_path; end; numbytes := 0; if writeprocessmemory(process, memremote, memlocal, size, numbytes) if sendmessage(lvhandle, lvm_getitem, 0, lparam(memremote)) <> 0 if readprocessmemory(process, memremote, memlocal, size, numbytes) result := string(pchar(cardinal(memlocal) + cardinal(sizeof(tlvitem)))); except on e: exception showmessage('something bad happened :(' + slinebreak + e.message); end; if assigned(memremote) virtualfreeex(process, memremote, 0, mem_release); if assigned(memlocal) virtualfree(memlocal, 0, mem_release); closehandle(process); end; end;
and here's how call; pass handle , you'll item's text
procedure tform1.button1click(sender: tobject); var s: string; begin s := getitemtext(123456); if s <> '' showmessage(s); end;
Comments
Post a Comment