K
K
Konstantin12018-06-13 05:00:43
Delphi
Konstantin1, 2018-06-13 05:00:43

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;

The bottom line is that in the process that the service calls, OpenEvent ends with an ERROR_FILE_NOT_FOUND error. If you do it instead of OpenEvent -> CreateEvent, then everything seems to work, but it seems that this process has its own Event, albeit with the same name (you will never wait for WaitSingleObject in the service of this Event). Tried to do CreateEvent and then OpenEvent, then GetLastError produces 0xc0000008. What is this code is also unclear.
I tried to do it not from a service - I created a test application - everything starts working.
Probably a problem that these two processes are launched under different users. How to force to see Event from process started under other user?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question