delphi - Error when running program with VirtualShellTools from a service -
i create service in delphi. need service run program. in windows 7, use code execute program :
procedure executeprocessasloggedonuser(filename: string); implementation function getshellprocessname: string; var reg: tregistry; begin reg := tregistry.create(key_read); try reg.rootkey := hkey_local_machine; reg.openkeyreadonly ('software\microsoft\windows nt\currentversion\winlogon'); result := reg.readstring('shell'); reg.free; end; end; function getshellprocesspid(const name: string): longword; var snapshot: thandle; process: tprocessentry32; b: boolean; begin result := 0; snapshot := createtoolhelp32snapshot(th32cs_snapprocess, 0); if snapshot <> invalid_handle_value try fillchar(process, sizeof(process), 0); process.dwsize := sizeof(process); b := process32first(snapshot, process); while b begin if comparetext(process.szexefile, name) = 0 begin result := process.th32processid; break; end; b := process32next(snapshot, process); end; closehandle(snapshot); end; end; function getshellhandle: thandle; var pid: longword; begin pid := getshellprocesspid(getshellprocessname); result := openprocess(process_all_access, false, pid); end; procedure executeprocessasloggedonuser(filename: string); var ph: thandle; htoken, ntoken: thandle; procinfo: tprocessinformation; startinfo: tstartupinfo; begin ph := getshellhandle; if ph > 0 begin if openprocesstoken(ph, token_duplicate or token_query, htoken) begin if duplicatetokenex(htoken, token_assign_primary or token_duplicate or token_query, nil, securityimpersonation, tokenprimary, ntoken) begin if impersonateloggedonuser(ntoken) begin // initialize startupinfo structure fillchar(startinfo, sizeof(tstartupinfo), 0); startinfo.cb := sizeof(tstartupinfo); // specify process runs in interactive desktop startinfo.lpdesktop := pchar('winsta0\default'); // launch process in client's logon session createprocessasuser(ntoken, nil, pchar(filename), nil, nil, false, create_new_console or normal_priority_class, nil, nil, startinfo, procinfo); // end impersonation of client reverttoself(); end; closehandle(ntoken); end; closehandle(htoken); end; end; end; the code works fine "empty" program. drop tvirtualexpolorertreeview onto form of program. if start service there error when program being called. guess program can't enumerate pidl or blabla (i don't know windows shell). how force program can run normally?
your winsta0 might cause:
starting windows vista, way services (and processes started services) can interact desktop changed, services no longer run in same session user @ console.
by default, cannot interact desktop more.
see this thread nice links on matter.
Comments
Post a Comment