Welcome to the myhosting.com Forums.
[RESOLVED] Toggling a service under Windows7
I have these set of functions that have been working under WindowsXP and Vista. They Toggle a particular service. That is, they stop the service if it is running, and once it is stopped, it restarts the service. If anything goes wrong, such as a function to OpenSCManager or OpenService, etc... then the error is logged. It has been working for years, but under Win7, something odd is happening.
1. There are no failures, the return codes from all calls are reported as non-zero (success).
2. When you do a fresh reboot of Windows7, the "SERVICE_CONTROL_STOP" to the "Dnscache" returns non-zero (success).
3. All the functions that check the actual status of the service return non-zero.
4. "(SERVICE_STOPPED == ServiceStatus.dwCurrentState)" will always evaluate to false.
My toggle service will run and check once a second forever waiting for the service to stop, which it never will. You can go to the Service Manager and verify it is still running.
Now here is where it gets stranger. You can then go to the service manager and STOP the service (which gets my program out of the wait loop). After this point, until you reboot Windows7, my application will work correctly. It can toggle the service all day and succeed.
The same code that gets "stuck" on a fresh reboot, will not get stuck after you use the Service Manager (via the control panel or task manager) to stop the service once.
I did not post my code because it is very basic and you can pretty much look at the MSDN sample code and it looks very much like my code (cause I cut and paste most of it). If you want my code I can post it, although it is very vanilla.
Is there something special going on with Windows7? Something extra that may need to be done that is not needed on Vista? I have no idea why using the Service Manager "un-breaks" my program.
BTW my program is another service which runs as LocalSystem, so it has the privileges to do all this. Furthermore all the functions succeed, none of them are returning an error.
I just have no idea why this is happening on Windows7.
By Affiliate-Manager in forum Affiliates
Last Post: 2009-05-30, 09:33 AM