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 need 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?
You can use the GetModuleFileName function. See the Installing a Service example.
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.
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.