Solved

Current  Directory

Posted on 1998-12-15
5
246 Views
Last Modified: 2010-08-05
Is there a string/function in VC++ 5
for getting the current directory that the app is running from?
0
Comment
Question by:atsui
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 30 total points
ID: 1180240
Yes. It is called 'GetCurrentDirectory()', the prototype is
DWORD GetCurrentDirectory( DWORD nBufferLength,
 // size, in characters, of directory buffer
 
LPTSTR lpBuffer
 // address of buffer for current directory
 
);
 



0
 
LVL 22

Expert Comment

by:nietod
ID: 1180241
>>  the current directory that the app is running from?
Those are two different things.  There is a current directory, that is the directory that is used by most commands when a file is specified without a path.   Seperate from that is the directory that the app is running from.  

Which do you want?  

jkr's answer is for the current directory, but if you want the directory that the app is running from use GetModuleFileName() and remove the name from the path returned.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1180242
That's right. 'GetCurrentDirectory()' retrieves the directory the application was started from of the one set by 'SetCurrentDirectory()' (both refer to the 'working directory'), whereas 'GetModuleFileName()' returns the path for the file used to create the calling process (maybe 8.3 format, use 'GetLongPathName()' to convert it).
0
 
LVL 1

Author Comment

by:atsui
ID: 1180243
So is this what I'm supposed to do to get the string?

      LPSTR curdir;
      GetCurrentDirectory(30,curdir);
      ShellExecute(NULL,"explore",*curdir,NULL,NULL,SW_SHOW)
0
 
LVL 86

Expert Comment

by:jkr
ID: 1180244
Ooops, no - as 'LPSTR curdir' isn't initialized, the results are unpredictable...
Better try

char curdir[ MAX_PATH];
GetCurrentDirectory(MAX_PATH,curdir);
ShellExecute(NULL,"explore",curdir,NULL,NULL,SW_SHOW);

NOTE that i removed '*' from the last line ...

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question