GetCurrentDirectory Function

来源:互联网 发布:大学生java实训学校 编辑:程序博客网 时间:2024/06/07 06:19

Retrieves the current directory for the current process.检索应用程序的当前工作目录。

Syntax

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

Parameters 参数

nBufferLength

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

缓冲区的长度为当前工作目录,缓冲区的长度必须包括一个终止空字符

lpBuffer

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 thenBufferLength parameter to 0.

一个得到当前工作目录字符的缓冲区的指针。这个结尾的字符串确定绝对的路径到当前工作目录。为了确定要求的缓冲区的大小,设置这个参数为空并且nBufferLength参数为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, callGetLastError.

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.

    In certain rare cases, if the specified directory is on the current drive, the function might omit the drive letter and colon from the path. Therefore, the size that is returned by the function might be two characters less than the size of the specified string, not including the terminating null character. This behavior might occur in edge situations such as in a services application. If you need the drive letter, make a subsequent call toGetFullPathName to retrieve the drive letter.

    Example Code

    The directory at the end of the active path is called the current directory; it is the directory in which the active application started, unless explicitly changed. An application can determine which directory is current by calling theGetCurrentDirectory function.

    An application can change the current directory by calling the SetCurrentDirectory function.

    The following example demonstrates the use of GetCurrentDirectory andSetCurrentDirectory.

    #include <windows.h> #include <stdio.h>#include <tchar.h>#define BUFSIZE MAX_PATH void _tmain(int argc, TCHAR **argv) {    TCHAR Buffer[BUFSIZE];   DWORD dwRet;   if(argc != 2)   {      _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);      return;   }   dwRet = GetCurrentDirectory(BUFSIZE, Buffer);   if( dwRet == 0 )   {      printf("GetCurrentDirectory failed (%d)\n", GetLastError());      return;   }   if(dwRet > BUFSIZE)   {      printf("Buffer too small; need %d characters\n", dwRet);      return;   }   if( !SetCurrentDirectory(argv[1]))   {      printf("SetCurrentDirectory failed (%d)\n", GetLastError());      return;   }   _tprintf(TEXT("Set current directory to %s\n"), argv[1]);   if( !SetCurrentDirectory(Buffer) )   {      printf("SetCurrentDirectory failed (%d)\n", GetLastError());      return;   }   _tprintf(TEXT("Restored previous directory (%s)\n"), Buffer);}