How to send input to cmd when cmd is running as administrator?

∥☆過路亽.° 提交于 2021-01-23 07:53:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!