-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexprst.cpp
More file actions
326 lines (265 loc) · 8.11 KB
/
exprst.cpp
File metadata and controls
326 lines (265 loc) · 8.11 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// NLAUNCH
#include <windows.h>
#include <conio.h>
#include <psapi.h>
#pragma comment( lib, "shell32" )
#pragma comment( lib, "user32" )
// TODO IMPOVE PATH GEATHERING MECHANISM
/* IntPtr MyHwnd = FindWindow(null, "Directory");
var t = Type.GetTypeFromProgID("Shell.Application");
dynamic o = Activator.CreateInstance(t);
try
{
var ws = o.Windows();
for (int i = 0; i < ws.Count; i++)
{
var ie = ws.Item(i);
if (ie == null || ie.hwnd != (long)MyHwnd) continue;
var path = System.IO.Path.GetFileName((string)ie.FullName);
if (path.ToLower() == "explorer.exe")
{
var explorepath = ie.document.focuseditem.path;
}
}
}
finally
{
Marshal.FinalReleaseComObject(o);
} */
// I have found var explorepath = new Uri(ie.LocationURL).LocalPath; to also work when there's no selected item.
// https://docs.microsoft.com/en-us/windows/win32/shell/shellwindows?redirectedfrom=MSDN
// https://stackoverflow.com/questions/20960316/get-folder-path-from-explorer-window
//C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\Hostx64\x|||TRUNCED!!
using namespace std;
//#define DEBUG
#ifdef DEBUG
#include <iostream>
void conon()
{
///////////////////////////////////////////
//SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
AllocConsole();
FILE* s = freopen("CONIN$", "r", stdin);
s = freopen("CONOUT$", "w", stdout);
s = freopen("CONOUT$", "w", stderr);
HANDLE chand = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO inf;
inf.dwSize = 100;
inf.bVisible = FALSE;
SetConsoleCursorInfo(chand, &inf);
system("chcp 65001");
/////////////////////////////////////
}
#endif
typedef struct _dw_list_item
{
DWORD dw;
DWORD reserved;
struct _dw_list_item *next;
} DW_LIST_ITEM;
void dwAttachUq(DW_LIST_ITEM **root, DWORD dw)
{
DW_LIST_ITEM *head = *root;
if(head != NULL)
{
while(head->next != NULL)
{
if(head->dw == dw)
{
return;
}
head = head->next;
}
if(head->dw == dw) // Check last element also
{
return;
}
}
DW_LIST_ITEM tmp;
tmp.dw = dw;
tmp.next = NULL;
static HANDLE heap = GetProcessHeap();
LPVOID ih = HeapAlloc(heap, 0, sizeof(DW_LIST_ITEM));
CopyMemory(ih, &tmp, sizeof(DW_LIST_ITEM));
if(head == NULL)
{
*root = (DW_LIST_ITEM *)ih;
}
else
{
head->next = (DW_LIST_ITEM *)ih; // I ACCESSED NULLPTR HERE, BAKA ME!
}
//head == NULL ? *root = (STR_LIST_ITEM *)ih : head->next = (STR_LIST_ITEM *)ih; // LEGAL!
}
void dwDestoryList(DW_LIST_ITEM **root)
{
DW_LIST_ITEM *cur = *root;
while(cur != NULL)
{
DW_LIST_ITEM *tmp = cur;
static HANDLE heap = GetProcessHeap();
HeapFree(heap, 0, cur);
cur = tmp->next;
}
*root = NULL;
}
typedef struct _str_list_item
{
LPWSTR str;
struct _str_list_item *next;
} STR_LIST_ITEM;
void slAttach(STR_LIST_ITEM **root, LPCWSTR str)
{
STR_LIST_ITEM *head = *root;
if(head != NULL)
{
while(head->next != NULL)
{
head = head->next;
}
}
SIZE_T l = wcslen(str) + 1;
static HANDLE heap = GetProcessHeap();
LPVOID sh = HeapAlloc(heap, 0, l * sizeof(wchar_t));
CopyMemory(sh, str, l * sizeof(wchar_t));
STR_LIST_ITEM tmp;
tmp.str = (LPWSTR)sh;
tmp.next = NULL;
LPVOID ih = HeapAlloc(heap, 0, sizeof(STR_LIST_ITEM));
CopyMemory(ih, &tmp, sizeof(STR_LIST_ITEM));
if(head == NULL)
{
*root = (STR_LIST_ITEM *)ih;
}
else
{
head->next = (STR_LIST_ITEM *)ih; // I ACCESSED NULLPTR HERE, BAKA ME!
}
//head == NULL ? *root = (STR_LIST_ITEM *)ih : head->next = (STR_LIST_ITEM *)ih; // LEGAL!
}
void slDestoryList(STR_LIST_ITEM **root)
{
STR_LIST_ITEM *cur = *root;
while(cur != NULL)
{
STR_LIST_ITEM *tmp = cur;
static HANDLE heap = GetProcessHeap();
HeapFree(heap, 0, cur->str);
HeapFree(heap, 0, cur);
cur = tmp->next;
}
*root = NULL;
}
bool pathOfExe(LPCWSTR path, LPCWSTR name)
{
SIZE_T el = wcslen(name); // EXE length
SIZE_T pl = wcslen(path); // Path length
//wcout << path+pl-el-1 << endl;
return !wcscmp(path+pl-el, name);
}
STR_LIST_ITEM *exp_wnds; // EXPLORER WINDOWS
DW_LIST_ITEM *exp_pids; // EXPLORER PID-S
#pragma warning( suppress : 4100 ) // 'lp': unreferenced formal parameter
BOOL __declspec(nothrow) CALLBACK ewGetExpPidsAndWnds(HWND hwnd, LPARAM lp)
{
static wchar_t buff[500];
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
HANDLE hproc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
GetModuleFileNameExW(hproc, NULL, buff, 500);
if(pathOfExe(buff, L"explorer.exe"))
{
dwAttachUq(&exp_pids, pid);
SendMessageW(hwnd, WM_GETTEXT, 500, (LPARAM)buff);
if(buff[0] != 0 && buff[1] == L':' && buff[2] == L'\\')
{
slAttach(&exp_wnds, buff);
}
}
return TRUE;
}
int spawnProc(LPCWSTR app, LPCWSTR cmd)
{
// NEVER QUOTE lpApplicationName IT SCREWS UP PARSING RESULTING IN ACESS_DENIED (ERR5)
LPWSTR cmd_out = NULL;
if(cmd != NULL)
{
static wchar_t cmd_out_buff[500];
SIZE_T appl = wcslen(app);
//SIZE_T cmdl = wcslen(cmd);
cmd_out_buff[0] = L'\"';
wcscpy(cmd_out_buff+1, app);
cmd_out_buff[appl+1] = L'\"';
cmd_out_buff[appl+2] = L' ';
wcscpy(cmd_out_buff+appl+3, cmd);
cmd_out = cmd_out_buff;
}
STARTUPINFOW si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
memset(&pi, 0, sizeof(pi));
//stealFground();
// Create Process ============================================================================
BOOL res = CreateProcessW(
app, // [I|O] Name of the module to be executed, that's it
cmd_out, // [IO|O] Command line to be exectued, searches PATH, adds extention
NULL, // [I|O] Sec. Attrib. for inhereting new process by child processes
NULL, // [I|O] Sec. Attrib. for inhereting new thread by child processes
FALSE, // [I] New proc. inherits each inheritable handle
0, // [I] Process creation flags
NULL, // [I|O] Ptr to environment block of new process (inherit if NULL)
NULL, // [I|O] Full path to current directory for the process
&si, // [I] Ptr to STARTUPINFO struct, if dwFlags = 0, def. values used
&pi); // [O] Ptr to PROCESS_INFORMATION struct with new proc identification info
// ===========================================================================================
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return res == 0;
}
int wmain()
{
#ifdef DEBUG
conon();
#endif
EnumWindows(ewGetExpPidsAndWnds, 0);
//system("pause");
// EXTERMINATE ALL DA EXPLORERS!
DW_LIST_ITEM *pid = exp_pids;
while(pid != NULL)
{
HANDLE exp_proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid->dw);
TerminateProcess(exp_proc, 1);
WaitForSingleObject(exp_proc, INFINITE);
CloseHandle(exp_proc);
pid = pid->next;
}
//system("pause");
// REVIVE EXPLORER!
spawnProc(L"C:\\Windows\\explorer.exe", NULL); // Spawn Shell
//system("pause");
Sleep(2000);
STR_LIST_ITEM *wnd = exp_wnds;
while(wnd != NULL) // Spawn Windows
{
// Launches Explorer as separate thread within main shell explorer.exe process
// Let The Shell do all the hard work =========================================================
ShellExecuteW(
NULL, // [I|O] Handle to parent window that will show error or UI messages
L"explore", // [I|O] Verb string -> open|edit|explore|find|open|print|runas|NULL(def)
wnd->str, // [I] File or object that the verb is beeing executed on
NULL, // [I|O] Cmd arguments to pass to the file, if it is exe file
NULL, // [I|O] Default working directory of the action NULL -> current dir
SW_SHOWNORMAL); // [I] Parameter that sets default nCmdShow status of the window
// ============================================================================================
//std::wcout << wnd->str << std::endl;
//spawnProc(L"C:\\Windows\\explorer.exe", wnd->str);
wnd = wnd->next;
}
slDestoryList(&exp_wnds);
dwDestoryList(&exp_pids);
//system("pause");
return 0;
}