当前位置: 代码迷 >> VC >> 怎么获取当前目录
  详细解决方案

怎么获取当前目录

热度:350   发布时间:2016-05-05 00:01:35.0
如何获取当前目录?
我的目的是,在Windows资源管理器的任意目录下,右键执行自己添加的命令,然后在当前目录建立若干个文件夹,现在的问题是,如何在右键事件中将当前目录传给我的程序。谢谢各位
------解决思路----------------------
GetCurrentDirectory Function

Retrieves the current directory for the current process.


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 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.

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 to GetFullPathName to retrieve the drive letter.

------解决思路----------------------
GetWorkDirectory,还可以从modle取应用程序目录
------解决思路----------------------
这个函数可以满足你的要求,DWORD WINAPI GetCurrentDirectory(
  __in          DWORD nBufferLength,
  __out         LPTSTR lpBuffer
);

放心使用吧!
  相关解决方案