问题
I created an application that sends keyboard input to cmd.exe
.
This works when running cmd
as a normal user but fails when cmd
is run as Administrator.
This is my code:
Var
Wnd:hwnd;
begin
wnd:=FindWindow('ConsoleWindowClass',0);
if wnd <> 0 then
begin
setforegroundWindow(wnd);
keybd_event(Ord('A'),0,0,0);
end;
end;
Notice that ConsoleWindowClass
is the class name of cmd
.
How can I send input to cmd
when cmd
is running as administrator?
回答1:
How can I type with this code inside cmd when cmd is runing as administrator?
You cannot. This behaviour is by design. The cmd process runs with a higher integrity level than your process. Your only way to overcome this is to arrange that the process that fakes input also runs as administrator.
回答2:
Finally found that if we run the base application as admin and then try to send an input to cmd
it works fine.
来源:https://stackoverflow.com/questions/41186226/how-to-send-input-to-cmd-when-cmd-is-running-as-administrator