If longer than MAX_PATH, try to shorten it to reduce crash
authorSup Yut Sum <ch3cooli@gmail.com>
Fri, 19 Apr 2013 13:06:17 +0000 (19 21:06 +0800)
committerSup Yut Sum <ch3cooli@gmail.com>
Fri, 19 Apr 2013 13:06:17 +0000 (19 21:06 +0800)
Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com>
src/Utils/CommonAppUtils.cpp

index d585179..3697069 100644 (file)
@@ -239,7 +239,24 @@ bool CCommonAppUtils::FileOpenSave(CString& path, int * filterindex, UINT title,
        TCHAR szFile[MAX_PATH] = {0};           // buffer for file name. Explorer can't handle paths longer than MAX_PATH.\r
        ofn.lStructSize = sizeof(OPENFILENAME);\r
        ofn.hwndOwner = hwndOwner;\r
-       _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path);\r
+       if (path.GetLength() >= MAX_PATH)\r
+       {\r
+               CString dir = path;\r
+               while (true)\r
+               {\r
+                       int index = dir.ReverseFind(_T('\\'));\r
+                       if (index < 0)\r
+                               break;\r
+                       dir = dir.Left(index);\r
+                       if (PathFileExists(dir))\r
+                               break;\r
+               }\r
+               GetShortPathName(dir, szFile, MAX_PATH);\r
+               CString remain = path.Right(path.GetLength() - dir.GetLength());\r
+               _tcscat_s(szFile, MAX_PATH, remain);\r
+       }\r
+       else\r
+               _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path);\r
        ofn.lpstrFile = szFile;\r
        ofn.nMaxFile = _countof(szFile);\r
 \r