13 out of 21 rated this helpful - Rate this topic

GetCurrentDirectory function

Applies to: desktop apps only

Retrieves the current directory for the current process.

Syntax

DWORD WINAPI GetCurrentDirectory(
  __in   DWORD nBufferLength,
  __out  LPTSTR lpBuffer
);

Parameters

nBufferLength [in]

The length of the buffer for the current directory string, in TCHARs. The buffer length must include room for a terminating null character.

lpBuffer [out]

A pointer to the buffer that receives the current directory string. This null-terminated string specifies the absolute path to the current directory.

To determine the required buffer size, set this parameter to NULL and the nBufferLength parameter to 0.

Return value

If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If the buffer that is pointed to by lpBuffer is not large enough, the return value specifies the required size of the buffer, in characters, including the null-terminating character.

Remarks

Each process has a single current directory that consists of two parts:

  • A disk designator that is either a drive letter followed by a colon, or a server name followed by a share name (\\servername\sharename)
  • A directory on the disk designator

To set the current directory, use the SetCurrentDirectory function.

Multithreaded applications and shared library code should not use the GetCurrentDirectory function and should avoid using relative path names. The current directory state written by the SetCurrentDirectory function is stored as a global variable in each process, therefore multithreaded applications cannot reliably use this value without possible data corruption from other threads that may also be reading or setting this value. This limitation also applies to the SetCurrentDirectory and GetFullPathName functions. The exception being when the application is guaranteed to be running in a single thread, for example parsing file names from the command line argument string in the main thread prior to creating any additional threads. Using relative path names in multithreaded applications or shared library code can yield unpredictable results and is not supported.

Examples

For an example, see Changing the Current Directory.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

GetCurrentDirectoryW (Unicode) and GetCurrentDirectoryA (ANSI)

See also

CreateDirectory
Directory Management Functions
GetSystemDirectory
GetWindowsDirectory
RemoveDirectory
SetCurrentDirectory

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Docs on the return value are confusing
The docs for the return value seems confusing. It's not clear how to tell the difference between success (>0) and an error if the buffer is too small (also >0). I suggest the docs for the return value clarify this, eg: --- Return value If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character. This value will be /less than/ nBufferLength. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the buffer that is pointed to by lpBuffer is not large enough, the return value specifies the required size of the buffer, in characters, including the null-terminating character. This value will be /greater/ than nBufferLength, indicating this special error condition. ---- Examples could also be included to clarify proper usage; one example for how to properly dynamically allocate the buffer using the two-call method (preferred approach), and another showing how to work with a pre-allocated buffer. The dynamic example might be something like:seem to want to be something like: DWORD cwdsz = GetCurrentDirectory(0,0); // determine size needed char *cwd = (char*)malloc(cwdsz); if ( GetCurrentDirectory(cwdsz, cwd) == 0 ) { OS error.. } else { success.. } free((void*)cwd); ..and for a pre-allocated buffer (not recommended, but shows how to detect errors correctly): char cwd[SOME_VALUE]; DWORD cwdsz = sizeof(cwd); int ret = GetCurrentDirectory(cwdsz, cwd); if ( ret == 0 ) { OS error.. } else if ( ret > slen ) { buffer too small error.. } else { success.. }
intermediate dirs
all intermediate dis have to exist
if AAA doesn t exist AAA/BBB cannot be created
creating c:\AAA\BBB directory

surprisingly, the following code fail to create a directory:

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL; // this will be have to change in the future
::CreateDirectory("c:\\AAA\\BBB", &saAttr));

it will succide to create c:\AAA, but not c:\AAA\BBB

Do I have to implement it by my self , or do I miss something? suggesions?