printf()
Re: printf()
Why not using AHK-buildin function Format()?
https://autohotkey.com/docs/commands/Format.htm
https://autohotkey.com/docs/commands/Format.htm
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: printf()
@ jNizM, printf prints to stdout.
Use dllcall, eg,
Cheers.
Use dllcall, eg,
Code: Select all
dllcall("msvcrt.dll\printf", "astr", "%x", "int", 12648430, "cdecl int")
Re: printf()
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)
Re: printf()
Good point iPhilip, however, printf has a few other features which Format lacks. It doesn't do Title Case though .
Cheers.
Cheers.
Re: printf()
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")
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")
Re: printf()
It returns a positive integer, indicating success. I could printf to file starting the script like,
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.
Code: Select all
"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" >"Error Log.txt"
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.
Re: printf()
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: printf()
vIsReady
Re: printf()
@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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: printf()
Printing to console is no problem. However printf is also format.
Can someone post a complete example for printf? Thanks.
Can someone post a complete example for printf? Thanks.
Re: printf()
I'm not sure what you want. Format string then print,
Cheers
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
Re: printf()
Wait but that's sprintf! Not printf ))
-
- Posts: 10
- Joined: 05 Dec 2018, 04:06
Re: printf()
Hello guys!
This is cool!
I would like to read StdOut after write!
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:=""
}
Re: printf()
So we can't use printf? Shame.
Re: printf()
I heard, StdIn/Out works via stream. May be that's why we can't read afterwards.
But we can always read via copy:
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
}
-
- Posts: 10
- Joined: 05 Dec 2018, 04:06
Re: printf()
Copy works very well!
Re: printf()
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: printf()
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 github, its for v2. I'll add a instructions later.
Cheers.
Re: printf()
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
Who is online
Users browsing this forum: No registered users and 90 guests