BOOL CPathUtils::MakeSureDirectoryPathExists(LPCTSTR path) { size_t len = _tcslen(path) + 10; std::unique_ptr<TCHAR[]> buf(new TCHAR[len]); std::unique_ptr<TCHAR[]> internalpathbuf(new TCHAR[len]); TCHAR * pPath = internalpathbuf.get(); SECURITY_ATTRIBUTES attribs; SecureZeroMemory(&attribs, sizeof(SECURITY_ATTRIBUTES)); attribs.nLength = sizeof(SECURITY_ATTRIBUTES); attribs.bInheritHandle = FALSE; ConvertToBackslash(internalpathbuf.get(), path, len); do { SecureZeroMemory(buf.get(), (len)*sizeof(TCHAR)); TCHAR * slashpos = _tcschr(pPath, '\\'); if (slashpos) _tcsncpy_s(buf.get(), len, internalpathbuf.get(), slashpos - internalpathbuf.get()); else _tcsncpy_s(buf.get(), len, internalpathbuf.get(), len); CreateDirectory(buf.get(), &attribs); pPath = _tcschr(pPath, '\\'); } while ((pPath++)&&(_tcschr(pPath, '\\'))); return CreateDirectory(internalpathbuf.get(), &attribs); }
BOOL CPathUtils::MakeSureDirectoryPathExists(LPCTSTR path) { const size_t len = wcslen(path); const size_t fullLen = len+10; std::unique_ptr<TCHAR[]> buf(new TCHAR[fullLen]); std::unique_ptr<TCHAR[]> internalpathbuf(new TCHAR[fullLen]); TCHAR * pPath = internalpathbuf.get(); SECURITY_ATTRIBUTES attribs; SecureZeroMemory(&attribs, sizeof(SECURITY_ATTRIBUTES)); attribs.nLength = sizeof(SECURITY_ATTRIBUTES); attribs.bInheritHandle = FALSE; ConvertToBackslash(internalpathbuf.get(), path, fullLen); if (wcsncmp(internalpathbuf.get(), L"\\\\?\\", 4) == 0) pPath += 4; do { SecureZeroMemory(buf.get(), fullLen*sizeof(TCHAR)); TCHAR * slashpos = wcschr(pPath, '\\'); if (slashpos) wcsncpy_s(buf.get(), fullLen, internalpathbuf.get(), slashpos - internalpathbuf.get()); else wcsncpy_s(buf.get(), fullLen, internalpathbuf.get(), fullLen); CreateDirectory(buf.get(), &attribs); pPath = wcschr(pPath, '\\'); } while ((pPath++)&&(wcschr(pPath, '\\'))); const BOOL bRet = CreateDirectory(internalpathbuf.get(), &attribs); return bRet; }