diff --git a/src/DSUtil/FileHandle.cpp b/src/DSUtil/FileHandle.cpp index 1e6088784..2eaee39dc 100644 --- a/src/DSUtil/FileHandle.cpp +++ b/src/DSUtil/FileHandle.cpp @@ -159,15 +159,14 @@ CStringW GetCombineFilePath(LPCWSTR dir, LPCWSTR file) CStringW GetCanonicalizeFilePath(LPCWSTR path) { CStringW newPath; - DWORD buflen = ::GetFullPathNameW(path, 0, nullptr, nullptr); + const DWORD buflen = ::GetFullPathNameW(path, 0, nullptr, nullptr); if (buflen > 0) { DWORD len = ::GetFullPathNameW(path, buflen, newPath.GetBuffer(buflen - 1), nullptr); - if (len > buflen) { + if (len >= buflen) { len = 0; } newPath.ReleaseBufferSetLength(len); } - return newPath; } @@ -186,6 +185,20 @@ CStringW GetStripToRoot(LPCWSTR path) return newPath; } +CStringW GetCurrentDir() +{ + CStringW curDir; + const DWORD buflen = ::GetCurrentDirectoryW(0, nullptr); + if (buflen > 0) { + DWORD len = ::GetCurrentDirectoryW(buflen, curDir.GetBuffer(buflen - 1)); + if (len >= buflen) { + len = 0; + } + curDir.ReleaseBufferSetLength(len); + } + return curDir; +} + // // Generate temporary files with any extension // diff --git a/src/DSUtil/FileHandle.h b/src/DSUtil/FileHandle.h index 0536cba6e..13245ab73 100644 --- a/src/DSUtil/FileHandle.h +++ b/src/DSUtil/FileHandle.h @@ -54,6 +54,8 @@ CStringW GetCanonicalizeFilePath(LPCWSTR path); void StripToRoot(CStringW& path); CStringW GetStripToRoot(LPCWSTR path); +CStringW GetCurrentDir(); + BOOL GetTemporaryFilePath(CStringW strExtension, CStringW& strFileName); CStringW CompactPath(LPCWSTR Path, UINT cchMax); diff --git a/src/apps/mplayerc/MainFrm.cpp b/src/apps/mplayerc/MainFrm.cpp index 50a30fd66..f1e2e688b 100644 --- a/src/apps/mplayerc/MainFrm.cpp +++ b/src/apps/mplayerc/MainFrm.cpp @@ -5534,8 +5534,7 @@ LRESULT CMainFrame::HandleCmdLine(WPARAM wParam, LPARAM lParam) if (!s.slFiles.empty()) { GetCDROMType(s.slFiles.front()[0], sl); } else { - CString dir; - dir.ReleaseBufferSetLength(GetCurrentDirectoryW(MAX_PATH, dir.GetBuffer(MAX_PATH))); + CString dir = GetCurrentDir(); GetCDROMType(dir[0], sl); for (WCHAR drive = 'C'; sl.empty() && drive <= 'Z'; drive++) { @@ -12397,16 +12396,19 @@ CString CMainFrame::OpenFile(OpenFileData* pOFD) CorrectAceStream(fn); if (SUCCEEDED(hr)) { - WCHAR path[MAX_PATH] = { 0 }; - BOOL bIsDirSet = FALSE; - if (!::PathIsURLW(fn) && ::GetCurrentDirectoryW(MAX_PATH, path)) { - bIsDirSet = ::SetCurrentDirectoryW(GetFolderPath(fn)); + CStringW oldcurdir; + BOOL bIsDirSet = FALSE; + if (!::PathIsURLW(fn)) { + oldcurdir = GetCurrentDir(); + if (oldcurdir.GetLength()) { + bIsDirSet = ::SetCurrentDirectoryW(GetFolderPath(fn)); + } } hr = m_pGB->RenderFile(fn, nullptr); if (bIsDirSet) { - ::SetCurrentDirectoryW(path); + ::SetCurrentDirectoryW(oldcurdir); } } @@ -12540,10 +12542,13 @@ CString CMainFrame::OpenFile(OpenFileData* pOFD) CorrectAceStream(fn); if (SUCCEEDED(hr)) { - WCHAR path[MAX_PATH] = { 0 }; - BOOL bIsDirSet = FALSE; - if (!::PathIsURLW(fn) && ::GetCurrentDirectoryW(MAX_PATH, path)) { - bIsDirSet = ::SetCurrentDirectoryW(GetFolderPath(fn)); + CStringW oldcurdir; + BOOL bIsDirSet = FALSE; + if (!::PathIsURLW(fn)) { + oldcurdir = GetCurrentDir(); + if (oldcurdir.GetLength()) { + bIsDirSet = ::SetCurrentDirectoryW(GetFolderPath(fn)); + } } if (CComQIPtr pGBA = m_pGB.p) { @@ -12551,7 +12556,7 @@ CString CMainFrame::OpenFile(OpenFileData* pOFD) } if (bIsDirSet) { - ::SetCurrentDirectoryW(path); + ::SetCurrentDirectoryW(oldcurdir); } } } diff --git a/src/apps/mplayerc/PlayerCaptureDialog.cpp b/src/apps/mplayerc/PlayerCaptureDialog.cpp index f5e15f0bb..10d5a37fc 100644 --- a/src/apps/mplayerc/PlayerCaptureDialog.cpp +++ b/src/apps/mplayerc/PlayerCaptureDialog.cpp @@ -1,6 +1,6 @@ /* * (C) 2003-2006 Gabest - * (C) 2006-2023 see Authors.txt + * (C) 2006-2024 see Authors.txt * * This file is part of MPC-BE. * @@ -649,7 +649,7 @@ void CPlayerCaptureDialog::InitControls() } } else { // Use current directory - m_file.ReleaseBufferSetLength(GetCurrentDirectoryW(MAX_PATH, dir.GetBuffer(MAX_PATH))); + m_file = GetCurrentDir(); } CoTaskMemFree(pathVideos);