Working Directory in a NT Service

cero
cero used Ask the Experts™
on
Hi,

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??
Thanxs
cero
Comment
Watch Question

Do more with

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

char acMyLocation [ MAX_PATH];

GetModuleFileName ( NULL, acMyLocation, MAX_PATH);

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

Commented:
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

Author

Commented:
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
cero.
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