Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Dienst automatisch starten (https://www.delphipraxis.net/26790-dienst-automatisch-starten.html)

endeffects 29. Jul 2004 14:21


Dienst automatisch starten
 
hallo,

ich hab mir heute einen kleines tool geschrieben
das ich als dienst laufen lassen möchte,
das installieren ist dabei kein problem
(filename.exe /install /silent)

allerdings läuft der dienst dann noch nicht
(Status beendet), wie kann ich den dienst
nach dem install automatisch starten lassen
ohne dies über den dienstemanager zu tun

Blutiger Anfänger 22. Aug 2004 01:06

Re: Dienst automatisch starten
 
Rate doch mal. Wenn das Programm den Dienst installieren kann, kann es ihn auch starten:

ms-help://MS.PSDK.1033/dllproc/base/starting_a_service.htm

Code:
DWORD StartSampleService()

    SERVICE_STATUS ssStatus;
    DWORD dwOldCheckPoint;
    DWORD dwStartTickCount;
    DWORD dwWaitTime;
    DWORD dwStatus;
 
    schService = OpenService( 
        schSCManager,         // SCM database
        "Sample_Srv",         // service name
        SERVICE_ALL_ACCESS);
 
    if (schService == NULL)
    { 
        MyErrorExit("OpenService");
    }
 
    if (!StartService(
            schService, // handle to service
            0,          // number of arguments
            NULL) )     // no arguments
    {
        MyErrorExit("StartService");
    }
    else
    {
        printf("Service start pending.\n");
    }
 
    // Check the status until the service is no longer start pending.
 
    if (!QueryServiceStatus( 
            schService,  // handle to service
            &ssStatus) ) // address of status information structure
    {
        MyErrorExit("QueryServiceStatus");
    }
 
    // Save the tick count and initial checkpoint.

    dwStartTickCount = GetTickCount();
    dwOldCheckPoint = ssStatus.dwCheckPoint;

    while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
    { 
        // Do not wait longer than the wait hint. A good interval is
        // one tenth the wait hint, but no less than 1 second and no
        // more than 10 seconds.
 
        dwWaitTime = ssStatus.dwWaitHint / 10;

        if( dwWaitTime < 1000 )
            dwWaitTime = 1000;
        else if ( dwWaitTime > 10000 )
            dwWaitTime = 10000;

        Sleep( dwWaitTime );

        // Check the status again.
 
        if (!QueryServiceStatus( 
                schService,  // handle to service
                &ssStatus) ) // address of structure
            break;
 
        if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
        {
            // The service is making progress.

            dwStartTickCount = GetTickCount():
            dwOldCheckPoint = ssStatus.dwCheckPoint;
        }
        else
        {
            if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
            {
                // No progress made within the wait hint
                break;
            }
        }
    } 

    if (ssStatus.dwCurrentState == SERVICE_RUNNING)
    {
        printf("StartService SUCCESS.\n");
        dwStatus = NO_ERROR;
    }
    else
    { 
        printf("\nService not started. \n");
        printf(" Current State: %d\n", ssStatus.dwCurrentState);
        printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
        printf(" Service Specific Exit Code: %d\n",
            ssStatus.dwServiceSpecificExitCode);
        printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
        printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
        dwStatus = GetLastError();
    } 
 
    CloseServiceHandle(schService);
    return dwStatus;
}


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:52 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz