Windows服务C

发布于 2024-12-09 03:11:48 字数 1404 浏览 1 评论 0原文

我已经用 C 语言编写了一个 Windows 服务,但是缺乏有关用这种语言编写服务的文档,这意味着我有一些事情需要帮助。

  1. 我想创建一个配置文件,服务读取该文件以获取一些设置/选项。我想知道执行此操作的最佳方法是什么以及如何执行。服务 .exe 将与其他文件/程序一起安装在用户指定的位置。我的第一个问题是我应该将此配置文件保留在与服务 .exe 相同的目录中还是应该将其放在为程序创建的 %appdata% 文件夹中?

  2. 服务如何找到配置文件。我应该将配置文件的位置存储在注册表中并将注册表项硬编码到服务中吗?或者是否可以安装该服务,以便将配置文件路径作为默认命令行参数?或者我应该将配置文件保存在与服务 .exe 相同的文件夹中并使用注册表 HKLM/System/CurrentControlSet/Services//ImagePath

  3. 我有以下代码:

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
}

ServiceMain 从哪里获取其命令行参数,是特定服务属性下的“启动参数:”框吗?是否可以使用一些默认参数安装它?是否可以稍后以编程方式添加一些?

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

  2. 如何在生产中安装此服务?我一直在使用 Windows sdk 中的 sc 进行开发,但这在生产中不起作用。执行此操作的唯一方法是确保安装了 microsoft .net Framework 然后运行 ​​installutil 程序吗?我的服务是在不使用 .net 框架的情况下编写的,因此我希望有一种方法可以在不安装 .net 的情况下安装该服务

额外问题:我的服务输出到日志文件。它是否可以检查文件有多大,如果超过任意文件大小则将其删除?

PS 我不知道为什么列表重置为 1 & 2在代码后面,编辑框中清楚地写着4和5。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-12-16 03:11:48
  1. 由于服务是计算机范围的而不是每个用户的,因此请将配置文件存储在 CSIDL_COMMON_APPDATA
  2. 调用 SHGetFolderPath 传递 CSIDL_COMMON_APPDATA 来查找该文件夹的位置。
  3. 命令行参数 argv 来自注册服务时指定的命令行。这是您可以从服务控制管理器中看到的内容。

我还没有回答第二组问题。您实际上一次应该只问一个问题。不过,我确实了解一些有关安装服务的知识。您当然不需要任何.net。具体如何执行可能取决于您使用的安装工具。

  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.

断肠人 2024-12-16 03:11:48

许多服务使用以下注册表子项来保存配置值:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\Parameters

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

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\Parameters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文