2

The service was on Windows 10, I compiled it with VS2015 and language was C. an exe will be called in the function ServiceMain, it ran normally when I launched service manually and got results as expected from exe. but when I shutdown PC, then started. Service was running automatically, but the exe was not launched, I must launched it manually if I have booted my PC. and the log was gone when the process went into the function ServiceMain. I'm confused thoroughly.

ServiceMain codes as below:

    void ServiceMain(int argc, char** argv)
    {
        int error;
        WriteToLog("Service Main");
        ServiceStatus.dwServiceType =SERVICE_WIN32; 
        ServiceStatus.dwCurrentState =SERVICE_START_PENDING;
        ServiceStatus.dwControlsAccepted =SERVICE_ACCEPT_STOP 
 |SERVICE_ACCEPT_SHUTDOWN ;
        ServiceStatus.dwWin32ExitCode = 0;
        ServiceStatus.dwServiceSpecificExitCode = 0;
        ServiceStatus.dwCheckPoint = 0;
        ServiceStatus.dwWaitHint = 0;

        hStatus = RegisterServiceCtrlHandler(ServiceName,
     (LPHANDLER_FUNCTION)ControlHandler);
        if (hStatus == (SERVICE_STATUS_HANDLE)0)
        {
            return;
        }
        ServiceStatus.dwCurrentState = SERVICE_RUNNING;
        SetServiceStatus(hStatus, &ServiceStatus);
        installHelper.RunService();

        while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
        {
            Sleep(SLEEP_TIME);
        }
        return;
    }

The exe is called by installHelper.RunService(). Is there any wrong with these codes? and how should I get exe run automatically on service?

I'm a green hand for the service job. Appreciate for your help in advance!

stanway
  • 29
  • 2
  • There is a reference implementation here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687414(v=vs.85).aspx – ta.speot.is Nov 29 '17 at 01:22
  • I'm not sure if it involves privileges, service is running on Session0 with SYSTEM privilege when PC boot up, and exe has been switched into current logon user session, its privilege is SYSTEM also. I have tried switch exe's privilege into current user, exe still can't launched by service automatically except it can't read some files from drive C when boot up. – stanway Nov 29 '17 at 02:24
  • @ta.speot.is, thanks for your reply.but it can't resolve my question. – stanway Nov 29 '17 at 02:38
  • I have registered exe into path \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run successfully. but still cannot be launched automatically when boot up. – stanway Nov 30 '17 at 01:07

0 Answers0