GetCurrentDirectory function

本文详细介绍了Windows API函数GetCurrentDirectory的使用方法,包括其语法、参数说明、返回值及注意事项等。此外,还提供了示例代码帮助理解如何调用此API获取进程的当前工作目录。

Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx

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.

 

Other Example:

	//Get the correct path.
	DWORD dwLen=MAX_PATH;
	TCHAR szBuf[MAX_PATH+1];
	GetCurrentDirectory(dwLen, szBuf);


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值