Registriert seit: 30. Okt 2004
475 Beiträge
Delphi 12 Athens
|
Re: Programm von Dienst starten lassen (Jetzt aber wirklich
21. Jun 2008, 20:27
Ja, klar. Hatte das ja schon bei meinem letzen Posting geschrieben. Habe eben noch die kleineren Anpassungen, die Du geschrieben hast, eingebaut.
So sieht das ganze in der letzten Version aus:
Delphi-Quellcode:
procedure StartApp(const App, Parameters, CurDir : TJwString);
var
StartupInfo : TStartupInfoA;
ProcInfo : TProcessInformation;
pEnv : Pointer;
pCurDir,
pCmdLine : TJwPChar;
ServiceToken, CopiedToken, UserToken : TJwSecurityToken;
begin
ZeroMemory(@StartupInfo, sizeof(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.lpDesktop := 'winsta0\default';
pCmdLine := TJwPChar('"'+App+'" ' + Parameters);
pCurDir := Nil;
if Length(CurDir) > 0 then
pCurDir := TJwPChar(CurDir);
//get the token from the service system session
ServiceToken := TJwSecurityToken.CreateTokenEffective(MAXIMUM_ALLOWED);
TJwAutoPointer.Wrap(ServiceToken);
//copy the token to be able to change the TokenSessionId
//Info: Win2000: Only 0
// WinXP: Service=0, 1.User=0, 2.User=1, 3.User=2, ...
// WinVista: Service=0, 1.User=1, 2.User=2, 3.User=3, ...
CopiedToken := TJwSecurityToken.CreateDuplicateExistingToken(ServiceToken.TokenHandle, MAXIMUM_ALLOWED);
TJwAutoPointer.Wrap(CopiedToken);
//get the token of the logged in user
if is2000 then
UserToken := TJwSecurityToken.CreateCompatibilityQueryUserToken(MAXIMUM_ALLOWED, 'explorer.exe')
else //XP, 2003, Vista, 2008
UserToken := TJwSecurityToken.CreateWTSQueryUserTokenEx(nil, WtsGetActiveConsoleSessionID);
TJwAutoPointer.Wrap(UserToken);
//give the copied token the same sessionid as the logged in user
CopiedToken.TokenSessionId := UserToken.TokenSessionId;
//create the environment block using the logged in user
JwaWindows.CreateEnvironmentBlock(@pEnv, UserToken.TokenHandle, false);
try
if not CreateProcessAsUser(
CopiedToken.TokenHandle,
TJwPChar(App), //__in_opt LPCTSTR lpApplicationName,
pCmdLine, //__inout_opt LPTSTR lpCommandLine,
nil, //__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
nil, //__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
false, //__in BOOL bInheritHandles,
CREATE_NEW_CONSOLE or CREATE_DEFAULT_ERROR_MODE or CREATE_UNICODE_ENVIRONMENT,
//__in DWORD dwCreationFlags,
pEnv, //__in_opt LPVOID lpEnvironment,
pCurDir, //__in_opt LPCTSTR lpCurrentDirectory,
StartupInfo, //__in LPSTARTUPINFO lpStartupInfo,
ProcInfo //__out LPPROCESS_INFORMATION lpProcessInformation
) then
raiseLastOsError;
finally
DestroyEnvironmentBlock(pEnv);
end;
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
|