问题
I have a code that helps me run an external program as admin. It works smoothly if the user that is running my program is an admin.
procedure RunApp;
const
NotepadPath = 'C:\windows\system32\notepad.exe';
var
SI: TStartupInfo;
PI: TProcessInformation;
begin
ZeroMemory(@SI, SizeOf(SI));
SI.cb := SizeOf(SI);
CreateProcess(PChar(NotepadPath), nil, nil, nil, false, CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil, SI, PI);
end;
begin
Host := ParamStr(0);
FillChar(SEI, SizeOf(SEI), 0);
SEI.cbSize := SizeOf(SEI);
SEI.fMask := SEE_MASK_NOCLOSEPROCESS;
{$IFDEF UNICODE}
SEI.fMask := SEI.fMask or SEE_MASK_UNICODE;
{$ENDIF}
SEI.Wnd := 0;
SEI.lpVerb := 'runas';
SEI.lpFile := PChar(Host);
SEI.nShow := SW_NORMAL;
if IsUserAnAdmin then begin
RunApp;
Exit;
end;
if not ShellExecuteEx(@SEI) then begin
SetEnvironmentVariable('__COMPAT_LAYER', 'RUNASINVOKER');
RunApp;
end;
end.
But, when I run my program as a standard user, the UAC will prompt, after entering the admin password into UAC, the external program will automatically run in my admin user desktop instead of my current standard user account desktop.
Situation: Both account logged into my pc, User1(admin) and User2(non admin). In User2, I run that delphi program, an UAC is prompt for the external application (expected behaviour), I decided to run as an admin for the external application, so i key in my admin password.
What happened?: Window of my external application opens in User1 desktop instead of User2 desktop. Is this behaviour correct? If no, what could probably cause this?
来源:https://stackoverflow.com/questions/65486674/run-an-external-program-as-admin-in-a-standard-user-account-delphi