You can start
gui from a service. You need to use the logon credentials of the user not the service. You cannot create a window for the user display using session 0 - however you could create window for session 0, but it would not be accessible. Actually Vista do show such a window on a new desktop, but this could change on next update.
This example shows how to start cmd.exe from a service using the user credentials of the interactive logon session the user logged on. This does not work, if no user is logged on!
To use WTSQueryUserToken you must include
Jedi Api Lib or just load it from windows
dll. (see
MSDN for more information) -
http://jedi-apilib.sourceforge.net
Delphi-Quellcode:
procedure TService1.ServiceExecute(Sender: TService);
var hToken : THandle;
si: TStartupInfo;
pi: TProcessInformation;
bTerminate: Boolean;
ACurrentDir: String;
begin
if WTSQueryUserToken(WtsGetActiveConsoleSessionID, hToken) then
begin
FillChar(si, SizeOf(si), 0);
with si do
begin
cb := SizeOf(si);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_NORMAL;
end;
if not CreateProcessAsUser(hToken, PChar('cmd.exe'), nil, nil, nil, False,
NORMAL_PRIORITY_CLASS or CREATE_NEW_PROCESS_GROUP,
nil, nil, si, pi) then
MessageBox(0,PChar(SysErrorMessage(GetLastError)),'',MB_SERVICE_NOTIFICATION or MB_OK)
end
else
MessageBox(0,PChar(SysErrorMessage(GetLastError)),'',MB_SERVICE_NOTIFICATION or MB_OK);
CloseHandle(hToken);
end;