Answer the question
In order to leave comments, you need to log in
How to work with CreateEvent OpenEvent in different processes started by different users?
There is a service written in Delphi. She sometimes needs to receive information from the user. To do this, it launches a process under the user who is looking at the monitor. In this case, the service should not hang waiting for the process to complete.
If UseDensityOnVesna then begin
Events[l].SyncEventID:=CurSyncEventID;
Events[l].hevent:=CreateEvent(nil,True,False,PChar('DensityEvent'+IntToStr(CurSyncEventID)));
CallDensityEXE(l);
inc(CurSyncEventID);
end;
procedure TdmVesna.CallDensityEXE(l:integer);
var
AppPath,s{,EventName}:string;
cpr:bool;
si:STARTUPINFO;
pi:PROCESS_INFORMATION;
hToken:dword;
GetToken:boolean;
begin
Log2('Запуск Density SyncEventid='+IntToStr(Events[l].SyncEventID),2);
AppPath:=ExtractFilePath(ParamStr(0));
s:=AppPath+'Density.exe '+IntToStr(Events[l].SyncEventID)+' '+IntToStr(Events[l].Value)+' '+DateTimeToStr(Events[l].dt);
//EventName:='DensityEvent'+IntToStr(Events[l].SyncEventID);
//Events[l].hevent:=CreateEvent(nil,True,False,PChar(EventName));
Events[l].Density:=0;
Events[l].ObjectID:=0;
FillChar(si,sizeof(STARTUPINFO),0);
si.cb:=sizeof(STARTUPINFO);
si.wShowWindow:=SW_SHOWNORMAL;
si.dwFlags:=STARTF_USESHOWWINDOW;
si.lpDesktop:=nil;
//cpr:=CreateProcess(nil,PChar(s),nil,nil,false,NORMAL_PRIORITY_CLASS,nil,PChar(AppPath),si,pi);
GetToken:=WTSQueryUserToken(WtsGetActiveConsoleSessionID,@hToken);
If not GetToken then Log2('Процесс Density не создан, нет токена!')
else begin
cpr:=CreateProcessAsUser(hToken,nil,PChar(s),nil,nil,True,0,nil,nil,si,pi);
if cpr then begin
Log2('Процесс SyncEventid='+IntToStr(Events[l].SyncEventID)+' создан',2);
end else begin
Log2('Ошибка создания процесса SyncEventid='+IntToStr(CurSyncEventID)+' '+SysErrorMessage(GetLastError));
end;
end;
end;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question