Strange problem executing files from c:\windows\system32, even hb_vfExists() returns .f.

341 views
Skip to first unread message

Miroslav Georgiev

unread,
Aug 25, 2018, 5:20:32 AM8/25/18
to Harbour Users
Hi guys. I have strange problem with Harbour on Windows. I'm trying to execute 
QUERY USER
to find current RDP session number of logged user. 

file is in c:\windows\system32\query.exe


D
:\@@>dir c:\Windows\System32\query.exe


 
Volume in drive C is System Reserved Serial number is 9a68:56ae
 
Directory of  C:\Windows\System32\query.exe


12.04.2018  12:20          16 896  query.exe
             
16 896 bytes in 1 file and 0 dirs    20 480 bytes allocated
     
18 140 975 104 bytes free


D
:\@@>c:\Windows\System32\query.exe
Invalid parameter(s)
QUERY
{ PROCESS | SESSION | TERMSERVER | USER }



The problem is that harbour refuses to run query.exe (and other EXE files, qprocess.exe for example) from c:\windows\system32, and tell me that file not exists. However cmd.exe is exception - executeds normally! 
code:

compile with hbmk2 test

PROCEDURE Main()
local cRUN
   
    cRUN
:= "c:\Windows\System32\query.exe"
   
?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ) //.f., .f., .f.
   
if hb_processRun( cRUN ) == -1
       
?"Cannot run " + cRun
    endif
   
?
   
    cRUN
:= "c:\Windows\System32\qprocess.exe"
   
?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ) //.f., .f., .f.
   
if hb_processRun( cRUN ) == -1
       
?"Cannot run " + cRun
    endif
   
?
   
   
    cRUN
:= "c:\Windows\System32\cmd.exe"
   
?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ), "New shell will start. type EXIT <ENTER> to return." //.t., .t., .t.
   
if hb_processRun( cRUN ) == -1
       
?"Cannot run " + cRun
    endif
   
?"TEST END"
    wait
return

Output:

D:\@@>test


c
:\Windows\System32\query.exe exists: .F. .F. .F.
Cannot run c:\Windows\System32\query.exe


c
:\Windows\System32\qprocess.exe exists: .F. .F. .F.
Cannot run c:\Windows\System32\qprocess.exe


c
:\Windows\System32\cmd.exe exists: .T. .T. .T. New shell will start. type EXIT <ENTER> to return.Microsoft Windows [Version 10.0.17134.228]
(c) 2018 Microsoft Corporation. All rights reserved.


D
:\@@>exit


Tested under Win10 1803 64-bit, Win8.1 64-bit, Run as Administrator.

Permissions are same for both files:

Capture.PNG


Capture1.PNG


Anybody has a clue?



Andrzej P. Wozniak

unread,
Aug 25, 2018, 6:13:25 AM8/25/18
to harbou...@googlegroups.com
From: Miroslav Georgiev <miro....@gmail.com>
Sent: Saturday, August 25, 2018 11:20 AM

> The problem is that harbour refuses to run query.exe (and other EXE
> files, qprocess.exe for example) from c:\windows\system32, and tell me
> that file not exists. *However cmd.exe is exception - executeds
> normally!
>
> Tested under Win10 1803 64-bit, Win8.1 64-bit, Run as Administrator.
^^^^^^^ ^^^^^^
It's the clue of the problem.
Your Harbour app is 32-bit, and you are trying to call 64-bit commands.
Windows automatically redirects such calls to C:\Windows\SysWOW64, but
there is NO query.exe and qprocess.exe there.

--
Regards from Poland
Andrzej P. Woźniak



Miroslav Georgiev

unread,
Aug 25, 2018, 4:08:06 PM8/25/18
to Harbour Users
Thank you. Nice explanation.
Do you know a workaround?

Theo Pluym

unread,
Aug 26, 2018, 2:31:36 AM8/26/18
to Harbour Users
Depends of the systeem. On an Windows 2008R2 RDP server "c:\Windows\SysWOW64\query.exe" and "c:\Windows\SysWOW64\qprocess.exe" do exist and the modified version of of your program does work.

PROCEDURE Main()
local cRUN
    
    cRUN := "c:\Windows\SysWOW64\query.exe"
    ?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ) //.f., .f., .f.
    if hb_processRun( cRUN ) == -1
        ?"Cannot run " + cRun
    endif
    ?
    
    cRUN := "c:\Windows\SysWOW64\qprocess.exe"
    ?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ) //.f., .f., .f.
    if hb_processRun( cRUN ) == -1
        ?"Cannot run " + cRun
    endif
    ?
    
    
    cRUN := "c:\Windows\SysWOW64\cmd.exe"
    ?cRUN, "exists:", hb_vfexists( cRUN ), file( cRUN ), hb_FileExists( cRUN ), "New shell will start. type EXIT <ENTER> to return." //.t., .t., .t.
    if hb_processRun( cRUN ) == -1
        ?"Cannot run " + cRun
    endif
    ?"TEST END"
    wait
return

I expect you only like to know the RDP session number because you are using the program on an RDP server ?


Andrzej P. Wozniak

unread,
Aug 26, 2018, 5:54:57 AM8/26/18
to harbou...@googlegroups.com
From: Miroslav Georgiev <miro....@gmail.com>
Sent: Saturday, August 25, 2018 10:08 PM

>> Your Harbour app is 32-bit, and you are trying to call 64-bit commands.
>> Windows automatically redirects such calls to C:\Windows\SysWOW64, but
>> there is NO query.exe and qprocess.exe there.
> Thank you. Nice explanation.
> Do you know a workaround?

Have you tried to copy needed exe files from System32 to your harbour app
directory? There won't be call to System32 and redirection to SysWOW64 then.
Note that calls to "C:\Program files" may also be redirected.

Miroslav Georgiev

unread,
Aug 26, 2018, 12:28:23 PM8/26/18
to Harbour Users
Yeah, on Server 2016 query.exe is on both locations. Only on desktop OS query.exe is only in 64-bit location..

Miroslav Georgiev

unread,
Aug 26, 2018, 12:32:51 PM8/26/18
to Harbour Users
Thanks to all of you. I think it's easiest to call cmd.exe /c query.exe with hb_ProcessRun() , then parse the StdOut. This will work on both desktop (patched with https://github.com/stascorp/rdpwrap) and servers

Calling via cmd.exe will add some milliseconds to execution time but it's not so important after all

Andrzej P. Wozniak

unread,
Aug 26, 2018, 1:29:52 PM8/26/18
to harbou...@googlegroups.com
From: Miroslav Georgiev <miro....@gmail.com>
Sent: Sunday, August 26, 2018 6:32 PM

> Thanks to all of you. I think it's easiest to call cmd.exe /c query.exe
> with hb_ProcessRun() , then parse the StdOut. This will work on both
> desktop (patched with https://github.com/stascorp/rdpwrap) and servers
>
> Calling via cmd.exe will add some milliseconds to execution time but it's
> not so important after all

You can try another workaround using symlink described here:
https://community.microfocus.com/borland/test/silk_test/w/knowledge_base/20127/why-am-i-unable-to-execute-the-command-query-session-from-the-sys-execute-method-on-a-64-bit-os

But if you have to use more and more Windows specific features, you should
think about direct Windows API calls rather than calling external programs.
See the topic on stackoverflow:
https://stackoverflow.com/questions/36382623/use-system-with-some-dos-commands-issue-query-exe-is-not-recognized-as-an-in

Miroslav Georgiev

unread,
Aug 26, 2018, 1:43:38 PM8/26/18
to Harbour Users
Andzrej, that's really useful. Thanks.

Miroslav Georgiev

unread,
May 21, 2019, 5:03:21 AM5/21/19
to Harbour Users
Reply all
Reply to author
Forward
0 new messages