-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathscreenshot_withhiddenwindows.lua
More file actions
491 lines (425 loc) · 16.8 KB
/
screenshot_withhiddenwindows.lua
File metadata and controls
491 lines (425 loc) · 16.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
-- screenshot_withhiddenwindows.lua
-- Desktop screenshot + individual window capture (attempts to capture all visible windows)
-- Note: Non-foreground/hidden windows may appear black due to GDI limitations
-- Disable JIT for thread safety
jit.off()
local ffi = require("ffi")
local bit = require("bit")
-- Windows API definitions
ffi.cdef[[
typedef void* HWND;
typedef void* HDC;
typedef void* HGDIOBJ;
typedef void* HBITMAP;
typedef unsigned long DWORD;
typedef int BOOL;
typedef long LONG;
typedef unsigned short WORD;
typedef unsigned int UINT;
typedef BOOL (*WNDENUMPROC)(HWND, long);
typedef struct {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct {
BITMAPINFOHEADER bmiHeader;
DWORD bmiColors[3];
} BITMAPINFO;
HWND GetDesktopWindow();
HWND GetForegroundWindow();
HDC GetDC(HWND hWnd);
int ReleaseDC(HWND hWnd, HDC hDC);
HDC CreateCompatibleDC(HDC hdc);
int DeleteDC(HDC hdc);
HBITMAP CreateCompatibleBitmap(HDC hdc, int cx, int cy);
HGDIOBJ SelectObject(HDC hdc, HGDIOBJ h);
BOOL DeleteObject(HGDIOBJ ho);
BOOL BitBlt(HDC hdc, int x, int y, int cx, int cy, HDC hdcSrc, int x1, int y1, DWORD rop);
int GetDIBits(HDC hdc, HBITMAP hbm, UINT start, UINT cLines, void *lpvBits, BITMAPINFO *lpbmi, UINT usage);
int GetDeviceCaps(HDC hdc, int index);
BOOL EnumWindows(WNDENUMPROC lpEnumFunc, long lParam);
int GetWindowTextA(HWND hWnd, char* lpString, int nMaxCount);
BOOL IsWindowVisible(HWND hWnd);
int GetWindowTextLengthA(HWND hWnd);
DWORD GetWindowThreadProcessId(HWND hWnd, DWORD* lpdwProcessId);
BOOL GetWindowRect(HWND hWnd, void* lpRect);
BOOL PrintWindow(HWND hWnd, HDC hdcBlt, UINT nFlags);
BOOL GetClientRect(HWND hWnd, void* lpRect);
HDC GetWindowDC(HWND hWnd);
BOOL ClientToScreen(HWND hWnd, void* lpPoint);
]]
local gdi32 = ffi.load("gdi32")
local user32 = ffi.load("user32")
-- Compatibility for older Lua versions
local unpack = table.unpack or unpack
-- Constants
local SRCCOPY = 0x00CC0020
local HORZRES = 8
local VERTRES = 10
local DIB_RGB_COLORS = 0
local PW_CLIENTONLY = 0x00000001
-- Helper functions
local function getTempPath()
return os.getenv("TEMP") or os.getenv("TMP") or "C:\\Windows\\Temp"
end
local function getComputerName()
return os.getenv("COMPUTERNAME") or "UNKNOWN"
end
local function getFormattedTimestamp()
return os.date("%Y%m%d_%H%M%S")
end
local function getScreenshotFilePath()
return getTempPath() .. "\\" .. getComputerName() .. "_SCREENSHOT_" .. getFormattedTimestamp() .. ".bmp"
end
local function getLogFilePath()
return getTempPath() .. "\\" .. getComputerName() .. "_SCREENSHOT_" .. getFormattedTimestamp() .. ".log"
end
-- Log message to both file and stdout
local logFile = nil
local function logMsg(message)
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
local logMessage = timestamp .. " - " .. message
-- Write to stdout
print(logMessage)
-- Write to log file
if logFile then
logFile:write(logMessage .. "\n")
logFile:flush() -- Ensure immediate write
end
end
-- Enumerate all visible windows
local windowList = {}
local function enumWindowsCallback(hwnd, lParam)
if user32.IsWindowVisible(hwnd) ~= 0 then
local length = user32.GetWindowTextLengthA(hwnd)
if length > 0 then
local buffer = ffi.new("char[?]", length + 1)
user32.GetWindowTextA(hwnd, buffer, length + 1)
local title = ffi.string(buffer)
if title and title ~= "" then
local pidBuffer = ffi.new("DWORD[1]")
user32.GetWindowThreadProcessId(hwnd, pidBuffer)
local pid = pidBuffer[0]
table.insert(windowList, {
hwnd = tostring(hwnd),
title = title,
pid = pid
})
end
end
end
return 1 -- Continue enumeration
end
local function enumerateWindows()
windowList = {}
local callback = ffi.cast("WNDENUMPROC", enumWindowsCallback)
user32.EnumWindows(callback, 0)
callback:free()
return windowList
end
-- Function to sanitize filename (remove invalid characters)
local function sanitizeFilename(filename)
-- Remove or replace invalid filename characters
local sanitized = filename:gsub('[<>:"/\\|?*]', '_')
sanitized = sanitized:gsub('%s+', '_') -- Replace spaces with underscores
-- Limit length to avoid path issues
if #sanitized > 100 then
sanitized = sanitized:sub(1, 100)
end
return sanitized
end
-- Function to capture a single window to BMP file
local function captureWindow(hwnd, pid, title, timestamp)
-- Get window dimensions (full window including frame)
local rect = ffi.new("struct { long left; long top; long right; long bottom; }")
if user32.GetWindowRect(hwnd, rect) == 0 then
logMsg(" WARNING: Failed to get window rect for: " .. title)
return false
end
local width = rect.right - rect.left
local height = rect.bottom - rect.top
-- Skip windows that are too small or too large (likely hidden, minimized, or invalid)
if width <= 10 or height <= 10 or width > 10000 or height > 10000 then
logMsg(" SKIP: Invalid dimensions " .. width .. "x" .. height .. " for: " .. title)
return false
end
-- Get the window DC (includes the entire window, not just client area)
local windowDC = user32.GetWindowDC(hwnd)
if windowDC == nil then
logMsg(" WARNING: Failed to get window DC for: " .. title)
return false
end
-- Create compatible DC and bitmap
local memDC = gdi32.CreateCompatibleDC(windowDC)
if memDC == nil then
logMsg(" WARNING: Failed to create compatible DC for: " .. title)
user32.ReleaseDC(hwnd, windowDC)
return false
end
local hBitmap = gdi32.CreateCompatibleBitmap(windowDC, width, height)
if hBitmap == nil then
logMsg(" WARNING: Failed to create compatible bitmap for: " .. title)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(hwnd, windowDC)
return false
end
gdi32.SelectObject(memDC, hBitmap)
-- Use BitBlt to copy the entire window (including frame and decorations)
-- Source coordinates are 0,0 because GetWindowDC gives us DC for the whole window
local result = gdi32.BitBlt(memDC, 0, 0, width, height, windowDC, 0, 0, SRCCOPY)
if result == 0 then
logMsg(" WARNING: BitBlt failed for window: " .. title)
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(hwnd, windowDC)
return false
end
-- Setup BITMAPINFO
local bmi = ffi.new("BITMAPINFO")
bmi.bmiHeader.biSize = ffi.sizeof("BITMAPINFOHEADER")
bmi.bmiHeader.biWidth = width
bmi.bmiHeader.biHeight = height
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 32
bmi.bmiHeader.biCompression = 0
bmi.bmiHeader.biSizeImage = 0
-- Calculate image size
local bytesPerPixel = 4
local rowSize = width * bytesPerPixel
if rowSize % 4 ~= 0 then
rowSize = rowSize + (4 - (rowSize % 4))
end
local imageSize = rowSize * height
local bitmapData = ffi.new("uint8_t[?]", imageSize)
-- Get bitmap bits
result = gdi32.GetDIBits(memDC, hBitmap, 0, height, bitmapData, bmi, DIB_RGB_COLORS)
if result == 0 then
logMsg(" WARNING: GetDIBits failed for: " .. title)
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(hwnd, windowDC)
return false
end
-- Create filename: COMPUTERNAME_SCREENSHOT_PID_PROCESSNAME_TIMESTAMP.bmp
local sanitizedTitle = sanitizeFilename(title)
local filename = getTempPath() .. "\\" .. getComputerName() .. "_SCREENSHOT_" ..
pid .. "_" .. sanitizedTitle .. "_" .. timestamp .. ".bmp"
-- Manually construct BMP file header
local fileHeaderSize = 14
local infoHeaderSize = ffi.sizeof("BITMAPINFOHEADER")
local fileSize = fileHeaderSize + infoHeaderSize + imageSize
local dataOffset = fileHeaderSize + infoHeaderSize
local fileHeaderBytes = {
0x42, 0x4D,
bit.band(fileSize, 0xFF),
bit.band(bit.rshift(fileSize, 8), 0xFF),
bit.band(bit.rshift(fileSize, 16), 0xFF),
bit.band(bit.rshift(fileSize, 24), 0xFF),
0x00, 0x00,
0x00, 0x00,
bit.band(dataOffset, 0xFF),
bit.band(bit.rshift(dataOffset, 8), 0xFF),
bit.band(bit.rshift(dataOffset, 16), 0xFF),
bit.band(bit.rshift(dataOffset, 24), 0xFF)
}
local fileHeader = string.char(unpack(fileHeaderBytes))
-- Write BMP file
local file = io.open(filename, "wb")
if not file then
logMsg(" WARNING: Failed to create file: " .. filename)
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(hwnd, windowDC)
return false
end
file:write(fileHeader)
file:write(ffi.string(bmi.bmiHeader, ffi.sizeof("BITMAPINFOHEADER")))
file:write(ffi.string(bitmapData, imageSize))
file:close()
-- Cleanup
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(hwnd, windowDC)
logMsg(" CAPTURED: " .. filename .. " (" .. width .. "x" .. height .. ")")
return true
end
-- Main screenshot capture function
local function captureScreenshot()
local outputFile = getScreenshotFilePath()
local logFilePath = getLogFilePath()
-- Open log file
logFile = io.open(logFilePath, "w")
if not logFile then
print("ERROR: Failed to open log file: " .. logFilePath)
return
end
logMsg("Starting screenshot capture")
logMsg("Output file: " .. outputFile)
logMsg("Log file: " .. logFilePath)
-- Enumerate and log visible windows
logMsg("=== Enumerating Visible Windows ===")
local windows = enumerateWindows()
logMsg("Found " .. #windows .. " visible windows")
-- Get foreground window
local fgWnd = user32.GetForegroundWindow()
local fgTitle = ""
if fgWnd ~= nil then
local length = user32.GetWindowTextLengthA(fgWnd)
if length > 0 then
local buffer = ffi.new("char[?]", length + 1)
user32.GetWindowTextA(fgWnd, buffer, length + 1)
fgTitle = ffi.string(buffer)
end
end
logMsg("Foreground Window: " .. (fgTitle ~= "" and fgTitle or "(None)"))
logMsg("")
-- Log all windows
for i, win in ipairs(windows) do
local isForeground = (win.title == fgTitle) and " [FOREGROUND]" or ""
logMsg(string.format(" [%d] PID:%d HWND:%s%s", i, win.pid, win.hwnd, isForeground))
logMsg(" Title: " .. win.title)
end
logMsg("=== End Window Enumeration ===")
logMsg("")
-- Capture individual window screenshots
logMsg("=== Capturing Individual Window Screenshots ===")
local capturedCount = 0
local timestamp = getFormattedTimestamp()
for i, win in ipairs(windows) do
logMsg("Capturing window [" .. i .. "]: " .. win.title)
-- Convert HWND string back to pointer
local hwndPtr = ffi.cast("HWND", tonumber(win.hwnd:match("0x%x+")))
if captureWindow(hwndPtr, win.pid, win.title, timestamp) then
capturedCount = capturedCount + 1
end
end
logMsg("=== Window Screenshots Complete: " .. capturedCount .. "/" .. #windows .. " captured ===")
logMsg("")
-- Get desktop window and DC
local desktopWnd = user32.GetDesktopWindow()
local desktopDC = user32.GetDC(desktopWnd)
if desktopDC == nil then
logMsg("ERROR: Failed to get desktop DC")
return
end
-- Get screen dimensions
local screenWidth = gdi32.GetDeviceCaps(desktopDC, HORZRES)
local screenHeight = gdi32.GetDeviceCaps(desktopDC, VERTRES)
logMsg("Screen dimensions: " .. screenWidth .. "x" .. screenHeight)
-- Create compatible DC and bitmap
local memDC = gdi32.CreateCompatibleDC(desktopDC)
local hBitmap = gdi32.CreateCompatibleBitmap(desktopDC, screenWidth, screenHeight)
gdi32.SelectObject(memDC, hBitmap)
-- Copy screen to bitmap
if gdi32.BitBlt(memDC, 0, 0, screenWidth, screenHeight, desktopDC, 0, 0, SRCCOPY) == 0 then
logMsg("ERROR: BitBlt failed")
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(desktopWnd, desktopDC)
return
end
logMsg("Screen captured to bitmap")
-- Setup BITMAPINFO structure for GetDIBits
-- First call to get the actual bitmap info
local bmi = ffi.new("BITMAPINFO")
bmi.bmiHeader.biSize = ffi.sizeof("BITMAPINFOHEADER")
bmi.bmiHeader.biWidth = screenWidth
bmi.bmiHeader.biHeight = screenHeight -- POSITIVE for bottom-up DIB (standard BMP format)
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 32
bmi.bmiHeader.biCompression = 0 -- BI_RGB
bmi.bmiHeader.biSizeImage = 0
-- Calculate image size: For 32-bit BMP, each row is width * 4 bytes
-- Rows must be aligned to 4-byte boundary (DWORD aligned)
local bytesPerPixel = 4 -- 32 bits = 4 bytes
local rowSize = screenWidth * bytesPerPixel
-- Align to 4-byte boundary
if rowSize % 4 ~= 0 then
rowSize = rowSize + (4 - (rowSize % 4))
end
local imageSize = rowSize * screenHeight
logMsg("Calculated row size: " .. rowSize .. ", image size: " .. imageSize)
local bitmapData = ffi.new("uint8_t[?]", imageSize)
-- Get bitmap bits
local result = gdi32.GetDIBits(memDC, hBitmap, 0, screenHeight, bitmapData, bmi, DIB_RGB_COLORS)
if result == 0 then
logMsg("ERROR: GetDIBits failed")
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(desktopWnd, desktopDC)
return
end
logMsg("Bitmap data retrieved: " .. imageSize .. " bytes")
-- Manually construct BMP file header (14 bytes, packed with #pragma pack(2))
-- BITMAPFILEHEADER must be packed to 2-byte alignment
local fileHeaderSize = 14
local infoHeaderSize = ffi.sizeof("BITMAPINFOHEADER")
local fileSize = fileHeaderSize + infoHeaderSize + imageSize
local dataOffset = fileHeaderSize + infoHeaderSize
-- Manually pack BITMAPFILEHEADER (14 bytes total)
local fileHeaderBytes = {
0x42, 0x4D, -- bfType ('BM') - 2 bytes
bit.band(fileSize, 0xFF), -- bfSize - 4 bytes (little endian)
bit.band(bit.rshift(fileSize, 8), 0xFF),
bit.band(bit.rshift(fileSize, 16), 0xFF),
bit.band(bit.rshift(fileSize, 24), 0xFF),
0x00, 0x00, -- bfReserved1 - 2 bytes
0x00, 0x00, -- bfReserved2 - 2 bytes
bit.band(dataOffset, 0xFF), -- bfOffBits - 4 bytes (little endian)
bit.band(bit.rshift(dataOffset, 8), 0xFF),
bit.band(bit.rshift(dataOffset, 16), 0xFF),
bit.band(bit.rshift(dataOffset, 24), 0xFF)
}
local fileHeader = string.char(unpack(fileHeaderBytes))
-- Write BMP file
local file = io.open(outputFile, "wb")
if not file then
logMsg("ERROR: Failed to open file for writing: " .. outputFile)
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(desktopWnd, desktopDC)
return
end
-- Write headers and data
file:write(fileHeader)
file:write(ffi.string(bmi.bmiHeader, ffi.sizeof("BITMAPINFOHEADER")))
file:write(ffi.string(bitmapData, imageSize))
file:close()
logMsg("BMP Header: Type=BM, Size=" .. fileSize .. ", OffBits=" .. dataOffset)
logMsg("BMP Info: Width=" .. bmi.bmiHeader.biWidth ..
", Height=" .. bmi.bmiHeader.biHeight ..
", BitCount=" .. bmi.bmiHeader.biBitCount ..
", SizeImage=" .. bmi.bmiHeader.biSizeImage)
logMsg("Desktop screenshot saved to " .. outputFile)
-- Cleanup
gdi32.DeleteObject(hBitmap)
gdi32.DeleteDC(memDC)
user32.ReleaseDC(desktopWnd, desktopDC)
logMsg("Resources cleaned up successfully")
logMsg("")
logMsg("=== CAPTURE SESSION COMPLETE ===")
logMsg("Total windows captured: " .. capturedCount)
logMsg("Desktop screenshot: " .. outputFile)
-- Close log file
if logFile then
logFile:close()
end
end
-- Run the screenshot capture
captureScreenshot()