Skip to content

Instantly share code, notes, and snippets.

@thehappycheese
Last active April 27, 2024 02:48
Show Gist options
  • Save thehappycheese/0e595455cb2edf2081241628ac8cc1f1 to your computer and use it in GitHub Desktop.
Save thehappycheese/0e595455cb2edf2081241628ac8cc1f1 to your computer and use it in GitHub Desktop.
# This works... but cant read the output array in the final step! :(
# Experimental!
UserLib ← &ffi ⊂□"User32.dll"
GdiLib ← &ffi ⊂□"Gdi32.dll"
KernelLib ← &ffi ⊂□"Kernel32.dll"
MessageBoxA ← UserLib {"int" "MessageBoxA" "long" "const char*" "const char*" "unsigned int"}
# MessageBoxA {0 "Yes Please" "About to Take A Screenshot" 0}
GetSystemMetrics ← UserLib {"int" "GetSystemMetrics" "int"}
GetDeviceContext ← UserLib {"long long" "GetDC" "long"}
CreateCompatibleDeviceContext ← GdiLib {"long long" "CreateCompatibleDC" "long long"}
CreateCompatibleBitmap ← GdiLib {"long long" "CreateCompatibleBitmap" "int" "int" "int"}
ScreenSize ← ∵GetSystemMetrics [0 1] # Primary Screen Size
# struct BITMAPINFOHEADER{ [SIZE 320]
# 32 unsigned long DWORD biSize;
# 32 long LONG biWidth;
# 32 long LONG biHeight;
# 16 short WORD biPlanes;
# 16 short WORD biBitCount;
# 32 unsigned long DWORD biCompression;
# 32 unsigned long DWORD biSizeImage;
# 32 long LONG biXPelsPerMeter;
# 32 long LONG biYPelsPerMeter;
# 32 unsigned long DWORD biClrUsed;
# 32 unsigned long DWORD biClrImportant;
# }
# struct BITMAPINFO{ [size 352]
# 320 BITMAPINFOHEADER bmiHeader;
# 32 RGBQUAD bmiColors[1];
# }
BITMAPINFO ← "{unsigned long; long; long; short; short; unsigned long; unsigned long; long; long; unsigned long; unsigned long; long}"
# gdi32.BitBlt(hdcMemory, 0, 0, w, h, hdcScreen, l, t, SRCCOPY)
BitBlt ← GdiLib {"long long" "BitBlt" "long long" "int" "int" "int" "int" "long long" "int" "int" "long"}
SelectObject ← GdiLib {"long long" "SelectObject" "long long" "long long"}
GetDIBits ← GdiLib {"int" "GetDIBits" "long long" "long long" "unsigned int" "unsigned int" "int*" ⊂BITMAPINFO"*" "unsigned int"}
Valid ← ≠0 # not NULL
ValidGDI ← ≠4294967295 # not HGDI_ERROR
s ← GetDeviceContext 0
⍤ "Failed to get Screen Device Context" Valid s
m ← CreateCompatibleDeviceContext s
⍤ "Failed to CreateCompatibleDeviceContext" Valid m
b ← GdiLib {"long long" "CreateCompatibleBitmap" "int" "int" "int"} {m °[⊙⊙]ScreenSize}
⍤ "Failed to Create Bitmap" Valid b
# returns previously selected object handel if succesful
SelectObject {m b}
⍤ "Failed to Select Object" ↧⊃Valid ValidGDI
# returns 1 if successful
BitBlt {
m # [in] mem handel
0 0 # [in] top left of destination rect
°[⊙⊙]ScreenSize # [in] screen size
s # [in] source device context
0 0 # [in] top left of source rectangle
52256} # [in] opperation (SRCCOPY=52256)
⍤ "Failed to Blit" Valid
# returns the number of lines (image height) if successful, as well as the BITMAPINFO struct, and the data array
GetDIBits {
m # [in] mem handel
b # [in] bitmap
0 # [in] start
⊡1 ScreenSize # [in] height
× 0 ⇡ ×3 /×ScreenSize # [out] create an array big enough to read the result
[
# [in & out] BITMAPINFO struct
350
°[⊙⊙]ScreenSize
1 # Planes ; always 1
×8 3 # Channel bitcount
0 # compression BI_RGB
0 # biSizeImage
0 # biXPelsPerMeter
0 # biYPelsPerMeter
0 # biClrUsed
0 # biClrImportant
0 ] # bmiColors
0} # [in] usage DIB_RGB_COLORS
⍤ "Failed to Retrieve Result" ◇=⊢ ⇌ ScreenSize ⊢ .
# result is {1440 {40 2560 1440 1 24 0 11059200 0 0 0 0 0} 0}
# the first two elements are expected, the last element should be a massive array?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment