Solved

Current  Directory

Posted on 1998-12-15
5
242 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
Comment Utility
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
Comment Utility
>>  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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now