Working Directory in a NT Service

cero used Ask the Experts™

I don't know how to read the directory where the Exe File is located. Well, whenever I try to get the current directory, using GetCurrentDirectory or _getcwd, I get C:\WINNT\System32 but my file is located in another location. I read something about Get

Any idea??
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Use 'GetModuleFileName()', e.g.

char acMyLocation [ MAX_PATH];

GetModuleFileName ( NULL, acMyLocation, MAX_PATH);

The 'working directory' for services indeed is '%SystemRoot%\system32'

You have 2 options:

1) In your service, change the current directory to where you need it to be.  Usually you will use SetCurrentDirectory() to do this.

2) Access your file using an ABSOLUTE file name.  I.e. specify entire PATH to the file.
I used to use GetModuleFileName function:
char buf[MAX_PATH];
if (!::GetModuleFileName(0, buf, MAX_PATH))
// handle error

// buf contains the full path including file name
// trim file name and you will get the current directory


to jhance:

1) and 2) I'm doing that and I want to change that.

to jkr:
I will prove that.

to YuriPutivsky:
It's the same? it isn't?

thanks to all
Yes it does,
Sorry "jkr" I didn't refresh IE screen before sending comment and didn't see your comment...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial