开发者

How can I get the installed directory for a C++ Windows Service?

I have a C++ Windows service, and I would like to access an executable in the same directory as the service's executable (via the system function). I'd imagine to do this I'll n开发者_运维知识库eed to find that directory, so that I can refer to the target executable's path. How can I find the directory in which the service is installed, in code?


As others have mentioned, GetModuleFileName() can do that job but YMMV. The most reliable way is to read this information from the registry key HKLM\System\CurrentControlSet\Services\%ServiceName%\ImagePath where %ServiceName% is a name of your service.

For an example of reading the registry using C++ check this out.

Good luck!


You can use the QueryServiceConfig() function. The path to the binary is stored in the lpBinaryPathName field of the QUERY_SERVICE_CONFIG structure.


Depends.

If it's from within the service you can always use GetModuleFileName, which produces a fully qualified path.

If it's from another program, then since it is service you can inspect the service's registry information.


You can use the GetModuleFileName function. See the Installing a Service example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜