By default, Windows services start in the sytem32 directory (usually C:\WINDOWS\system32
).
Is there a way to set up a different working directory? I am thinking of some registry parameter beneath HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SomeService
.
So - can this be done?
You could use DLL injection to call
SetCurrentDirectory
after the process has already launched. This would require you to build an injector application, plus the DLL to inject. Some tutorials exist; probably the two best ones I've found are:You'll need a decent amount of C++ programming background (and a working build environment) to get through that.
However, this assumes that the service is looking at the current directory. Another possibility is that it's using
%path%
. You say that it "starts atsystem32
, tries a few more locations, and eventually its own directory", so this seems more likely to me.Compare the directories you see in
procmon
with your%path%
. If they're the same, consider modifying either theSYSTEM %path%
or the%path%
of the user running the service, so that the directory you want it to search is first.I believe Fred is right, though -- you're unlikely to see any significant performance benefit by doing any of this, unless it's happening very frequently. Simple file open operations are not particularly expensive, especially if it's a local path and the file doesn't actually exist.
Do this within the Service main function:
GetModuleFilename
. It will retrieve the module (the exe) filename including path, in the formC:\path\to\exe\your_service.exe
.std::string
functionfind_last_of()
), to find the last backslash. Strip/trim the string from there on to obtain the path to your module and therefore the directory of your exe.SetCurrentDirectory
and voila!Like MattB, I don't know of any way to change the service's working directory w/o access to the source code. For this specific scenario, it's likely that the extra directory checks don't impose that much unnecessary disk activity relative to the amount of i/o required for the full text indexing operation. Even if you could optimize them away, the full text index will be disk intensive by the nature of the beast.
Add an "AppDirectory" string value to the Parameters Key and set the value to your desired working directory.