Michael
Michael

Reputation: 6172

Windows Service C

I have written a windows service in C but the lack of documentation on writing a service in this language means that I have a few things I need help with.

  1. I want to create a config file that the service reads to get a few settings/options. I'm wondering what the best way to do this is and how to do it. The service .exe will be installed along with other files/programs at a user-specified location. The first question I have is should I keep this config file in the same directory as the service .exe or should I put it in an %appdata% folder that I create for my programs?

  2. How will the service be able to find the config file. Should I store the location of the config file in the registry and hardcode the registry key into the service? Or is it possible to install the service so that it has the config file path as a default command line argument? Or should I keep my config file in the same folder as the service .exe and use the registry HKLM/System/CurrentControlSet/Services/<servicename>/ImagePath?

  3. I have the following code:

void main() {
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = L"Service Name";
    ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;
    // Start the control dispatcher thread for our service
    StartServiceCtrlDispatcher(ServiceTable);
}
void ServiceMain(int argc, char** argv) { 
    //stuff here
}

Where does ServiceMain get its command line arguments from, is it the "Start parameters: " box under the specific service's properties? Is is possible to install it with some default parameters? Is it possible to add some in later programmatically?

  1. Is it possible to make my service conditionally start when another service starts?

  2. How can I install this service in production? I've been using sc from the windows sdk for development but this won't work in production. Is the only way to do this make sure microsoft .net framework is installed and then run the installutil program? My service is written without using .net framework so I would have hoped there would be a way to install the service without installing .net

Bonus Question: My service outputs to a log file. Is it possible for it to check how big the file is and delete it if it is over an arbitrary file size?

PS I don't know why the list resets to 1 & 2 after the code, it clearly says 4 and 5 in the edit box.

Upvotes: 1

Views: 1400

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596407

Many services use the following Registry subkey to hold config values:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\Parameters

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612993

  1. Since a service is machine-wide rather than per-user, store the configuration file under CSIDL_COMMON_APPDATA.
  2. Call SHGetFolderPath passing CSIDL_COMMON_APPDATA to find out where that folder is.
  3. The command line arguments argv come from the command line specified when the service was registered. This is what you can see from the service control manager.

I've not addressed the second set of questions. You should really only ask one question at a time. I do know a bit about installing services though. You certainly don't need any .net. Precisely how you should do it probably depends on which install tool you are using.

Upvotes: 2

Related Questions