Tân Dương Đình Tân Dương Đình - 10 days ago 5
C++ Question

GetCurrentDirectory does not really return the path of the executable file

I am coding a program as service using c++, when I test it as a normal program, the function

GetCurrentDirectory
returns the correct path. But when I try to install my program as service,
GetCurrentDirectory
returns
C:\Windows\System32
instead of the path of the executable.

How can I get the path of my executable file in a way that will work for a service?

Answer

Working directory for Windows services is always %WINDIR%\System32.

To get directory, where your executable resides, simply call GetModuleFileName with NULL for hModule argument, and manually strip executable name.