I'm trying to call execute an application from another windows application under another users account using this code:
procedure TForm1.Button3Click(Sender: TObject);
var
userName: string;
password: string;
domain: string;
path: string;
logonok: boolean;
impok: boolean;
hUserToken: THandle;
ProcessCreatedOK: Boolean;
startupInfo: TStartupInfo;
processInfo: TProcessInformation;
err: DWORD;
begin
username := 'theusername';
password := 'thepassword';
domain := '';
Path := 'myapp.exe';
LogonOK := LogonUser(PWideChar(username), PWideChar(domain), PWideChar(password),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hUserToken);
if LogonOK then
begin
impOK := ImpersonateLoggedOnUser(hUserToken);
if impok then
begin
FillChar(startupInfo, SizeOf(startupInfo), 0);
startupInfo.CB := SizeOf(startupinfo);
startupInfo.dwFlags := STARTF_USESHOWWINDOW;
startupinfo.wShowWindow := SW_SHOWNORMAL;
NewState.PrivilegeCount := 1;
res := LookupPrivilegeValue(
nil,
SE_ASSIGNPRIMARYTOKEN_NAME,
NewState.Privileges[0].Luid);
Win32Check(res);
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
res := AdjustTokenPrivileges(
hUserToken,
False,
NewState,
SizeOf(NewState),
nil,
returnLength);
Win32Check(res);
NewState.PrivilegeCount := 1;
res := LookupPrivilegeValue(
nil,
SE_INCREASE_QUOTA_NAME,
NewState.Privileges[0].Luid);
Win32Check(res);
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
res := AdjustTokenPrivileges(
hUserToken,
False,
NewState,
SizeOf(NewState),
nil,
returnLength);
Win32Check(res);
ProcessCreatedOK := CreateProcessAsUser(
hUserToken,
nil,
PChar(path),
nil, nil,
false,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil,
startupInfo, processInfo);
if GetLastError <> 0 then
begin
err := GetlastError;
ShowMessage(inttostr(err) + ' ' + SysErrorMessage(err));
end;
end;
end;
I'm getting a 1314 "A required privilege is not held by the client" error.
Now from some digging around I can see that I need to apply some privileges to the impersonated account: SE_ASSIGNPRIMARYTOKEN_NAME and SE_INCREASE_QUOTA_NAME?
Does anyone have some suggestions on how to do this?
I've seen the JCL example given in this Stackoverflow answer but I get similar issues with regards to account privileges.
I'd prefer not to use CreateProcessWithLogonW - I can explain why on request.