printf()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

Re: printf()

07 Dec 2018, 05:37

Okay this is some advance stuff here.

In C/C++ for printf I just do this and it works. Wonder why it won't in AHK.

Code: Select all

if (!AllocConsole())
    MessageBox(NULL, L"The console window was not created", NULL, MB_ICONEXCLAMATION);

FILE* fp;

freopen_s(&fp, "CONOUT$", "w", stdout);

printf("Hello console on\n");
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: printf()

11 Dec 2018, 16:54

Is it possible to redirect StdOut error messages to the console? This didn't work:

Code: Select all

;#Warn
;#Warn UseUnsetLocal, StdOut
#Warn UseUnsetGlobal, StdOut
;#Warn LocalSameAsGlobal, StdOut
;#Warn UseUnsetGlobal, MsgBox

DllCall("kernel32\AllocConsole")
oStdOut := FileOpen("*", "w `n")

Loop
{
	var := UndefinedVar
	Sleep, 1000
	oStdOut.Write("abc`n")
	oStdOut.Read(0) ;flush the write buffer
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: printf()

18 Mar 2019, 17:40

I tested my 'ConsolePrint' script, and noticed that if you close the console window, the AHK process is also closed. Is there a way to prevent this?

A workaround is to disable the Close button:

Code: Select all

		;SC_CLOSE := 0xF060 ;MF_BYCOMMAND := 0x0
		if (hWnd := DllCall("kernel32\GetConsoleWindow", "Ptr"))
		&& (hMenu := DllCall("user32\GetSystemMenu", "Ptr",hWnd, "Int",0, "Ptr"))
			DllCall("user32\DeleteMenu", "Ptr",hMenu, "UInt",0xF060, "UInt",0x0)

This link suggests it may not be possible to prevent this:
c++ - How can I prevent my program from closing when a open console window is closed? - Stack Overflow
https://stackoverflow.com/questions/20232685/how-can-i-prevent-my-program-from-closing-when-a-open-console-window-is-closed
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Archimede, jollyjoe and 321 guests