The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


How to Start your Windows Service When a USB Flash Drive is Inserted

Windows Services are designed to start up at boot and run 24×7, but that framework is overkill for server software that must occasionally come alive to process specific events and go back to a waiting state. Service Triggers were introduced in Windows 7 to remedy the situation but at this point, only a handful of triggering events are supported. We look forward to more capabilities (and better documentation!) of this useful feature in future versions of Windows.

If you have a Windows Service that works with USB flash drives, you can use our free Service Trigger Editor utility to configure your service to start when a flash drive is inserted. To do so:

  1. Download Service Trigger Editor from our web site. Note that it is a standalone executable, ServiceTriggerEditor.exe, so just save it in a new folder on your hard drive.

  2. Run ServiceTriggerEditor.exe. You should see the standard security prompt if User Account Control (UAC) is enabled on your PC:


    Service Trigger Editor: UAC Prompt

    Service Trigger Editor is digitally signed by our company for your safety & security so please click Yes to proceed.

  3. Once the utility’s main window comes up, listing all services installed on your PC, find and highlight the service you wish to modify. We have selected the SyncToy service (created by our AlwaysUp product) for this tutorial.


    Service Trigger Editor: Main Window

  4. Select Trigger > Add… to summon the Add Trigger window. Adjust the settings to start the service when A specific device arrives (or is present at startup).


    Add a Device Arrival Trigger

  5. Next, click the Add… button on the right, enter USBSTOR\GenDisk in the window that comes up, and click OK to record that value.


    Add Trigger Data

    We’re now done configuring this new trigger so click the Save >> button.


    Add a Device Arrival Trigger

That’s it. Next time you insert a USB flash drive, your service will start. Enjoy!

Posted in Service Trigger Editor | Tagged , , | Leave a comment

Essential Tools for Windows Services: SC.EXE

While the useful NET.EXE utility is great for starting an stopping windows services, it cannot do much beyond that. Enter Microsoft’s SC.EXE – a versatile command-line utility built into Windows that can help you start, stop, restart or configure any Windows Service.

Type SC at a command prompt to see the extensive set of options available:

DESCRIPTION:
        SC is a command line program used for communicating with the
        Service Control Manager and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...
        The option  has the form "\\ServerName"
        Further help on commands can be obtained by typing: "sc [command]"
        Commands:
          query-----------Queries the status for a service, or
                          enumerates the status for types of services.
          queryex---------Queries the extended status for a service, or
                          enumerates the status for types of services.
          start-----------Starts a service.
          pause-----------Sends a PAUSE control request to a service.
          interrogate-----Sends an INTERROGATE control request to a service.
          continue--------Sends a CONTINUE control request to a service.
          stop------------Sends a STOP request to a service.
          config----------Changes the configuration of a service (persistent).
          description-----Changes the description of a service.
          failure---------Changes the actions taken by a service upon failure.
          failureflag-----Changes the failure actions flag of a service.
          sidtype---------Changes the service SID type of a service.
          privs-----------Changes the required privileges of a service.
          qc--------------Queries the configuration information for a service.
          qdescription----Queries the description for a service.
          qfailure--------Queries the actions taken by a service upon failure.
          qfailureflag----Queries the failure actions flag of a service.
          qsidtype--------Queries the service SID type of a service.
          qprivs----------Queries the required privileges of a service.
          qtriggerinfo----Queries the trigger parameters of a service.
          qpreferrednode--Queries the preferred NUMA node of a service.
          delete----------Deletes a service (from the registry).
          create----------Creates a service. (adds it to the registry).
          control---------Sends a control to a service.
          sdshow----------Displays a service's security descriptor.
          sdset-----------Sets a service's security descriptor.
          showsid---------Displays the service SID string corresponding to
                          an arbitrary name.
          triggerinfo-----Configures the trigger parameters of a service.
          preferrednode---Sets the preferred NUMA node of a service.
          GetDisplayName--Gets the DisplayName for a service.
          GetKeyName------Gets the ServiceKeyName for a service.
          EnumDepend------Enumerates Service Dependencies.

        The following commands don't require a service name:
        sc <server> <command> <option>
          boot------------(ok | bad) Indicates whether the last boot should
                          be saved as the last-known-good boot configuration
          Lock------------Locks the Service Database
          QueryLock-------Queries the LockStatus for the SCManager Database

Stopping/Starting a Service with SC

To stop a windows service from an elevated DOS prompt, run:

SC STOP <Service-Name>

where <Service-Name> is the name of the service. Be sure to enclose the name in quotes if it contains a space!

For example, to stop the Print Spooler service (named “Spooler”), run:

SC STOP Spooler


SC: Stopping the Print Spooler Service

Notice that the SC command will simply make a request for the service to stop and return immediately, before the service has actually stopped. This is evidenced by the STOP_PENDING state (which means that the service is in the process of winding down) returned in the screenshot above. If you plan to use this command in a batch file, you may need to add a sleep/pause after calling SC to give the service some time to respond. (The NET.EXE command, which will wait/block until the service has completely stopped, may be a better choice in this respect.)

Similarly, to start a windows service, use:

SC START <Service-Name>

Again, the request will be made but SC will not wait for the service to complete its startup before returning.

Using SC to Check the Status of a Service

To discover the state of your service, run SC with the QUERYEX option:

SC QUERYEX <Service-Name>

Check on the Spooler service like this:

SC QUERYEX Spooler


SC: Checking the Print Spooler Service

If the service is running, SC will return the underlying process identifier (“PID”) which can be used to manipulate the service’s process. This is very handy information when a service is stuck or unresponsive and must be forcibly terminated!

Disabling a Service

The CONFIG option enables you to modify a service’s settings. If you wish to disable a naughty service, preventing anyone from starting it, type:

SC CONFIG <Service-Name> start= disabled

For example, this command disables the infamous Interactive Services Detection Service (named “UI0Detect”):

SC CONFIG UI0Detect start= disabled

Note that the space in between “start=” and “disabled” is required!

How to Create a New Service with SC

SC can be used to create a new service as well. Type “SC CREATE” to see the many settings that can be applied but at a minimum you must specify:

  • the name of the service,
  • the display name of the service (a more descriptive moniker),
  • the full path to the executable hosting the service

For example, the following command creates a service called “MyService” with an executable located in “C:\MyService\MyService.exe”:

SC CREATE MyService binPath= “C:\MyService\MyService.exe” DisplayName= “My very cool service”

Once installed, you can work with the new service as normal in the Services application:

SC: New Service Created

Note that only executables explicitly written to interface with the Windows Service Control Manager should be installed this way. While SC will happily accept a regular, non-service binary, you will receive the fatal Error 1053 when you attempt to start the service. Employ a service wrapper like AlwaysUp in that situation.

Using SC to Delete a Service

The command to remove a service with SC is straightforward:

SC DELETE <Service-Name>

To discard the service named “MyService” that we installed above, use:

SC DELETE MyService

Needless to say, please use this command with caution!. Once a service is deleted it cannot be easily re-instated. Removing the wrong service can render your computer unusable!

SC is the Complete Command Line Utility for Windows Services

So whenever you need to work with a service via a batch file or from a DOS command prompt, look to SC for support. This versatile, essential tool has earned its reputation as the “Swiss Army Knife” for Windows Services!

Posted in Windows Services | Tagged , , | 8 Comments

Trouble with MT4 Build 600 and AlwaysUp?

Version 4 of the popular MetaTrader trading platform (affectionately called MT4) received a major overhaul in February. This forum post describes the many improvements in that “Build 600”, but perhaps the most dramatic changes were to the data storage layer:


The new version of the MetaTrader 4 terminal features the updated structure of user data storage. In earlier versions all programs, templates, profiles etc. were stored directly in terminal installation folder. Now all necessary data required for a particular user are stored in a separate directory called data folder.

Unfortunately this can cause MetaTrader terminals installed under AlwaysUp to stop working!

If you are having problems running MT4 as a Windows Service with AlwaysUp, please:

  1. Make sure you have your Windows account specified on the Logon tab, as described in step 6 of our MT4 tutorial. Using the default LocalSystem account is no longer an option now that the terminal’s data is kept in a user-specific folder.

    AlwaysUp Logon Tab

  2. When upgrading to build 600, MT4 moves many files automatically but some files will need to be moved manually. Please review “Automatic Copying of MQL4 Programs When Updating from Older Builds” and ensure that all your terminal’s files are migrated to the new locations.

Following this advice has worked for all MT4 customers experiencing problems with build 600 to date. Of course, please feel free to contact us if you have any questions or remain stuck after implementing the above!

Posted in AlwaysUp | Tagged , | Leave a comment

MyFolders Rated “Top Freeware” by Computer! Totaal Magazine

Computer! Totaal Magazine

We are pleased to announce that Dutch monthly magazine Computer! Totaal has ranked MyFolders as one of their “Top 15 freeware tips” in the January 2014 issue! Our convenient, intuitive utility that will save you time when working with your favorite folders came in at #12.

Posted in MyFolders | Tagged , | 3 Comments

Essential Tools for Windows Services: Process Explorer

If you want to understand what’s really going on with the programs on your computer, then look no further than Microsoft’s excellent (and free) Process Explorer. Think of it as the “Task Manager on steroids”, with the ability to show all processes, threads, handles, and of course, Windows Services running on your PC.

When launched, Process Explorer shows a colorful tree of all the active processes. The interface automatically refreshes itself every few seconds to highlight processes as they come and go. All Windows Services run under the wninit.exe > services.exe branch:


Process Explorer: Services Tree

Double-clicking an entry allows you to dig into a specific process. For example, here is what is shown for spoolsv.exe, the Windows Print Spooler:


Process Explorer: Spooler Process (spoolsv.exe)

You can start, stop, restart or even change the permissions of the Spooler service from the Services tab:


Process Explorer: Spooler Service

Back on Process Explorer’s main screen, summon the Lower Pane (View > Show Lower Pane) for some serious detective work. You can review all DLLs loaded, or even better, see all the files, registry keys and other objects locked by a process by viewing Handles for the lower pane (View > Lower Pane View > Handles). Here we see that iTunes (being run as a service with AlwaysUp) is using the “counters.dat” file:


Process Explorer: iTunes Handles

And perhaps most useful of all, Process Explorer can help you track down which application is preventing you from deleting a file or folder! Choose Find > Find Handle or DLL… and search for the file by name. Here we can see that the counters.dat file used by iTunes is also being held by Explorer and QuickBooks:


Process Explorer: Search

Process Explorer has many other interesting features. Easily terminate any process (and all its sup-processes if necessary), boost the priority of any process to make it run faster, and much more. Enjoy!

Posted in Windows Services | Tagged , , | Leave a comment