窗口服务C [英] Windows Service C

查看:160
本文介绍了窗口服务C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C写的一个窗口服务,但是,在写这种语言服务缺乏文档意味着我有几件事我需要帮助。


  1. 我想创建一个配置文件,该服务读取获得几个设置/选项。我不知道该怎么办最好的方法是什么以及如何去做。该服务的.exe将与用户指定的位置等的文件/节目一起安装。第一个问题我已经是我应该保持在同一目录中这个配置文件作为服务.exe或我应该把它,我为我的程序创建%APPDATA%文件夹中?


  2. 该服务如何能够找到的配置文件。我应该存储在注册表和硬盘code中的注册表项到服务配置文件的位置?或者是有可能安装服务,以便它有一个默认的命令行参数的配置文件路径?或者我应该把我的配置文件在同一文件夹作为服务.exe和使用注册表 HKLM /系统/ CurrentControlSet /服务/<服务名>?/的ImagePath


  3. 我有以下的code:


 无效的主要(){
    SERVICE_TABLE_ENTRY ServiceTable [2];
    ServiceTable [0] = .lpServiceName L服务名称;
    ServiceTable [0] .lpServiceProc =(LPSERVICE_MAIN_FUNCTION)的ServiceMain;
    ServiceTable [1] .lpServiceName = NULL;
    ServiceTable [1] .lpServiceProc = NULL;
    //开始控制调度线程为我们的服务
    StartServiceCtrlDispatcher(ServiceTable);
}
无效的ServiceMain(INT ARGC,字符** argv的){
    //这里的东西
}

哪里的ServiceMain从中获取其命令行参数,它是启动参数:框下的特定服务的属性?是可以和某些缺省参数安装呢?是否有可能增加一些在以后的编程?


  1. 是否有可能使另一个服务启动时,我的服务条件启动?


  2. 我如何安装在生产这种服务?我一直在使用从Windows SDK的开发SC但这不会在生产工作。为做到这一点请确保安装Microsoft .NET框架,然后运行installutil程序的唯一途径?我的服务是不使用.NET Framework,所以我本来希望写会有无需安装.NET安装服务的方式


奖金问题:我的服务输出到一个日志文件。是否有可能为它检查文件有多大,并删除它,如果它是在一个任意文件的大小?

PS我不知道为什么这个列表重置为1安培; 2 code后,上面清清楚楚地写着4和5中的编辑框。


解决方案

  1. 由于服务是全机而不是每个用户的配置文件存储下的 CSIDL_COMMON_APPDATA

  2. 的SHGetFolderPath 通过 CSIDL_COMMON_APPDATA 来找出该文件夹中。

  3. 命令行参数的argv 来自服务时登记指定的命令行。这就是你可以从服务控制管理器看到的。

我还没有解决第二套问题。你真的应该一次只问一个问题。我大概了解了一下,虽然安装服务。你当然不需要任何.NET。 precisely你应该怎么办,它可能取决于哪安装你正在使用的工具。

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.

解决方案

  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.

这篇关于窗口服务C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆