是否有可靠的API来获取Windows中的Windows文件夹?

sxi*_*eng -1 c++ windows api directory

在C++中使用Windows获取Windows文件夹是否有可靠的API?我使用以下方式,但它失败了.

    BOOL CQUserInfoHelper::GetWindowsPath(CString& strWindowsPath)
    {
        TCHAR windowsPathTemp[MAX_PATH];
        int nSize = MAX_PATH;
        ::GetWindowsDirectory(
            windowsPathTemp,
            nSize);
        strWindowsPath = windowsPathTemp;
        return TRUE;
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

试试这个 -

const DWORD dwBufferLength = 65537;
CStringW strBuffer;

if (!::GetCurrentDirectory(   dwBufferLength , 
                              strBuffer.GetBuffer(dwBufferLength))    ) 
   return L"";
Run Code Online (Sandbox Code Playgroud)

...

strBuffer.ReleaseBuffer();
Run Code Online (Sandbox Code Playgroud)