printf()

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

printf()

04 Dec 2018, 05:45

How to use printf() from msvcrt.dll in AHK?
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: printf()

04 Dec 2018, 09:18

Why not using AHK-buildin function Format()?
https://autohotkey.com/docs/commands/Format.htm
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

04 Dec 2018, 11:05

@ jNizM, printf prints to stdout.

Use dllcall, eg,

Code: Select all

dllcall("msvcrt.dll\printf", "astr", "%x", "int", 12648430, "cdecl int")
Cheers.
iPhilip
Posts: 853
Joined: 02 Oct 2013, 12:21

Re: printf()

04 Dec 2018, 17:57

Another choice is to use the FileAppend command with an asterisk (*) for Filename along with the Format command, e.g.

Code: Select all

FileAppend, % Format("{:T}", "hello world"), *
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

05 Dec 2018, 03:33

Good point iPhilip, however, printf has a few other features which Format lacks. It doesn't do Title Case though ;).

Cheers.
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

Re: printf()

05 Dec 2018, 07:49

Are you sure this works?

DllCall("AllocConsole")
stdin := FileOpen(DllCall("GetStdHandle", "int", -10, "ptr"), "h `n")
stdout := FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")

dllcall("msvcrt.dll\printf", "astr", "%x", "int", 12648430, "cdecl int")
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

05 Dec 2018, 11:01

It returns a positive integer, indicating success. I could printf to file starting the script like,

Code: Select all

"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" >"Error Log.txt"
see FileAppend -> FileName.

Also, you do not need to call GetStdHandle, you can call stdout := fileopen("*", "w"). You can follow the example on the fileopen page and use sprintf to format a string and then write it to stdout, if you really need any formatting not available via format.

Cheers.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: printf()

05 Dec 2018, 11:46

Wow, so this is all we need for a proper AHK console.

Code: Select all

q:: ;AHK console
ConsolePrint("1 " A_Now "`n")
ConsolePrintLine("2 " A_Now)
ConsolePrintLine()
return

ConsolePrint(ByRef vText:="")
{
	static vIsReady := 0, oStdOut
	if !vIsReady
	{
		DllCall("kernel32\AllocConsole")
		oStdOut := FileOpen("*", "w `n")
		vIsReady := 1
	}
	oStdOut.Write(vText)
	oStdOut.Read(0) ;flush the write buffer
}

ConsolePrintLine(ByRef vText:="")
{
	ConsolePrint(vText)
	ConsolePrint("`n")
}
Last edited by jeeswg on 05 Dec 2018, 12:04, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

05 Dec 2018, 12:00

vIsReady
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: printf()

05 Dec 2018, 12:04

@Helgef: Fixed. The function is now ready. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

Re: printf()

05 Dec 2018, 16:16

Printing to console is no problem. However printf is also format.

Can someone post a complete example for printf? Thanks.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

05 Dec 2018, 16:29

I'm not sure what you want. Format string then print,

Code: Select all

sprintf(byref buf, fmt, p*){
	local
	p.push("cdecl int")
	c := dllcall("msvcrt.dll\_scprintf", "astr", fmt, p*)
	varsetcapacity(buf, c + 1, 0)
	c := dllcall("msvcrt.dll\sprintf", "ptr", &buf, "astr", fmt, p*)
	buf := strget(&buf, "CP0")
	return c
}
msgbox % sprintf(buf, "0x%x`n%llu", "int", 256, "int64", -1) "`n`n" buf
Cheers
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

Re: printf()

05 Dec 2018, 16:33

Wait but that's sprintf! Not printf :)))
heavyhotuser
Posts: 10
Joined: 05 Dec 2018, 04:06

Re: printf()

06 Dec 2018, 06:52

Hello guys!
This is cool!
I would like to read StdOut after write!

Code: Select all

q:: ;AHK console
ConsolePrint("1 " A_Now "`n")
sleep, 300
ConsolePrint("2 " A_Now "`n")
MsgBox % ConsolePrint(, 1)
ExitApp
return

ConsolePrint(ByRef vText:="", RW:="")
{
	static vIsReady := 0, oStdOut
	if !vIsReady
	{
		DllCall("kernel32\AllocConsole")
		oStdOut := FileOpen("*", "w `n")
		vIsReady := 1
	}
	if !RW 
	{
		oStdOut.Write(vText)
		oStdOut.Read(0) ;flush the write buffer
	} 
	else
	return oStdOut.ReadAll(), RW:=""
}
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

Re: printf()

06 Dec 2018, 06:57

So we can't use printf? Shame.
User avatar
rommmcek
Posts: 1502
Joined: 15 Aug 2014, 15:18

Re: printf()

06 Dec 2018, 09:31

I heard, StdIn/Out works via stream. May be that's why we can't read afterwards.
But we can always read via copy:

Code: Select all

ConsoleRead()
{
	Clipboard := ""
	ControlSend,, ^{a}^{c}, C:\Program Files\AutoHotkey\AutoHotkey.exe ahk_class ConsoleWindowClass
	ClipWait, 1
	return vTextR := Clipboard
}
heavyhotuser
Posts: 10
Joined: 05 Dec 2018, 04:06

Re: printf()

06 Dec 2018, 09:45

Copy works very well! :bravo:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: printf()

07 Dec 2018, 00:14

When I use #Warn in StdOut mode, the warnings don't appear in the console. Any ideas? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

07 Dec 2018, 01:26

quantum wrote:
06 Dec 2018, 06:57
So we can't use printf? Shame.
As I told you, it works if you start the script from the command line as described in the fileappend page.

Also, I translated this example from msdn, see :arrow: github, its for v2. I'll add a instructions later.

Cheers.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: printf()

07 Dec 2018, 02:25

jeeswg wrote:
07 Dec 2018, 00:14
When I use #Warn in StdOut mode, the warnings don't appear in the console. Any ideas? Thanks.
In the example I mentioned above, warnings from the child script are printed to the console (in the parent script).

I added instructions

Cheers.
Edit:
Also, starting a script as (also from fileappend)

Code: Select all

"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" |more
from the command line also prints warnings and printf in the command prompt.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 118 guests