Announcement

Collapse
No announcement yet.

Delphi.exe als Applikation oder als Windows Dienst starten

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delphi.exe als Applikation oder als Windows Dienst starten

    Habe vor einiger Zeit eine Delphi VCL Formular Anwendung geschrieben und auch über die Jahre gepflegt. Läuft prima, auch an Win7 angepasst.
    Nun ist der Wunsch aufgekommen, diese EXE als Windows Dienst laufen zu lassen. Nichts leichter als das: Mit Delphi Wizard einen Dienst erstellt und das alte Hauptfenster unter
    Application.CreateForm(TService,Service);
    Application.CreateForm(TForm1,Form1);
    eingebaut. Läuft auch alles ohne Probleme. Aber jetzt ist weiteres begehren aufgetaucht: Die beiden *.dpr Dateien unterscheiden sich minimal. Gibt es eine Möglichkeit zu erkennen, ob die EXE als Dienst oder als Formular Anwendung gestartet wird? Dann bräuchte ich keine 2 EXE erstellen und wenn Formulare gewünscht wären (da ein Dienst unter Win7 mit Desktop 0 ja nicht mit den user Desktop spricht) könnte dieser einfach so gestartet werden.

  • #2
    Danke, habe es selbst gefunden...!
    Eigentlich ist es ganz einfach: Es ist bloß herauszufinden, welcher Parent die Applic gestartet hat. Und beider Code, der für das Formular und der für den Service ist zu integrieren.
    Für interessierte: Hier der Code der dpr Datei...

    program MyApplic;

    uses
    Windows, Forms, Tlhelp32, SysUtils, SvcMgr,
    uMyApplic in 'uMyApplic.pas' {wMyApplic: TwMyApplic},
    uMyApplicService in 'uMyApplicService.pas' {wMyApplicService: TwMyApplicService};

    {$R *.RES}

    var
    ProcessId, ParentProcessId : Cardinal;
    ParentFileName : String;
    hSnapShot: THandle;
    ProcInfo: TProcessEntry32;

    begin
    //get my own process ID
    ProcessId := GetCurrentProcessId();
    ParentProcessId := 0;
    ParentFileName := '';
    //find out the parent process ID
    hSnapShot := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapShot <> THandle(-1)) then
    try
    ProcInfo.dwSize := SizeOf(ProcInfo);
    if (Process32First(hSnapshot, ProcInfo)) then
    repeat
    if ProcInfo.th32ProcessID = ProcessId then begin
    ParentProcessId := ProcInfo.th32ParentProcessID;
    break;
    end;
    until not Process32Next(hSnapShot, ProcInfo);
    finally
    CloseHandle(hSnapShot);
    end;
    //find out the parent file name
    if ParentProcessId>0 then begin
    hSnapShot := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapShot <> THandle(-1)) then
    try
    ProcInfo.dwSize := SizeOf(ProcInfo);
    if (Process32First(hSnapshot, ProcInfo)) then
    repeat
    if ProcInfo.th32ProcessID = ParentProcessId then begin
    ParentFileName := LowerCase(ExtractFileName(ProcInfo.szExeFile));
    break;
    end;
    until not Process32Next(hSnapShot, ProcInfo);
    finally
    CloseHandle(hSnapShot);
    end;
    end;
    //run as Service or as Formular application
    if (ParentFileName = 'services.exe') OR
    (ParamStr(1)='/install') OR (ParamStr(1)='/uninstall') then begin
    if not SvcMgr.Application.DelayInitialize or SvcMgr.Application.Installing then
    SvcMgr.Application.Initialize;
    SvcMgr.Application.CreateForm(TwMyApplicService, wMyApplicService);
    SvcMgr.Application.CreateForm(TwMyApplic, wMyApplic);
    SvcMgr.Application.Run;
    end else begin
    //check wheter another instance is already running
    if IsAnotherInstanceRunning('MyApplic') then begin
    //terminate this instance
    Sleep(2);
    Forms.Application.Terminate;
    end else begin
    Forms.Application.Initialize;
    Forms.Application.Title := 'MyApplic';
    Forms.Application.CreateForm(TwMyApplic, wMyApplic);
    Forms.Application.Run;
    end;
    end;
    end.

    Comment

    Working...
    X