-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdesktop_interact.py
More file actions
176 lines (143 loc) · 6.09 KB
/
desktop_interact.py
File metadata and controls
176 lines (143 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
from winapi import *
# -----------------------------------------------------------------------------
# Helper: get the desktop SysListView32
# -----------------------------------------------------------------------------
def get_desktop_listview():
progman = FindWindowEx(0, 0, "Progman", None)
defview = FindWindowEx(progman, 0, "SHELLDLL_DefView", None)
if not defview:
workerw = FindWindowEx(0, 0, "WorkerW", None)
while workerw and not defview:
defview = FindWindowEx(workerw, 0, "SHELLDLL_DefView", None)
workerw = FindWindowEx(0, workerw, "WorkerW", None)
if not defview:
return None
listview = FindWindowEx(defview, 0, "SysListView32", None)
return listview
# -----------------------------------------------------------------------------
# Helper: open the process that owns the ListView
# -----------------------------------------------------------------------------
def open_listview_process(hwnd):
pid = wintypes.DWORD(0)
thread_id = GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
if thread_id == 0 or pid.value == 0:
raise RuntimeError("Failed to get the ListView PID")
# print(f"[dbg] ListView PID (explorer) = {pid.value}, Python PID = {os.getpid()}")
# Try ALL_ACCESS; if it fails, you can narrow the required flags
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid.value)
if not hProcess:
err = ctypes.get_last_error()
raise OSError(err, f"OpenProcess failed with error {err:#x}")
return hProcess
# -----------------------------------------------------------------------------
# get_icon_name(index)
# -----------------------------------------------------------------------------
def get_icon_name(index):
listview = get_desktop_listview()
if not listview:
print("Desktop ListView not found")
return None
# Number of icons
count = SendMessage(listview, LVM_GETITEMCOUNT, 0, 0)
print("[dbg] Desktop icons reported by ListView:", count)
if index < 0 or index >= count:
print("Index out of range")
return None
# Open the ListView owner process (explorer.exe)
hProcess = open_listview_process(listview)
# Structure sizes
text_max = 260
local_text_buffer = ctypes.create_unicode_buffer(text_max)
lvitem_local = LVITEMW()
lvitem_size = ctypes.sizeof(LVITEMW)
try:
# Allocate remote memory: first for the text buffer, then for the LVITEM
remote_text = remote_alloc(hProcess, text_max * ctypes.sizeof(ctypes.c_wchar))
remote_lvitem = remote_alloc(hProcess, lvitem_size)
# Prepare local LVITEM, but with pszText pointing to remote_text (in the remote process)
lvitem_local.mask = LVIF_TEXT
lvitem_local.iItem = index
lvitem_local.iSubItem = 0
lvitem_local.state = 0
lvitem_local.stateMask = 0
# Cast the remote address to LPWSTR
lvitem_local.pszText = ctypes.cast(remote_text, wintypes.LPWSTR)
lvitem_local.cchTextMax = text_max
lvitem_local.iImage = 0
lvitem_local.lParam = 0
lvitem_local.iIndent = 0
lvitem_local.iGroupId = 0
lvitem_local.cColumns = 0
lvitem_local.puColumns = None
lvitem_local.piColFmt = None
lvitem_local.iGroup = 0
# Write LVITEM into remote memory
written = ctypes.c_size_t(0)
ok = WriteProcessMemory(
hProcess,
remote_lvitem,
ctypes.byref(lvitem_local),
lvitem_size,
ctypes.byref(written),
)
if not ok or written.value != lvitem_size:
err = ctypes.get_last_error()
raise OSError(err, f"WriteProcessMemory(LVITEM) failed: {err:#x}")
# Send the message to the ListView using the remote LVITEM pointer
SendMessage(listview, LVM_GETITEMTEXTW, index, remote_lvitem)
# Read the text from remote memory into the local buffer
read = ctypes.c_size_t(0)
ok = ReadProcessMemory(
hProcess,
remote_text,
local_text_buffer,
text_max * ctypes.sizeof(ctypes.c_wchar),
ctypes.byref(read),
)
if not ok:
err = ctypes.get_last_error()
raise OSError(err, f"ReadProcessMemory(text) failed: {err:#x}")
# Return the buffer value (Python string)
return local_text_buffer.value
finally:
# Free remote memory and close handle
remote_free(hProcess, remote_text)
remote_free(hProcess, remote_lvitem)
CloseHandle(hProcess)
# -----------------------------------------------------------------------------
# move_first_icon(x, y)
# -----------------------------------------------------------------------------
def move_first_icon(x, y):
listview = get_desktop_listview()
if not listview:
print("Desktop icon list not found")
return
count = SendMessage(listview, LVM_GETITEMCOUNT, 0, 0)
print("Total icons:", count)
if count == 0:
print("No icons")
return
lparam = (x & 0xFFFF) | (y << 16)
SendMessage(listview, LVM_SETITEMPOSITION, 0, lparam)
print(f"Icon 0 moved to ({x}, {y})")
def move_icon(index, x, y):
listview = get_desktop_listview()
if not listview:
return
# ListView client coordinates
lparam = (int(x) & 0xFFFF) | ((int(y) & 0xFFFF) << 16)
SendMessage(listview, LVM_SETITEMPOSITION, index, lparam)
def get_item_count() -> int:
listview = get_desktop_listview()
count = SendMessage(listview, LVM_GETITEMCOUNT, 0, 0)
return count
def disable_snap_to_grid() -> bool:
listview = get_desktop_listview()
if not listview:
return False
# Clear only the SNAPTOGRID bit (mask in wParam)
SendMessage(listview, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_SNAPTOGRID, 0)
# Verify
ex_style = int(SendMessage(listview, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0))
return (ex_style & LVS_EX_SNAPTOGRID) == 0