使用文件I / O正确创建和运行win32服务 [英] Correctly creating and running a win32 service with file I/O

查看:156
本文介绍了使用文件I / O正确创建和运行win32服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已根据此代码示例编写了一个非常简单的服务应用程序。

I've written a very simple service application based on this code example.

作为其正常运行的一部分的应用程序假定在其找到的目录中或其执行路径中存在一个文件。

The application as part of its normal running assumes there exists a file in the directory it is found, or in its execution path.

当我'安装'服务,然后从控制面板中的服务管理器启动服务。应用程序失败,因为它找不到打开和读取的文件(即使该文件与安装的可执行文件在同一目录中)。

When I 'install' the service and then subsequently 'start' the service from the service manager in control panel. The application fails because it can't find the file to open and read from (even though the file is in the same directory as the installed executable).

我的问题是当一个windows服务运行时,这是预期的运行路径应该是?

My question is when a windows service is run, which is the expected running path supposed to be?

当调用CreateService时,似乎只有一个路径参数为二进制,而不是执行。有没有什么地方指示二进制应该从哪里执行?

When calling 'CreateService' there only seems to be a path parameter for the binary, not for execution. Is there someway to indicate where the binary should be executed from?

我试过这个在windows vista和windows 7.得到同样的问题。

I've tried this on windows vista and windows 7. Getting the same issues.

推荐答案

由于Windows服务是从与普通用户模式应用程序不同的上下文运行的,所以最好不要对工作目录做任何假设,相对路径。除了工作目录的差异,服务可以使用完全不同的权限集等运行。

Since Windows services are run from a different context than normal user-mode applications, it's best if you don't make any assumptions about working directories or relative paths. Aside from differences in working directories, a service could run using a completely different set of permissions, etc.

使用文件的绝对路径你的服务需要应该完全避免这个问题。绝对路径将被解释为相同,而不管工作目录,因此这应该使您的服务的工作目录不相关。有几种方法可以解决此问题:

Using an absolute path to the file that your service needs should avoid this problem entirely. Absolute paths will be interpreted the same regardless of the working directory, so this should make the working directory of your service irrelevant. There are several ways to go about this:


  1. 硬编码绝对路径 - 这可能是最简单的方式来避免的问题,但它也是最不灵活。这种方法对于基本的开发和测试工作可能是很好的,但是你可能希望在其他人开始使用你的程序之前更复杂一些。

  2. 将绝对路径存储在环境中变量 - 这为您提供了额外的灵活性,因为现在可以将路径设置为任意值,并根据需要进行更改。由于服务可以作为具有不同环境变量集的不同用户运行,因此这种方法仍然存在一些问题。

  3. 在注册表中存储绝对路径 - 这可能是最愚蠢的方法。从注册表中检索路径会为所有用户帐户产生相同的结果,并且这在安装时相对容易设置。

  1. Hard-code the absolute path - This is perhaps the easiest way to avoid the problem, however it's also the least flexible. This method is probably fine for basic development and testing work, but you probably want something a bit more sophisticated before other people start using your program.
  2. Store the absolute path in an environment variable - This gives you an extra layer of flexibility since the path can now be set to any arbitrary value and changed as needed. Since a service can run as a different user with a different set of environment variables, there are still some gotchas with this approach.
  3. Store an absolute path in the registry - This is probably the most fool-proof method. Retrieving the path from the registry will give you the same result for all user accounts, plus this is relatively easy to set up at install time.

这篇关于使用文件I / O正确创建和运行win32服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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