简体   繁体   中英

Windows SC.exe how to query control codes on a service

What I Have Done

I am trying to implement ServiceBase.OnCustomCommand(int command) in a custom Windows Service called MyTestService , with custom command codes as done in this answer :

public enum MyCustomCommands { ExecuteScript = 128 };

I noticed it's common to start custom command values at 128 .


I attempted to implement custom command codes like so:

public enum MyCustomCommandCodes
{
    Test1 = 1,
    Test2 = 2,
}

protected override void OnCustomCommand(int command)
{
    switch (command)
    {
        case (int)MyCustomCommandCodes.Test1:
            eventLog1.WriteEntry("Test1");
            break;
        case (int)MyCustomCommandCodes.Test2:
            eventLog1.WriteEntry("Test2");
            break;
        default:
            eventLog1.WriteEntry("default");
            break;
    }
}

I am calling OnCustomCommand using Window's command-line utility Service Control (SC) .

When I enter sc control MyTestService 1 , I found instead of calling case Test1 , it actually stopped the service.

In my mind, this explains why people start command values at 128 , it's to prevent overlap with codes already in use.


I would like to be able to see which control codes are already in use by a Windows service.

Two-part Question :

  • Is there a way to query the list of all codes that are in use?
  • If yes, is there also a way to query what each code actually does?

Per @madreflection's comment, which points out the ControlService function: https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-controlservice

User-defined control code range: 128 to 255 .

And it also states why sc control MyTestService 1 stops the service, it's because 0x1 corresponds with the SERVICE_CONTROL_STOP control code.


This answers my question from a different direction, with a lookup table given by Microsoft.

Any other answers showing control code querying functions are still welcome:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM