Error on Runwait, %comspec% /c query session

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest

Error on Runwait, %comspec% /c query session

07 Feb 2017, 11:33

I am trying to execute a simple line of code that used to run on 32bit OS.
It is part of a bigger script and unfortunately on 64bit OS it doesn't work anymore.

the code is:

Code: Select all

RDPquery_txt:=A_ScriptDir "\RDPquery.txt"
RunWait, %comspec% /c query session >>%RDPquery_txt%, , min
If I launch the same two line code on 32 bit OS it still works.

If I type query session on command line on 64bit OS it works fine

If I launch the following batch file on 64bit OS it also works

Code: Select all

@echo off
set %RDPquery_txt="RDPquery.txt"
%comspec% /c query session >>%RDPquery_txt%
I tried to specify C:\Windows\system32\cmd.exe instead of %comspec% but nothing changes.

I don't know what to try more...
Please help!

Thanks
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Error on Runwait, %comspec% /c query session

07 Feb 2017, 12:53

maybe try with parameter /k to see what happens
if the ahk script is in the same folder maybe not needed fullpath for RDPquery_txt

Code: Select all

setworkingdir,%a_scriptdir%
;RDPquery_txt:=A_ScriptDir . "\RDPquery.txt"
RunWait, %comspec% /k query session >>RDPquery_txt, ,
;RunWait, %comspec% /c query session >>RDPquery_txt, ,hide   ; - try to run hidden if you have success
return
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Error on Runwait, %comspec% /c query session

08 Feb 2017, 03:52

Are you running the 32-bit AutoHotkey on your 64-bit OS?

See if

Code: Select all

; Runs 64-bit query.exe with 32-bit cmd
; Look at https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx for details on "Sysnative"
RDPquery_txt:=A_ScriptDir "\RDPquery.txt"
RDPquery_exe:=A_WinDir . (A_Is64bitOS && A_PtrSize == 4 ? "\Sysnative\" : "\System32\") . "query.exe"
RunWait, %comspec% /c %RDPquery_exe% session >>%RDPquery_txt%, , min
works for you.

Alternative approaches:
  • Use the 64-bit version of AutoHotkey on a 64-bit OS if possible
  • Keep your RunWait line as it is, and wrap it in matching calls to Wow64DisableWow64FsRedirection and Wow64RevertWow64FsRedirection. This will run 64-bit cmd with 64-bit query.exe. Might save some time if you have lots of similar lines that are failing because of the redirection
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: Error on Runwait, %comspec% /c query session

09 Feb 2017, 05:13

Using the code you proposed it still doesn't work, but it changes a bit the behavior.

In fact, if I run the whole application I can see the output on command prompt.
I get a "Invalid parameter(s)" message:
Image

At least it finds the query command so I believe I should go in this direction. Unfortunately my knowledge is not enough to modify your code

Code: Select all

; Runs 64-bit query.exe with 32-bit cmd
; Look at https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx for details on "Sysnative"
RDPquery_txt:=A_ScriptDir "\RDPquery.txt"
RDPquery_exe:=A_WinDir . (A_Is64bitOS && A_PtrSize == 4 ? "\Sysnative\" : "\System32\") . "query.exe"
RunWait, %comspec% /c %RDPquery_exe% session >>%RDPquery_txt%, , min
I downloaded the last autohotkey and tried 32bit and 64bit, but no difference.

I would prefer to keep my actual compiler as I don't want to check the whole library...
But first priority it to make it running on 32 and 64 bit OS.

What can I try?
Thanks
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Error on Runwait, %comspec% /c query session

09 Feb 2017, 14:14

What OS is that on? With that font, I'm guessing either XP 64-bit or Windows 7/8. If it's the former, then using Sysnative won't work IIRC. You'll have to DllCall Wow64DisableWow64FsRedirection etc. there - there should be many examples littered around the forum.

If query is failing to execute under normal circumstances (and I have no idea if that screenshot is actually the result of an AutoHotkey script or whether you typed something [what, I don't really know, because it's missing if that's the case] yourself into cmd), then AutoHotkey can't help you there. Try garry's suggestions to see why, like he said, query is failing to run when started from AutoHotkey. I just assumed it was because there isn't a query.exe in SysWow64 (at least here) and if you're running 32-bit AutoHotkey on 64-bit Windows, then System32 is going to get redirected there, but the issue could be something else entirely. I don't know. :)
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: Error on Runwait, %comspec% /c query session

10 Feb 2017, 09:54

Sorry for the lack of information. I'll try to recover here.:)

It should run on Windows 7 64bit Enterprise edition SP1.
The screenshot comes from the execution of the whole application where the line is used, that prints the output into command prompt.
Here's the full application
Autologin.ahk
full application
(8.85 KiB) Downloaded 98 times
Actually I just run the attached file on my personal PC with Windows 7 64bit Professonal and it works! Why not on Enterprise edition?
Anyway I would like it to be reliable, it's so strange...
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Error on Runwait, %comspec% /c query session

10 Feb 2017, 11:23

Ah. Thanks. query session just runs qwinsta - see if running that in your script instead works. (And does running qwinsta on its own in the command prompt on your Windows 7 64bit Enterprise edition machine actually work?) You also might want to consider using SetWorkingDir at the beginning at your script - LogFile contains a relative path to the log file and without SetWorkingDir, the directory where the file is written is at the whim of the working directory the parent process of your script set. (Or you could make LogFile's path an absolute one.)

But, anyway, since I don't really know what's up and as I've already written similar code last week, here's one WinAPI way of doing what qwinsta does (it won't run on anything lower than Windows 7 and if qwinsta doesn't work, I doubt this will but it's gotta be more reliable than parsing command-line output. Tested on my Windows 10 64-bit laptop that's not joined to a domain with AutoHotkey 64-bit and 32-bit):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

if ((wtsapi32 := DllCall("LoadLibrary", "Str", "wtsapi32.dll", "Ptr"))) {
	if (DllCall("wtsapi32\WTSEnumerateSessionsEx", "Ptr", WTS_CURRENT_SERVER_HANDLE := 0, "UInt*", 1, "UInt", 0, "Ptr*", pSessionInfo, "UInt*", wtsSessionCount)) {
		WTS_CONNECTSTATE_CLASS := {0: "WTSActive", 1: "WTSConnected", 2: "WTSConnectQuery", 3: "WTSShadow", 4: "WTSDisconnected", 5: "WTSIdle", 6: "WTSListen", 7: "WTSReset", 8: "WTSDown", 9: "WTSInit"}
		cbWTS_SESSION_INFO_1 := A_PtrSize == 8 ? 56 : 32
		Loop % wtsSessionCount {
			currSessOffset := cbWTS_SESSION_INFO_1 * (A_Index - 1)
			ExecEnvId := NumGet(pSessionInfo+0, currSessOffset, "UInt")
			currSessOffset += 4
			State := NumGet(pSessionInfo+0, currSessOffset, "UInt")
			currSessOffset += 4
			SessionId := NumGet(pSessionInfo+0, currSessOffset, "UInt")
			currSessOffset += A_PtrSize
			SessionName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
			currSessOffset += A_PtrSize
			HostName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
			currSessOffset += A_PtrSize
			UserName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
			currSessOffset += A_PtrSize
			DomainName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
			currSessOffset += A_PtrSize
			FarmName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
			
			MsgBox % "Username: " . UserName . "`r`n" . "State: " . WTS_CONNECTSTATE_CLASS[State] . " (raw state: " . State . ")"
			
			;if (UserName = A_UserName && State != 0) {
			; ...
			;}
			;if (State == 0) ...
		}
		DllCall("wtsapi32\WTSFreeMemoryEx", "UInt", WTSTypeSessionInfoLevel1 := 2, "Ptr", pSessionInfo, "UInt", wtsSessionCount)
	}
	DllCall("FreeLibrary", "Ptr", wtsapi32)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Marium0505, mcl and 343 guests