简体   繁体   中英

How do I check if my executable is running for a service or for a normal program?

I'm developing a win service in using mingw I've been trying for hours I looked for examples on the inte.net I used ChatGPT and nothing it returned works and the few examples I found had nothing to do with what I wanted

I hope there is some way to do this the idea and before i create the SERVICE_TABLE_ENTRY i can check if my executable was started for a windows service, if yes i create the SERVICE_TABLE_ENTRY if I don't do anything else.

If your program supports multiple modes then I would suggest using a command line parameter like /service for the service registration.

Alternatively you could check if your process token contains S-1-5-6 (SECURITY_SERVICE_RID) but I'm not sure which Windows version that was introduced.

I saw a simple way to check this just create SERVICE_TABLE_ENTRY and use StartServiceCtrlDispatcher if it returns an error probably the executable was started like a normal program.

SERVICE_TABLE_ENTRY _dispatcher_entry_table[] = 
    {

        {"", (LPSERVICE_MAIN_FUNCTION)serviceMain},
    
        {NULL, NULL}
    
    };

    if (StartServiceCtrlDispatcher(_dispatcher_entry_table))
    {
    
        //probably a normal program
    
    }

If you want to check if it's running you should check if the service is running. Check under services by running services.msc and looking for the service you registered.

Some time ago I wrote a simple library to make your program a service (on Windows) or daemon (on *nix): https://sourceforge.net/projects/daemonservice/

Maybe you can check the source on how it was done there...

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