Why this AutoHotkey example no works in Unicode?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Why this AutoHotkey example no works in Unicode?

Post by Archimede » 19 Jan 2023, 04:37

I found this working example on Run help:

Code: Select all

RunWaitOne(command) {
    MsgBox % RunWaitOne("dir " A_ScriptDir)

    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99¬
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
If I modify it to:

Code: Select all

RunWaitOne(command) {
    MsgBox % RunWaitOne("dir " A_ScriptDir)

    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99¬
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /u /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
adding /u in the line
exec := shell.Exec(ComSpec " /u /C " command)
it no works ( the returned result is "" ): why?

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 22 Jan 2023, 12:39

Hallo.
Nobody can tell me why that function, that is an AutoHotkey example, can no work on Unicode?
The standard version, no Unicode, works, bur no generate the right particular character reproduction like à, È and so on...
Otherwise, how I can execute a command by command line and obtain the text output?
Thank you very much.

Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why this AutoHotkey example no works in Unicode?

Post by Rohwedder » 23 Jan 2023, 02:23

Hallo,
your "working example" does not work!
Correction:

Code: Select all

MsgBox % RunWaitOne("dir " A_ScriptDir)

RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 23 Jan 2023, 04:00

Yes, you have reason, it was a my inattention.
What YOU have post is working: it is the SAME like the AutoHotkey example on "Run".
But the problem is the same: I need to work with the Unicode version: is it possible?
I need the follow modified function works: is it possible?

Code: Select all

MsgBox % RunWaitOne("dir " A_ScriptDir)

RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /U /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
Is it possible to use ExpandEnvironmentStringsA or ExpandEnvironmentStrings? If yes, how? (I no know API functions).
I found some soluction redirecting the output on file and reading the file, but that function for me is too much slow.

Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why this AutoHotkey example no works in Unicode?

Post by Rohwedder » 23 Jan 2023, 12:27

Perhaps?:

Code: Select all

ClipBoard =
Run,% ComSpec " /U /C dir " A_ScriptDir "|clip",, Hide
ClipWait
MsgBox,% ClipBoard
Return

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 23 Jan 2023, 17:14

It is interesting, I tested a similar function and I found it works, but it changes the clipboard and for me is no acceptable because it must works in background while the computer can be normally used.
:-(

Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why this AutoHotkey example no works in Unicode?

Post by Rohwedder » 24 Jan 2023, 04:32

By the way, when I use the dir command in burque505's
Windows CMD shell in Gui control
my file names are displayed with correct German Umlauts ÄäÜüÖö.

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 24 Jan 2023, 05:42

Interesting...
I think, but I'm no sure, the Unicode problem is on ReadAll()...

Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why this AutoHotkey example no works in Unicode?

Post by Rohwedder » 24 Jan 2023, 08:56

This even provides correct German Umlauts ÄäÜüÖö without the need of the font Terminal:

Code: Select all

MsgBox % RunWaitOne("dir " A_ScriptDir)

RunWaitOne(Command)
{ ; just me: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=62522
   Static OEMCP := "CP" . DllCall("GetOEMCP", "UInt")
   shell := ComObjCreate("WScript.Shell")
   launch := "cmd.exe /c " Command
   exec := shell.Exec(ComSpec " /C " Command)
   result := exec.StdOut.ReadAll()
   If (A_IsUnicode) {
      VarSetCapacity(oemstr, StrPut(result, "CP0"), 0)
      StrPut(result, &oemstr, "CP0")
   }
   Return StrGet(A_IsUnicode ? &oemstr : &result, OEMCP)
}

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 24 Jan 2023, 11:00

Nice!
It works!
I suggest to modify the line:
MsgBox % RunWaitOne("dir " A_ScriptDir)
to:
MsgBox % RunWaitOne("dir " """" A_ScriptDir """")
If the dir path contain SPACES the first line no works, the modified line works.
The line
launch := "cmd.exe /c " Command
seem unuseful.
Thank you very much.

Now I have only another problem: I need to completely hide (no flashes) the cmd window while the line
exec := shell.Exec(ComSpec " /C " Command)
works: is it possible?

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 24 Jan 2023, 12:30

Hallo.
I tried to modify your function to hide the window while function: it seem work, but I no understand all very well (yes I modified it no understanding it... :-(): what you think about it?

Code: Select all

MsgBox % RunWaitOne("dir " """" A_ScriptDir """")

RunWaitOne(Command)
{ ; just me: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=62522
   Static OEMCP := "CP" . DllCall("GetOEMCP", "UInt")
   shell := ComObjCreate("WScript.Shell")

launch := "cmd.exe /c " . command . " > c:\temp.txt"
exec := shell.Run(launch, 0, true)
FileRead, result, c:\temp.txt
FileDelete, C:\temp.txt

;   launch := "cmd.exe /c " Command
;   exec := shell.Exec(ComSpec " /C " Command)
   ;result := shell.Exec(ComSpec " /D /Q /C " Command).StdOut.ReadAll()
;   result := exec.StdOut.ReadAll()
   If (A_IsUnicode) {
      VarSetCapacity(oemstr, StrPut(result, "CP0"), 0)
      StrPut(result, &oemstr, "CP0")
   }
   Return StrGet(A_IsUnicode ? &oemstr : &result, OEMCP)
}
It seem work...
Besides the redirection mode to file it is quickly than the standard read with
exec.StdOut.ReadAll()
I tested it and about it I am sure...

garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Why this AutoHotkey example no works in Unicode?

Post by garry » 25 Jan 2023, 10:38

@Rohwedder thank you
I use win-11
if I open DOS window , type DIR , I dont see chinese characters in filename
example OK: writes to textfile F1 UTF-16 LE ,DOS-commands like DIR or VER
for other commands like > PING or SYSTEMINFO ( this show me only chinese characters ) , for these use |CLIP

Code: Select all

;- test  : DOS command write to file F1 , then must read with *P1200 , because file F1 is UTF-16 LE
;-         only for dos commands like dir/ver > show chinese characters in filename
;-         commands like ping or systeminfo > result show only chinese characters
;-         so use clip for commands like ping or systeminfo ....
;-
#Warn
#Singleinstance,force
setworkingdir,%a_scriptdir%
Fileencoding,UTF-8
F1:=a_scriptdir . "\DOSoutput11.txt"
;-
;-- DOS-COMMANDS --------- :
;- OK , show chinese characters in file F1 :
cmd:="dir"
cmd:="dir&&ver"
;- Åäöü 夜来香 เย่ไหลเซียง_Ye Lai Xiang$Bkkjay Th$zS6jOzNty1M.txt  ;-- <<<  get result , DIR
;-
;- here use clip , otherwise see only chinese characters when use PING or SYSTEMINFO
;cmd:="dir&&ver&&systeminfo"
;cmd:="systeminfo&&ver"
;cmd:="dir&&ping autohotkey.com -n 2"
;-------------------------
cmdx:=cmd
Critical, ON
tooltip,DOS Running...
if cmd contains dir
;if (cmd="dir")
  {
  RunWait, %Comspec% /U /C "(%cmdx%) 2>&1 >%f1%",, Hide   ;- file is UTF-16 LE
  ;RunWait, %Comspec% /U /C "(%cmdx%) >%f1%",, Hide
  }
else
  RunWait, %Comspec% /C "(%cmdx%) 2>&1 |clip",, Hide  ;- OK for all commands , not see chinese characters in filename 
if cmd contains dir
;if (cmd="dir")
 FileRead,text1, *P1200 %f1%
else
 text1:=clipboard
tooltip
msgbox,%text1%
return
;====================================================
2nd example with DOScommandHere for command DIR ( see also chinese characters )

Code: Select all

#Warn
#Singleinstance,force
setworkingdir,%a_scriptdir%
Fileencoding,UTF-8
F1:=a_scriptdir . "\DOSoutput11.txt"
;-- DOS-COMMANDS ----------------------- :
;- OK show chinese characters in file F1 :
cmd:="dir"
cmd:="ver&&dir"
;- Åäöü 夜来香 เย่ไหลเซียง_Ye Lai Xiang$Bkkjay Th$zS6jOzNty1M.txt
;-
;- here use clip otherwise see only chinese characters when use systeminfo or ping
;cmd:="ver&&dir&&systeminfo"
;cmd:="systeminfo&&ver"
;cmd:="ver&&dir&&ping autohotkey.com -n 2"
;--------------------------------------
return
;-----------
esc::exitapp
;----------- start with F9 ---------------------------
$F9::
cmdx:=cmd
Critical, ON
tooltip,DOS Running...
;if cmd contains dir
if (cmd="dir")
  {
  gosub,a2
  RunWait, %Comspec% /U /C dir %aa% 2>&1 >%f1%,, Hide   ;- file is UTF-16 LE
  }
else
  RunWait, %Comspec% /C "(%cmdx%) 2>&1 |clip",, Hide
;if cmd contains dir
if (cmd="dir")
 FileRead,text1, *P1200 %f1%
else
 text1:=clipboard
tooltip
msgbox,%text1%
return
;------------- DOScommandHERE tmplinshi --------------
A2:
;- tmplinshi / DOScommandhere WIN11 https://www.autohotkey.com/boards/viewtopic.php?p=33858#p33858
	dirx:=""
    If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {
        WinHWND := WinActive()
        For win in ComObjCreate("Shell.Application").Windows
            If (win.HWND = WinHWND) {
                dirx := SubStr(win.LocationURL, 9) ; remove "file:///"
                dirx := RegExReplace(dirx, "%20", " ")
                Break
            }
    }
    ;Run, cmd, % dir ? dir : A_Desktop
aa:=dirx ? dirx : A_Desktop
stringreplace,aa,aa,/,\,all
return
;====================================================

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Why this AutoHotkey example no works in Unicode?

Post by Archimede » 27 Jan 2023, 12:15

About this function:

Code: Select all

MsgBox % RunWaitOne("dir " A_ScriptDir)

RunWaitOne(Command)
{ ; just me: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=62522
   Static OEMCP := "CP" . DllCall("GetOEMCP", "UInt")
   shell := ComObjCreate("WScript.Shell")
   launch := "cmd.exe /c " Command
   exec := shell.Exec(ComSpec " /C " Command)
   result := exec.StdOut.ReadAll()
   If (A_IsUnicode) {
      VarSetCapacity(oemstr, StrPut(result, "CP0"), 0)
      StrPut(result, &oemstr, "CP0")
   }
   Return StrGet(A_IsUnicode ? &oemstr : &result, OEMCP)
}
I tried to modify it to:

Code: Select all

MsgBox % RunWaitOne("dir " A_ScriptDir, sStandardError, iErrorLevel)

RunWaitOne( sCommand, ByRef sStandardError := "", ByRef iErrorLevel := 0 )
{

    Static sStdOutFileName := A_Temp . "\StdOut.txt"
	Static sStdErrorFileName := A_Temp . "\StdErr.txt"

    Static sOEMCP := DllCall( "GetOEMCP", "UInt" )

	RunWait, "%Comspec%" /D /Q /C (%sCommand%)>"%sStdOutFileName%"2>"%sStdErrorFileName%",, Hide UseErrorLevel
    iErrorLevel := ErrorLevel
    FileRead, sStandardOutput, *P%sOEMCP% %sStdOutFileName%
    FileDelete, %sStdOutFileName%
    FileRead, sStandardError, *P%sOEMCP% %sStdErrorFileName%
    FileDelete, %sStdErrorFileName%

    Return sStandardOutput
}
I'm sorry if the example is no perfect, because I adapted my solution to explain it here without test it.
The answer is:
the use of
*P%sOEMCP%
with
FileRead...
can replace the
If (A_IsUnicode) {...}
procedure?
I tested it and it seem work...

Post Reply

Return to “Ask for Help (v1)”