CreateProcessAsUser
{$R *.dfm}
function StartInteractiveClientProcess (
lpszUsername: LPTSTR; // client to log on
lpszDomain: LPTSTR; // domain of client's account
lpszPassword: LPTSTR; // client's password
lpCommandLine: LPTSTR // command line to execute
): boolean;
var
hToken: THandle;
pi: TProcessInformation;
si: TStartupInfo;
bResult: BOOL;
str: LPTSTR;
isLogon: boolean;
label Cleanup;
begin
bResult := FALSE;
isLogon := LogonUser(
lpszUsername,
lpszDomain,
lpszPassword,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
hToken);
if (not isLogon) then
begin
goto Cleanup;
end;
// Initialize the STARTUPINFO structure.
// Specify that the process runs in the interactive desktop.
ZeroMemory(@si, sizeof(STARTUPINFO));
si.cb := sizeof(STARTUPINFO);
si.lpDesktop:=PChar('winsta0\default');
// Launch the process in the client's logon session.
bResult := CreateProcessAsUser(
hToken, // client's access token
nil, //NULL, // file to execute
lpCommandLine, // command line
nil, //NULL, // pointer to process SECURITY_ATTRIBUTES
nil, //NULL, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, // creation flags
nil, //NULL, // pointer to new environment block
nil, //NULL, // name of current directory
si, // pointer to STARTUPINFO structure
pi // receives information about new process
);
Win32Check(bResult);
CloseHandle(hToken);
Cleanup:
StartInteractiveClientProcess:=bResult;
end;