khchen / winim

Windows API, COM, and CLR Module for Nim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redirecting STDOUT

itaymigdal opened this issue · comments

Hey khchen - love your winim stuff!
I'm struggling with one issue about 2 weeks - wish you can help.
I'm developing Nim implant that executes in-memory various .net assemblies (which I have no control of their source code) which they all obvious use Console.WriteLine.
I'm trying to capture the STDOUT of them by redirecting it to a file, this way: discard stdout.reopen("file.txt", fmWrite).
It works great when I run console applications.
But when I run a GUI application (like with rundll32.exe) I need to AllocConsole() and then use discard stdout.reopen("file.txt", fmWrite), then it redirects only the Nim native echo calls to the file and not captures the assembly Console.WriteLine calls as it goes only to the new console I allocated.
Tried 1000 things and workarounds, nothing works, I wish you could help.
Thanks!

AllocConsole()  # create console for GUI applications, will fail for console apps
discard stdout.reopen("a.o", fmWrite)  # redirect stdout for a file

var assembly = load(assembly_bytes)  # load in-memory assembly
var arr = toCLRVariant(arguments, VT_BSTR)
assembly.EntryPoint.Invoke(nil, toCLRVariant([arr]))  # invoke it

# for GUI apps only:
# Console.WriteLine("blabla") in assembly goes to the console
# echo "blabla" goes to the file