开发者

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

开发者 https://www.devze.com 2023-01-20 10:54 出处:网络
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

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

精彩评论

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