Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: TNotificationCenter: Die Benachrichtigungsplattform ist nicht verfügbar

  Alt 11. Apr 2019, 11:11
Deine App läuft ja im jeweiligen Benutzerkontext (nehme ich an).
Du musst also nur schauen, ob ein Dienst läuft, der mit "WpnUserService" anfängt:

Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

// nach einer Idee von: http://www.chami.com/tips/delphi/040698D.html


uses
  System.SysUtils,
  Winapi.Windows,
  Winapi.WinSvc;

function IsServiceRunning(const AServiceName: string): Boolean;
const
  // assume that the total number of services is less than 4096. increase if necessary
  cnMaxServices = 4096;

type
  TSvcA = array [0 .. cnMaxServices] of TEnumServiceStatus;
  PSvcA = ^TSvcA;

var
  I: integer;
  // service control manager handle
  schm: SC_Handle;
  // bytes needed for the next buffer, if any
  nBytesNeeded,
  // number of services
  nServices,
  // pointer to the next unread service entry
  nResumeHandle: DWORD;
  // service status array
  ssa: PSvcA;

  dwServiceType: DWORD;
  dwServiceState: DWORD;
begin
  Result := False;

  // connect to the service control manager
  schm := OpenSCManager(nil, nil, SC_MANAGER_ENUMERATE_SERVICE);

  if (schm > 0) then
  begin
    nResumeHandle := 0;

    New(ssa);
    try
      dwServiceType := SERVICE_WIN32_SHARE_PROCESS;
      dwServiceState := SERVICE_ACTIVE;

      EnumServicesStatus(
        schm,
        dwServiceType,
        dwServiceState,
        ssa^[0],
        SizeOf(ssa^),
        nBytesNeeded,
        nServices,
        nResumeHandle);

      // assume that our initial array was large enough to hold all
      // entries. add code to enumerate if necessary.

      for I := 0 to nServices - 1 do
      begin
        Result := string(ssa^[I].lpServiceName).StartsWith(AServiceName);
        if Result then
          Break;
      end;
    finally
      Dispose(ssa);

      // close service control manager handle
      CloseServiceHandle(schm);
    end;
  end;
end;

begin
  try
    Writeln('Is a WpnUserService running? ', IsServiceRunning('WpnUserService').ToString(TUseBoolStrs.True));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;

end.
  Mit Zitat antworten Zitat