Als Dienst gestartet funktioniert es nicht?



  • Hallo,

    ich habe ein kleine Programm geschrieben, welches ich mit srvany.exe als Dienst laufen lasse. Das Programm soll in einem Intervall x eine anders Programm aufrufen. Wenn man es normal startet funktioniert alles einwandfrei.
    Als Dienst gestartet ist der Prozess zwar offen aber es passiert nichts??
    Was mache ich falsch?

    Hier der Code:

    #include "stdafx.h"
    #include "windows.h"
    #include <string>
    #include <fstream>
    #include <stdio.h>

    using namespace std;

    void fehler ()
    {
    fstream ausgabestrom;//Daten schreiben
    DWORD msglen, ErrorCode;
    LPSTR msgBuf;
    string errorMeldung;
    ErrorCode = GetLastError();
    msglen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrorCode ,MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &msgBuf, 0, NULL);
    msgBuf [msglen] = TEXT('\0');
    errorMeldung=msgBuf;
    LocalFree (msgBuf);

    ausgabestrom.open("guardianerror.log",ios::out|ios::app);
    ausgabestrom<<errorMeldung;
    ausgabestrom.close();

    return;
    }

    void datenEinlesen(unsigned long &fTimetosleep)
    {
    fstream dateistrom;
    dateistrom.open("guardian.cfg",ios::out|ios::app);
    dateistrom.close();

    fstream eingabestrom; //Daten lesen
    eingabestrom.open("guardian.cfg",ios::in);

    eingabestrom>>fTimetosleep;
    return;
    }

    void main(int argc, char* argv[])
    {

    BOOL ret;
    string pfadDateiMailer =".\\mailer.exe";
    string pfadErrorlog;
    unsigned long timetosleep;

    datenEinlesen(timetosleep);
    while (1) // unendlich
    {
    STARTUPINFO startupInfo;
    GetStartupInfo (& startupInfo);
    PROCESS_INFORMATION processInfo;
    LPSTR castToLPSTR;
    LPCSTR castToLPCSTR;
    castToLPCSTR = pfadDateiMailer.c_str ();
    castToLPSTR =(LPSTR)castToLPCSTR;

    ret = CreateProcess (NULL, castToLPSTR ,NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInfo);

    if(ret==false)
    {
    fehler();
    }
    ret = CloseHandle(processInfo.hThread);

    if (ret == false)
    {
    fehler();
    }
    Sleep(timetosleep);
    }

    ExitProcess(0);
    };

    Danke im Voraus
    Erion



  • Bitte Code-Tags benutzen, das erleichtert das Lesen.

    cya 🙂



  • Okay, mein Fehler hier etwas übersichtlicher

    #include "stdafx.h"
    #include "windows.h"
    #include <string>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    void fehler ()
    {
        fstream ausgabestrom;//Daten schreiben
        DWORD msglen, ErrorCode;
        LPSTR msgBuf;
        string errorMeldung;
        ErrorCode = GetLastError();
        msglen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrorCode ,MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &msgBuf, 0, NULL);
        msgBuf [msglen] = TEXT('\0');
        errorMeldung=msgBuf;
        LocalFree (msgBuf);
    
        ausgabestrom.open("guardianerror.log",ios::out|ios::app);
        ausgabestrom<<errorMeldung;
        ausgabestrom.close();
    
        return;
    }
    
    void datenEinlesen(unsigned long &fTimetosleep)
    {
        fstream dateistrom;
        dateistrom.open("guardian.cfg",ios::out|ios::app);
        dateistrom.close();
    
        fstream eingabestrom;                   //Daten lesen
        eingabestrom.open("guardian.cfg",ios::in);
    
            eingabestrom>>fTimetosleep;
        return;
    }
    
    void main(int argc, char* argv[])
    {
    
        BOOL ret;
        string pfadDateiMailer =".\\mailer.exe";
        string pfadErrorlog;
        unsigned long timetosleep;
    
        datenEinlesen(timetosleep);
        while (1) // unendlich
        {
            STARTUPINFO startupInfo;
            GetStartupInfo (& startupInfo);
            PROCESS_INFORMATION processInfo;
            LPSTR  castToLPSTR;
            LPCSTR  castToLPCSTR;
            castToLPCSTR = pfadDateiMailer.c_str ();
            castToLPSTR =(LPSTR)castToLPCSTR;
    
            ret = CreateProcess (NULL, castToLPSTR ,NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInfo);
    
            if(ret==false)
            {
                fehler();
            }
            ret = CloseHandle(processInfo.hThread);
    
            if (ret == false)
            {
                fehler();
            }
            Sleep(timetosleep); // 360000 ms = 360 sec = 60 min = 1h -> Guardian schläft eine h
        }
    
        ExitProcess(0);
    };
    

    Gruß
    Erion



  • Schön wäre noch zu wissen, was nicht geht.

    Aber, ich glaube hier könnte es dran liegen:

    string pfadDateiMailer =".\\mailer.exe";
    

    Ist der Pfad denn gültig? Was ist denn in dem Moment das aktuelle Verzeichnis für den Dienst? Nie nicht hard gecodete Pfadangeben verwenden bei sowas.



  • Hallo,

    wenn man das Programm normal startet läuft alles einwandfrei.
    Richtet man es jedoch mit dem Systemprogramm srvany.exe als Dienst ein,
    wird es zwar beim Systemstart als Dienst gestartet(ist im Taskmanager zu sehen),
    aber es ruft die mailer.exe nicht mehr auf?

    @Luckie: Wie würdest Du den Pfad angeben?

    Gruß
    Erion



  • Du solltest den Pfad absolut angeben!


Anmelden zum Antworten