Register application to start on system event - windows

I have a windows service. I also develop a driver for a device.
I want my service to start only when the device is plugged in.
I thought that maybe I can somehow start the service from the driver, but after googling it, I understood that it is forbidden.
Is there any other way to start a service or an application from system event.

The StartService Function documentation discusses conditions for starting a service, including discussions about when it cannot be used and how to check for that condition. It specifically says that:
When a driver service is started, the StartService function does not
return until the device driver has finished initializing.
So, you'd have to complete device initialization before issuing the StartService, but that should be possible?

What I am going to do is to register my application as a task in Task Scheduler, and set the trigger to event in the event log.
Then in my driver I will raise this specific event.
This way I can run an application from the driver.

Related

Run background service immediately on registration WindowsPhone 8.1

I have implemented a background service that sends some data to server.
But the service is only triggered after the trigger is fired.
I want the service to fire immediately as and when its registered.
I have tried
DeviceTrigger
LocationTrigger
SystemTrigger(Internet available/Timezone-changed)
MaintenanceTrigger
TimeTrigger
but they all are fired when their conditions are met as documented. Does any one have any idea how to get this work, btw I'm on windows phone 8.1 and C++.
PS. I just want my service to get triggered as it is registered.
AFAIK you can’t do that directly. Microsoft is very strict about what background tasks can and cannot do because battery life depends on it.
There’s a workaround.
Move your server-sending functionality from your background service into a separate DLL. Call that DLL from both background service process, and the GUI process. If you need to, in that DLL you can guard shared resources with a named mutex or use any other IPC mechanism that works across processes.

How to tell a Windows service to restart or reload its configuration?

I have a GUI application that is used to configure a windows service and I want to start, stop or restart this service from thing GUI application.
One of the problems is that this service can run in two modes: windows service or manually from command line (so you have a console).
Now, I am looking for a way to communicate this, a way that would work in both cases.
It may be helpfull to specify that the service process already has and even handler that is dealing with:
CTRL_C_EVENT
CTRL_CLOSE_EVENT
CTRL_BREAK_EVENT
CTRL_LOGOFF_EVENT
CTRL_SHUTDOWN_EVENT
It would be quite easy to extend the handler if I knew what event will be triggered by the service restart option. Still, I should be able to trigger the same event from my GUI application so I will be able to tell the "server" to restart itself even if it is running in background.
The best is would be if I could do this without having to detect in which mode the server application is running.
There are Service Trigger Events that your service can listen for, and then your GUI can send an event. In particular, you want to register for an SERVICE_TRIGGER_TYPE_CUSTOM trigger, which causes your service to listen for an ETW event; your GUI code would then write the ETW event to trigger the event.
There is no service restart action. All you would do is stop the service normally, then start the service normally afterwards. That is all the SCM restart does internally, it is just exposed as a single operation in the SCM UI instead of as two separate options.

Windows Kernel Driver Boot\winlogon complete callback

Can I get an event callback to my kernel driver when the boot process has completed, or when a user logs in?
The simple answer is no.
The long answer is yes, but why?
I'll answer the second part, because it's easier. You can easily register to recieve a notification when any process is launched. A short examination of Windows Internals will tell you that from Vista and up, the process userinit.exe is the first process to be executed in any given user session.
To the first part, this very much changes depending on your definition of boot process. Is it when a GUI is loaded? Is it when the computer can receive network requests? Does it matter which network requests (TCP/IP, SMB, RPC)?
The answer to each of these is very different.
When win32K has finished loading
When the TCP/IP stack drivers finish loading
When specific services (RPC, Server service) are done loading
What is the problem you're trying to solve?

How to code the OnStart function for windows service program?

I created a service in Visual C++. Installed it using instsrv.exe and srvany.exe.
When I check at the Services applet in Control Panel, the service is registered. But when I want to start it, this message appears:
the service on Local Computer started and then stopped. Some service stop automatically if they have no work to do, for example the Performance logs and Alert Service.
I just have this code inside my OnStart function:
(void)system("C:\ReleaseExe\Example1Server.exe");
I my code correct? Or is there any other way to code it to make sure that the service can be in running state?
Assuming that exe you are starting up is actually your service, this is the wrong way to do it.
The Windows Service Control Manager (SCM) actually calls into your service's EXE via defined entry points - you need to provide the logic to do what your particular service needs in those entry points, including notifying the SCM of state changes in your service (STOPPED-> RUNNING, RUNNING->STOPPED, and so on).
Read the background info on MSDN, starting here, for clarification of what you must provide. Note that OnStart is specific to implementing Services in managed code, you won't be doing it that way in Visual C++. Perhaps C# would be an easier route?

Detecting windows shutdown event

I have a windows Service always running once PC started, how would i detect using .NET 3.5 or WMI, when the user or some other application/process has requested a shutdown or reboot in windows.
I do not want to stop it, just log the time and who initiated the shutdown (user or process is fine, don't really need to know which process)
Thank you
Use the Microsoft.Win32.SystemEvents.SessionEnding event.
However, note that you are in a Windows Service, and this event won't fire for you unless you create a hidden form in your service. You can get one of these for free by enabling the "Interact with Desktop" option on the service.
You're looking for the SystemEvents.SessionEnding event.
Occurs when the user is trying to log
off or shut down the system.
Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.

Resources