static nsresult GetShellFolderPath(int folder, nsAString& _retval) { wchar_t* buf; uint32_t bufLength = _retval.GetMutableData(&buf, MAXPATHLEN + 3); NS_ENSURE_TRUE(bufLength >= (MAXPATHLEN + 3), NS_ERROR_OUT_OF_MEMORY); nsresult rv = NS_OK; LPITEMIDLIST pItemIDList = nullptr; if (SUCCEEDED(SHGetSpecialFolderLocation(nullptr, folder, &pItemIDList)) && SHGetPathFromIDListW(pItemIDList, buf)) { // We're going to use wcslen (wcsnlen not available in msvc7.1) so make // sure to null terminate. buf[bufLength - 1] = L'\0'; _retval.SetLength(wcslen(buf)); } else { _retval.SetLength(0); rv = NS_ERROR_NOT_AVAILABLE; } CoTaskMemFree(pItemIDList); return rv; }
// Like GetMutableData, but returns false if it can't // allocate enough memory (e.g. due to OOM) rather than // returning zero (which could have other meanings) and // throws away the out-param pointer. bool SetLengthForWriting(nsAString& aDest, PRUint32 aDesiredLength) { PRUnichar* dummy; PRUint32 len = aDest.GetMutableData(&dummy, aDesiredLength); return (len >= aDesiredLength); }
static nsresult GetShellFolderPath(int folder, nsAString& _retval) { PRUnichar* buf; PRUint32 bufLength = _retval.GetMutableData(&buf, MAXPATHLEN + 3); NS_ENSURE_TRUE(bufLength >= (MAXPATHLEN + 3), NS_ERROR_OUT_OF_MEMORY); nsresult rv = NS_OK; #if defined(WINCE) && !defined(WINCE_WINDOWS_MOBILE) if (folder == CSIDL_APPDATA || folder == CSIDL_LOCAL_APPDATA) folder = CSIDL_PROFILE; BOOL ok = SHGetSpecialFolderPath(NULL, buf, folder, true); if (!ok) { _retval.SetLength(0); return NS_ERROR_FAILURE; } buf[bufLength - 1] = L'\0'; _retval.SetLength(wcslen(buf)); // sometimes CSIDL_PROFILE shows up without a root slash if (folder == CSIDL_PROFILE && buf[0] != '\\') { _retval.Insert('\\', 0); } #else LPITEMIDLIST pItemIDList = NULL; if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, folder, &pItemIDList)) && SHGetPathFromIDListW(pItemIDList, buf)) { // We're going to use wcslen (wcsnlen not available in msvc7.1) so make // sure to null terminate. buf[bufLength - 1] = L'\0'; _retval.SetLength(wcslen(buf)); } else { _retval.SetLength(0); rv = NS_ERROR_NOT_AVAILABLE; } CoTaskMemFree(pItemIDList); #endif return rv; }