C# file not found in System32 when running in 32 bit -
i'm trying run quser
, resulting string. when run code below, see following message the resulting string empty:
'quser' not recognized internal or external command, operable program or batch file.
process p = new process(); p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.filename = "cmd.exe"; p.startinfo.arguments = "/c quser /server:someserver"; p.start(); string output = p.standardoutput.readtoend(); p.waitforexit(); console.writeline(output);
so full command gets run
cmd.exe /c quser /ser:someserver
which runs fine when directly, fails c#.
i found similar question no answer here: executing quser windows command in c#; returning result string. question doesn't have quser not recognized
message though.
why command not recognized when running code?
i tried running quser
command directly so, file not found... strange
process p = new process(); p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.filename = @"c:\windows\system32\quser.exe"; p.startinfo.arguments = @"/server:someserver"; p.start(); string output = p.standardoutput.readtoend(); p.waitforexit(); console.writeline(output);
we found running in 64 bit, finds it. when running anycpu or 32bit, seems looking in syswow64, when directly tell in system32
alright, found solution.
basically, found in chat, system32
folder redirecting syswow64
on builds, causing quser
appear not there.
i have applied workaround.
to workaround it:
use following namespace:
using system.runtime.interopservices;
add following @ top of class file.
[dllimport("kernel32.dll", setlasterror = true)] public static extern int wow64disablewow64fsredirection(ref intptr ptr); [dllimport("kernel32.dll", setlasterror = true)] public static extern int wow64enablewow64fsredirection(ref intptr ptr);
before making
quser
call, make following calls:intptr val = intptr.zero; wow64disablewow64fsredirection(ref val);
after making
quser
call, revert changes:wow64enablewow64fsredirection(ref val);
full sample:
using system.runtime.interopservices; ... namespace csharptests { public class program { [dllimport("kernel32.dll", setlasterror = true)] public static extern int wow64disablewow64fsredirection(ref intptr ptr); [dllimport("kernel32.dll", setlasterror = true)] public static extern int wow64enablewow64fsredirection(ref intptr ptr); [dllimport("kernel32.dll", setlasterror = true)] public static extern int wow64revertwow64fsredirection(ref intptr ptr); static void main(string[] args) { intptr val = intptr.zero; wow64disablewow64fsredirection(ref val); process p = new process(); p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.filename = "cmd.exe"; p.startinfo.arguments = "/c quser"; p.start(); string output = p.standardoutput.readtoend(); p.waitforexit(); console.writeline(output); wow64revertwow64fsredirection(ref val); p = new process(); p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.filename = "cmd.exe"; p.startinfo.arguments = "/c quser"; p.start(); output = p.standardoutput.readtoend(); p.waitforexit(); console.writeline(output); } } }
results:
username sessionname id state idle time logon time ebrown console 1 active none 05/18/2015 09:2 1 'quser' not recognized internal or external command, operable program or batch file.
as can see, first quser
call succeeded, told os stop redirecting syswow64
, once re-enabled it, call failed.
i'm sure there's reason protection, don't need it.
additional considerations:
it prudent of implementing pattern first detect if workaround needs applied. such detection done using following boolean:
environment.getfolderpath(environment.specialfolder.systemx86).contains("system32")
in case of false boolean, need check that:
file.exists(@"c:\windows\system32\filenamehere")
in case:
file.exists(@"c:\windows\system32\qdisk.exe")
adapted from:
http://blog.airesoft.co.uk/2010/09/wow-disabling-wow64-fs-redirection-can-cause-problems-who-knew/
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx
Comments
Post a Comment