Windows API一日一练 52 GetCurrentDirectory和SetCurrentDirectory函数

在开发软件里,常常碰到要读取当前目录下的配置参数文件,或者打开当前目录下别的程序来运行,那么就需要获取当前进程的目录位置,这就需要使用函数 GetCurrentDirectory 获取当前进程所有在的目录。同时也可以使用 SetCurrentDirectory 函数来改变进程的当前目录。
 
函数 GetCurrentDirectory SetCurrentDirectory 声明如下:
 
WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryA(
    __in DWORD nBufferLength,
    __out_ecount_part_opt(nBufferLength, return + 1) LPSTR lpBuffer
    );
WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryW(
    __in DWORD nBufferLength,
    __out_ecount_part_opt(nBufferLength, return + 1) LPWSTR lpBuffer
    );
#ifdef UNICODE
#define GetCurrentDirectory GetCurrentDirectoryW
#else
#define GetCurrentDirectory GetCurrentDirectoryA
#endif // !UNICODE
 
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryA(
    __in LPCSTR lpPathName
    );
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryW(
    __in LPCWSTR lpPathName
    );
#ifdef UNICODE
#define SetCurrentDirectory SetCurrentDirectoryW
#else
#define SetCurrentDirectory SetCurrentDirectoryA
#endif // !UNICODE
 
nBufferLength 是缓冲区的大小。
lpBuffer 是接收目录的缓冲区指针。
lpPathName 是设置的目录。
 
调用函数的例子如下:
#001  // 获取或者改变当前目录路径。
#002  // 蔡军生  2007/10/17 QQ:9073204 深圳
#003  void GetCurDir(void)
#004  {
#005         //
#006         TCHAR szBuf[MAX_PATH];
#007         ZeroMemory(szBuf,MAX_PATH);
#008         if (GetCurrentDirectory(MAX_PATH,szBuf) > 0)
#009         {
#010               // 获取进程目录成功。
#011               OutputDebugString(szBuf);
#012         }
#013         else
#014         {
#015               // 改变当前目录位置。
#016               SetCurrentDirectory(_T("C://"));
#017         }
#018 
#019         OutputDebugString(_T("/r/n"));          
#020  }
#021 
 
 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/skiwnchh/p/10347059.html