Windows关机重启后C++服务无法启动问题咨询
Hey there, let’s break down why your C++ Windows service might be failing to start after a reboot, and walk through fixes for the most common issues:
1. Check Service Dependencies Configuration
You modified the service to use SERVICE_AUTO_START and passed SERVICE_DEPENDENCIES to InstallService. If this macro isn’t properly defined (e.g., it’s set to an empty string or invalid service names), the service might fail to start because it’s waiting on non-existent or unready dependencies:
- Open the Services console (
services.msc), find your service, right-click > Properties > Dependencies tab. Verify all listed dependencies are valid and set to start automatically. - If you don’t need specific dependencies, pass
NULLinstead ofSERVICE_DEPENDENCIESin theInstallServicecall to remove unnecessary dependencies.
2. Switch to Delayed Auto-Start to Fix Timing Issues
Some auto-start services fail because they try to launch before critical system services (like WinLogon, which handles user logins) are fully ready. Switching to delayed auto-start can resolve this timing conflict:
- Modify your
InstallServicecall to useSERVICE_AUTO_START_DELAYEDinstead ofSERVICE_AUTO_START:InstallService( SERVICE_NAME, SERVICE_DISPLAY_NAME, SERVICE_AUTO_START_DELAYED, // Use delayed auto-start to avoid race conditions NULL, // Remove dependencies if not required SERVICE_ACCOUNT, SERVICE_PASSWORD ); - Reinstall the service to apply this change.
3. Verify Service Account Permissions
Your service needs sufficient privileges to launch an elevated program without UAC prompts, and to start itself on boot:
- By default, the CppWindowsService template uses the
LocalSystemaccount, which has full system privileges. If you changed the service account to a different user, ensure it has the Log on as a service right (configured via Local Security Policy > Local Policies > User Rights Assignment) and administrative privileges. - Double-check the service’s login credentials in
services.msc> Properties > Log On tab. Ensure the account is valid and the password is correct (if using a domain/local user).
4. Dig Into Windows Event Logs for Exact Error Details
Windows logs every service startup failure with a specific error code, which is the fastest way to pinpoint the root cause:
- Open Event Viewer > Windows Logs > System.
- Look for events from the Service Control Manager source with an Error level. The event description will include an error code (e.g., 1053 for timeout, 1067 for unexpected process termination) and details about why the service failed to start.
- For example, error 1067 might mean the service’s main process crashed on startup—this could be due to invalid program paths, missing dependencies for your launched program, or unhandled exceptions in your service code.
5. Validate Your Program Launch Logic
Ensure the code that launches your target program is robust:
- Always use absolute paths for the program you’re launching (e.g.,
C:\MyApp\MyProgram.exeinstead ofMyProgram.exe). The service’s working directory is usuallyC:\Windows\System32, so relative paths will fail to find your program. - Verify that the program can run without user interaction—UAC prompts are suppressed, but if the program requires any GUI input or relies on user-specific environment variables, it might crash silently.
After trying these steps, reinstall your service and test a reboot to see if it starts successfully.
内容的提问来源于stack exchange,提问作者dev65

