ok I was wondering how one could include printf() intact but run without console
it seems if one uses WinMain() and compile with /subsystem:windows one can achieve this
here is a sample code
#include <stdio.h>
#include <windows.h>
int pringlob = 0;
int a = 9;
int b = 10;
int c = 33;
int WINAPI WinMain(_In_ HINSTANCE,_In_opt_ HINSTANCE,_In_ LPSTR, _In_ int)
{
if ((pringlob & 0x400000) != 0)
{
printf("this is mytest\n");
if ((pringlob & 0x400000) != 0)
{
printf("this is mytest 1 %d\n", a);
if ((pringlob & 0x400000) != 0)
{
printf("this is mytest 2 %d\n", b);
if ((pringlob & 0x400000) != 0)
{
printf("this is mytest 3 %d\n", c);
}
}
}
}
MessageBoxA(NULL,"NO CONSOLE TEST","NO CONSOLE USE ATTACHCONSOLE",MB_OK);
}
compiling this with in vs 2017 community
cl /Zi /W4 /analyze /Od /nologo /EHsc foo.cpp /link /release /subsystem:windows user32.lib
this runs without console but keeps printf intact which prints to the console that is Created By AttachConsole() on the pid of binary
open foo.exe in x64dbg run to
WinMainCrtStartup or @$exentry aka PEHeader->AddressOFEntryPoint
so that all dll init are done
open file->attach and note the pid of foo.exe
in the stack use push qword
and push pid
push return address which is WinmainCRTStartup
use Ctrl+g to go to kernelbase.AllocConsole
step over the function and return to WinMainCRTStartup
pop the pid using pop qword in stack
set the global to 0xffffffff
f9 to run the exe
you will get all the printfs in the newly created console
see the attached screenshot

edit :
what igorsk suggested in his answer is a better option adding a test of the same
:\>ls -lg
total 2
-rw-r--r-- 1 197121 91 Apr 3 02:01 complink.bat
-rw-r--r-- 1 197121 595 Apr 2 23:45 fook.cpp
:\>complink.bat
:\>cl /Zi /W4 /Od /EHsc /nologo /analyze fook.cpp /link /release /subsystem:windows user32.lib
fook.cpp
:\>dumpbin /headers fook.exe | grep -i subsystem
6.00 subsystem version
2 subsystem (Windows GUI)
:\>editbin /subsystem:console fook.exe
Microsoft (R) COFF/PE Editor Version 14.16.27045.0
Copyright (C) Microsoft Corporation. All rights reserved.
:\>dumpbin /headers fook.exe | grep -i subsystem
6.00 subsystem version
3 subsystem (Windows CUI)
:\>cdb -c "ed fook!pringlob 0xffffffff;g;q" fook.exe | awk "/Reading/,/quit/"
0:000> cdb: Reading initial command 'ed fook!pringlob 0xffffffff;g;q'
xxxxxxxxxx
this is mytest
this is mytest 1 9
this is mytest 2 10
this is mytest 3 33
quit: