From 58ceffe786ca774983931fa51a2f08b3fef02ffb Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sat, 27 Mar 2021 18:56:07 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=AB=E3=83=80?= =?UTF-8?q?=E9=81=B8=E6=8A=9E=E3=83=80=E3=82=A4=E3=82=A2=E3=83=AD=E3=82=B0?= =?UTF-8?q?=E3=82=92=20SHBrowseForFolder()=20=E3=81=A7=E3=81=AA=E3=81=8F?= =?UTF-8?q?=20IFileDialog=20=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/util/shell.cpp | 114 +++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index e6f1468148..746fdc1964 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -38,63 +38,81 @@ #include "env/CShareData.h" #include "env/DLLSHAREDATA.h" #include "extmodule/CHtmlHelp.h" +#include -int CALLBACK MYBrowseCallbackProc( - HWND hwnd, - UINT uMsg, - LPARAM lParam, - LPARAM lpData -) +/* フォルダ選択ダイアログ */ +BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName) { - switch( uMsg ){ - case BFFM_INITIALIZED: - ::SendMessage( hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpData ); - break; - case BFFM_SELCHANGED: - break; + using namespace Microsoft::WRL; + + ComPtr pDialog; + HRESULT hres; + + // インスタンスを作成 + hres = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog)); + if (FAILED(hres)) { + return FALSE; } - return 0; -} -/* フォルダ選択ダイアログ */ -BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName ) -{ - BOOL bRes; - WCHAR szInitFolder[MAX_PATH]; + // デフォルト設定を取得 + DWORD dwOptions = 0; + hres = pDialog->GetOptions(&dwOptions); + if (FAILED(hres)) { + return FALSE; + } - wcscpy( szInitFolder, pszInitFolder ); - /* フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない*/ - CutLastYenFromDirectoryPath( szInitFolder ); + // オプションをフォルダを選択可能に変更 + hres = pDialog->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM); + if (FAILED(hres)) { + return FALSE; + } - // 2010.08.28 フォルダを開くとフックも含めて色々DLLが読み込まれるので移動 - CCurrentDirectoryBackupPoint dirBack; - ChangeCurrentDirectoryToExeDir(); - // SHBrowseForFolder()関数に渡す構造体 - BROWSEINFO bi; - bi.hwndOwner = hWnd; - bi.pidlRoot = NULL; - bi.pszDisplayName = strFolderName; - bi.lpszTitle = pszTitle; - bi.ulFlags = BIF_RETURNONLYFSDIRS/* | BIF_EDITBOX*//* | BIF_STATUSTEXT*/; - bi.lpfn = MYBrowseCallbackProc; - bi.lParam = (LPARAM)szInitFolder; - bi.iImage = 0; - // アイテムIDリストを返す - // ITEMIDLISTはアイテムの一意を表す構造体 - LPITEMIDLIST pList = ::SHBrowseForFolder(&bi); - if( NULL != pList ){ - // SHGetPathFromIDList()関数はアイテムIDリストの物理パスを探してくれる - bRes = ::SHGetPathFromIDList( pList, strFolderName ); - // :SHBrowseForFolder()で取得したアイテムIDリストを削除 - ::CoTaskMemFree( pList ); - if( bRes ){ - return TRUE; - }else{ - return FALSE; + { + WCHAR szInitFolder[MAX_PATH]; + + wcscpy_s(szInitFolder, _countof(szInitFolder), pszInitFolder); + // フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない + + CutLastYenFromDirectoryPath(szInitFolder); + + // 初期フォルダを設定 + ComPtr psiFolder; + hres = SHCreateItemFromParsingName(szInitFolder, NULL, IID_PPV_ARGS(&psiFolder)); + if (SUCCEEDED(hres)) { + hres = pDialog->SetFolder(psiFolder.Get()); } } - return FALSE; + + // タイトル文字列を設定 + hres = pDialog->SetTitle(pszTitle); + if (FAILED(hres)) { + return FALSE; + } + + // フォルダ選択ダイアログを表示 + hres = pDialog->Show(hWnd); + if (FAILED(hres)) { + return FALSE; + } + + // 選択結果を取得 + ComPtr psiResult; + hres = pDialog->GetResult(&psiResult); + if (FAILED(hres)) { + return FALSE; + } + + PWSTR pszResult; + hres = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszResult); + if (FAILED(hres)) { + return FALSE; + } + + wcscpy(strFolderName, pszResult); + CoTaskMemFree(pszResult); + + return TRUE; } /*! 特殊フォルダのパスを取得する From 32e6f05649934df521898c44164492d686267b6b Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sat, 27 Mar 2021 23:02:29 +0900 Subject: [PATCH 2/8] =?UTF-8?q?SonarCloud=E6=8C=87=E6=91=98=E3=81=AE?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/dlg/CDlgExec.cpp | 2 +- sakura_core/dlg/CDlgGrep.cpp | 2 +- sakura_core/dlg/CDlgPluginOption.cpp | 2 +- sakura_core/dlg/CDlgTagsMake.cpp | 2 +- sakura_core/macro/CMacro.cpp | 2 +- sakura_core/outline/CDlgFileTree.cpp | 2 +- sakura_core/prop/CPropComBackup.cpp | 2 +- sakura_core/prop/CPropComEdit.cpp | 2 +- sakura_core/prop/CPropComHelper.cpp | 2 +- sakura_core/prop/CPropComMacro.cpp | 2 +- sakura_core/util/shell.cpp | 77 ++++++++++++++++------------ sakura_core/util/shell.h | 2 +- 12 files changed, 54 insertions(+), 45 deletions(-) diff --git a/sakura_core/dlg/CDlgExec.cpp b/sakura_core/dlg/CDlgExec.cpp index ecf8be4016..b17c1c491c 100644 --- a/sakura_core/dlg/CDlgExec.cpp +++ b/sakura_core/dlg/CDlgExec.cpp @@ -259,7 +259,7 @@ BOOL CDlgExec::OnBnClicked( int wID ) case IDC_BUTTON_REFERENCE2: { - if( SelectDir( GetHwnd(), LS(STR_DLGEXEC_SELECT_CURDIR), &m_szCurDir[0], &m_szCurDir[0] ) ){ + if( SelectDir( GetHwnd(), LS(STR_DLGEXEC_SELECT_CURDIR), &m_szCurDir[0], &m_szCurDir[0], m_szCurDir.GetBufferCount() ) ){ ::DlgItem_SetText( GetHwnd(), IDC_COMBO_CUR_DIR, &m_szCurDir[0] ); } } diff --git a/sakura_core/dlg/CDlgGrep.cpp b/sakura_core/dlg/CDlgGrep.cpp index f79c088dd6..dddf5f5478 100644 --- a/sakura_core/dlg/CDlgGrep.cpp +++ b/sakura_core/dlg/CDlgGrep.cpp @@ -509,7 +509,7 @@ BOOL CDlgGrep::OnBnClicked( int wID ) if( szFolder[0] == L'\0' ){ ::GetCurrentDirectory( nMaxPath, szFolder ); } - if( SelectDir( GetHwnd(), LS(STR_DLGGREP1), szFolder, szFolder ) ){ + if( SelectDir( GetHwnd(), LS(STR_DLGGREP1), szFolder, szFolder, _countof(szFolder) ) ){ SetGrepFolder( GetItemHwnd(IDC_COMBO_FOLDER), szFolder ); } } diff --git a/sakura_core/dlg/CDlgPluginOption.cpp b/sakura_core/dlg/CDlgPluginOption.cpp index 11e2c79219..42418c4f04 100644 --- a/sakura_core/dlg/CDlgPluginOption.cpp +++ b/sakura_core/dlg/CDlgPluginOption.cpp @@ -730,7 +730,7 @@ void CDlgPluginOption::SelectDirectory( int iLine ) WCHAR sTitle[MAX_LENGTH_VALUE+10]; auto_sprintf( sTitle, LS(STR_DLGPLUGINOPT_SELECT), buf); - if (SelectDir( GetHwnd(), (const WCHAR*)sTitle /*L"ディレクトリの選択"*/, szDir, szDir )) { + if (SelectDir( GetHwnd(), (const WCHAR*)sTitle /*L"ディレクトリの選択"*/, szDir, szDir, _countof(szDir) )) { // 末尾に\マークを追加する. AddLastChar( szDir, _countof(szDir), L'\\' ); ::DlgItem_SetText( GetHwnd(), IDC_EDIT_PLUGIN_OPTION_DIR, szDir ); diff --git a/sakura_core/dlg/CDlgTagsMake.cpp b/sakura_core/dlg/CDlgTagsMake.cpp index 06a7e62d81..40fe075b1e 100644 --- a/sakura_core/dlg/CDlgTagsMake.cpp +++ b/sakura_core/dlg/CDlgTagsMake.cpp @@ -125,7 +125,7 @@ void CDlgTagsMake::SelectFolder( HWND hwndDlg ) /* フォルダ */ ::DlgItem_GetText( hwndDlg, IDC_EDIT_TAG_MAKE_FOLDER, szPath, _MAX_PATH ); - if( SelectDir( hwndDlg, LS(STR_DLGTAGMAK_SELECTDIR), szPath, szPath ) ) + if( SelectDir( hwndDlg, LS(STR_DLGTAGMAK_SELECTDIR), szPath, szPath, _countof(szPath) ) ) { //末尾に\\マークを追加する. ::PathAddBackslashW( szPath ); diff --git a/sakura_core/macro/CMacro.cpp b/sakura_core/macro/CMacro.cpp index 3e822a1f35..bb47eaf95d 100644 --- a/sakura_core/macro/CMacro.cpp +++ b/sakura_core/macro/CMacro.cpp @@ -1939,7 +1939,7 @@ bool CMacro::HandleFunction(CEditView *View, EFunctionCode ID, const VARIANT *Ar } WCHAR szPath[ _MAX_PATH ]; - int nRet = SelectDir( View->GetHwnd(), sMessage.c_str(), sDefault.c_str(), szPath ); + int nRet = SelectDir( View->GetHwnd(), sMessage.c_str(), sDefault.c_str(), szPath, _countof(szPath) ); if( nRet == IDOK ){ SysString S( szPath, wcslen(szPath) ); Wrap( &Result )->Receive( S ); diff --git a/sakura_core/outline/CDlgFileTree.cpp b/sakura_core/outline/CDlgFileTree.cpp index a72a138492..7b0a18c5eb 100644 --- a/sakura_core/outline/CDlgFileTree.cpp +++ b/sakura_core/outline/CDlgFileTree.cpp @@ -537,7 +537,7 @@ BOOL CDlgFileTree::OnBnClicked( int wID ) // RADIO_GREP == folder WCHAR szDir[MAX_PATH]; DlgItem_GetText(GetHwnd(), IDC_EDIT_PATH, szDir, _countof(szDir) ); - if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir) ){ + if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir, _countof(szDir) ) ){ DlgItem_SetText(GetHwnd(), IDC_EDIT_PATH, szDir ); } }else{ diff --git a/sakura_core/prop/CPropComBackup.cpp b/sakura_core/prop/CPropComBackup.cpp index b2a33d2d5d..09529c121c 100644 --- a/sakura_core/prop/CPropComBackup.cpp +++ b/sakura_core/prop/CPropComBackup.cpp @@ -174,7 +174,7 @@ INT_PTR CPropBackup::DispatchEvent( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR WCHAR szFolder[_MAX_PATH]; ::DlgItem_GetText( hwndDlg, IDC_EDIT_BACKUPFOLDER, szFolder, _countof( szFolder )); - if( SelectDir( hwndDlg, LS(STR_PROPCOMBK_SEL_FOLDER), szFolder, szFolder ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMBK_SEL_FOLDER), szFolder, szFolder, _countof( szFolder ) ) ){ wcscpy( m_Common.m_sBackup.m_szBackUpFolder, szFolder ); ::DlgItem_SetText( hwndDlg, IDC_EDIT_BACKUPFOLDER, m_Common.m_sBackup.m_szBackUpFolder ); } diff --git a/sakura_core/prop/CPropComEdit.cpp b/sakura_core/prop/CPropComEdit.cpp index c70bea0df4..4693714f55 100644 --- a/sakura_core/prop/CPropComEdit.cpp +++ b/sakura_core/prop/CPropComEdit.cpp @@ -118,7 +118,7 @@ INT_PTR CPropEdit::DispatchEvent( WCHAR szPath[_MAX_PATH]; ::DlgItem_GetText( hwndDlg, IDC_EDIT_FILEOPENDIR, szMetaPath, _countof(szMetaPath) ); CFileNameManager::ExpandMetaToFolder( szMetaPath, szPath, _countof(szPath) ); - if( SelectDir( hwndDlg, LS(STR_PROPEDIT_SELECT_DIR), szPath, szPath ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPEDIT_SELECT_DIR), szPath, szPath, _countof(szPath) ) ){ CNativeW cmem(szPath); cmem.Replace(L"%", L"%%"); ::DlgItem_SetText( hwndDlg, IDC_EDIT_FILEOPENDIR, cmem.GetStringPtr() ); diff --git a/sakura_core/prop/CPropComHelper.cpp b/sakura_core/prop/CPropComHelper.cpp index 9eed90c925..4795daeb79 100644 --- a/sakura_core/prop/CPropComHelper.cpp +++ b/sakura_core/prop/CPropComHelper.cpp @@ -168,7 +168,7 @@ INT_PTR CPropHelper::DispatchEvent( }else{ wcscpy( szPath, m_Common.m_sHelper.m_szMigemoDict ); } - if( SelectDir( hwndDlg, LS(STR_PROPCOMHELP_MIGEMODIR), szPath, szPath ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMHELP_MIGEMODIR), szPath, szPath, _countof(szPath) ) ){ wcscpy( m_Common.m_sHelper.m_szMigemoDict, GetRelPath(szPath) ); // 2015.03.03 可能なら相対パスにする ::DlgItem_SetText( hwndDlg, IDC_EDIT_MIGEMO_DICT, m_Common.m_sHelper.m_szMigemoDict ); } diff --git a/sakura_core/prop/CPropComMacro.cpp b/sakura_core/prop/CPropComMacro.cpp index ecb1828c49..13a3136cb7 100644 --- a/sakura_core/prop/CPropComMacro.cpp +++ b/sakura_core/prop/CPropComMacro.cpp @@ -581,7 +581,7 @@ void CPropMacro::SelectBaseDir_Macro( HWND hwndDlg ) GetInidirOrExedir( szDir, folder ); } - if( SelectDir( hwndDlg, LS(STR_PROPCOMMACR_SEL_DIR), szDir, szDir ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMMACR_SEL_DIR), szDir, szDir, _countof(szDir) ) ){ // 末尾に\\マークを追加する. AddLastChar( szDir, _countof(szDir), L'\\' ); ::DlgItem_SetText( hwndDlg, IDC_MACRODIR, GetRelPath(szDir) ); // 2015.03.03 可能なら相対パスにする diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index 746fdc1964..c4d0d7007a 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -40,77 +40,86 @@ #include "extmodule/CHtmlHelp.h" #include + +/*! + @brief IFileDialog の初期フォルダを設定する + + @param [in] pDialog 設定対象のダイアログ + @param [in] pszInitFolder 初期フォルダに設定したいパス +*/ +static void SetInitialDir( Microsoft::WRL::ComPtr pDialog, const WCHAR* pszInitFolder ) +{ + + WCHAR szInitFolder[MAX_PATH]; + wcscpy_s( szInitFolder, _countof(szInitFolder), pszInitFolder ); + + // フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない + CutLastYenFromDirectoryPath( szInitFolder ); + + // 初期フォルダを設定 + Microsoft::WRL::ComPtr psiFolder; + HRESULT hres = SHCreateItemFromParsingName( szInitFolder, nullptr, IID_PPV_ARGS(&psiFolder) ); + if ( SUCCEEDED(hres) ) { + pDialog->SetFolder( psiFolder.Get() ); + } +} + /* フォルダ選択ダイアログ */ -BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName) +BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName, size_t nMaxCount ) { using namespace Microsoft::WRL; - ComPtr pDialog; HRESULT hres; // インスタンスを作成 - hres = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog)); - if (FAILED(hres)) { + hres = CoCreateInstance( CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog) ); + if ( FAILED(hres) ) { return FALSE; } // デフォルト設定を取得 DWORD dwOptions = 0; - hres = pDialog->GetOptions(&dwOptions); - if (FAILED(hres)) { + hres = pDialog->GetOptions( &dwOptions ); + if ( FAILED(hres) ) { return FALSE; } // オプションをフォルダを選択可能に変更 - hres = pDialog->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM); - if (FAILED(hres)) { + hres = pDialog->SetOptions( dwOptions | FOS_PICKFOLDERS | FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM ); + if ( FAILED(hres) ) { return FALSE; } - - { - WCHAR szInitFolder[MAX_PATH]; - - wcscpy_s(szInitFolder, _countof(szInitFolder), pszInitFolder); - // フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない - - CutLastYenFromDirectoryPath(szInitFolder); - - // 初期フォルダを設定 - ComPtr psiFolder; - hres = SHCreateItemFromParsingName(szInitFolder, NULL, IID_PPV_ARGS(&psiFolder)); - if (SUCCEEDED(hres)) { - hres = pDialog->SetFolder(psiFolder.Get()); - } - } + // 初期フォルダを設定 + SetInitialDir( pDialog, pszInitFolder ); // タイトル文字列を設定 - hres = pDialog->SetTitle(pszTitle); - if (FAILED(hres)) { + hres = pDialog->SetTitle( pszTitle ); + if ( FAILED(hres) ) { return FALSE; } // フォルダ選択ダイアログを表示 - hres = pDialog->Show(hWnd); - if (FAILED(hres)) { + hres = pDialog->Show( hWnd ); + if ( FAILED(hres) ) { return FALSE; } // 選択結果を取得 ComPtr psiResult; - hres = pDialog->GetResult(&psiResult); - if (FAILED(hres)) { + hres = pDialog->GetResult( &psiResult ); + if ( FAILED(hres) ) { return FALSE; } PWSTR pszResult; - hres = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszResult); - if (FAILED(hres)) { + hres = psiResult->GetDisplayName( SIGDN_FILESYSPATH, &pszResult ); + if ( FAILED(hres) ) { return FALSE; } - wcscpy(strFolderName, pszResult); - CoTaskMemFree(pszResult); + wcscpy_s( strFolderName, nMaxCount, pszResult ); + CoTaskMemFree( pszResult ); return TRUE; } diff --git a/sakura_core/util/shell.h b/sakura_core/util/shell.h index 1a496e6b59..9a407d5a3c 100644 --- a/sakura_core/util/shell.h +++ b/sakura_core/util/shell.h @@ -32,7 +32,7 @@ BOOL MyWinHelp(HWND hwndCaller, UINT uCommand, DWORD_PTR dwData); /* WinHelp のかわりに HtmlHelp を呼び出す */ // 2006.07.22 ryoji /* Shell Interface系(?) */ -BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName ); /* フォルダ選択ダイアログ */ +BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName, size_t nMaxCount ); /* フォルダ選択ダイアログ */ BOOL ResolveShortcutLink(HWND hwnd, LPCWSTR lpszLinkFile, LPWSTR lpszPath);/* ショートカット(.lnk)の解決 */ HWND OpenHtmlHelp( HWND hWnd, LPCWSTR szFile, UINT uCmd, DWORD_PTR data,bool msgflag = true); From 5dd13bf601f2ec76b09a73990745f5c695c881bc Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Fri, 2 Apr 2021 19:44:21 +0900 Subject: [PATCH 3/8] =?UTF-8?q?SelectDir()=20=E3=81=AB=E3=83=86=E3=83=B3?= =?UTF-8?q?=E3=83=97=E3=83=AC=E3=83=BC=E3=83=88=E5=BC=95=E6=95=B0=E3=81=A7?= =?UTF-8?q?=E5=9B=BA=E5=AE=9A=E9=95=B7=E9=85=8D=E5=88=97=E3=81=AE=E8=A6=81?= =?UTF-8?q?=E7=B4=A0=E6=95=B0=E3=82=92=E5=8F=96=E3=82=8B=E3=82=AA=E3=83=BC?= =?UTF-8?q?=E3=83=90=E3=83=BC=E3=83=AD=E3=83=BC=E3=83=89=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/dlg/CDlgGrep.cpp | 2 +- sakura_core/dlg/CDlgPluginOption.cpp | 2 +- sakura_core/dlg/CDlgTagsMake.cpp | 2 +- sakura_core/macro/CMacro.cpp | 2 +- sakura_core/outline/CDlgFileTree.cpp | 2 +- sakura_core/prop/CPropComBackup.cpp | 2 +- sakura_core/prop/CPropComEdit.cpp | 2 +- sakura_core/prop/CPropComHelper.cpp | 2 +- sakura_core/prop/CPropComMacro.cpp | 2 +- sakura_core/util/shell.h | 7 +++++++ 10 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sakura_core/dlg/CDlgGrep.cpp b/sakura_core/dlg/CDlgGrep.cpp index dddf5f5478..f79c088dd6 100644 --- a/sakura_core/dlg/CDlgGrep.cpp +++ b/sakura_core/dlg/CDlgGrep.cpp @@ -509,7 +509,7 @@ BOOL CDlgGrep::OnBnClicked( int wID ) if( szFolder[0] == L'\0' ){ ::GetCurrentDirectory( nMaxPath, szFolder ); } - if( SelectDir( GetHwnd(), LS(STR_DLGGREP1), szFolder, szFolder, _countof(szFolder) ) ){ + if( SelectDir( GetHwnd(), LS(STR_DLGGREP1), szFolder, szFolder ) ){ SetGrepFolder( GetItemHwnd(IDC_COMBO_FOLDER), szFolder ); } } diff --git a/sakura_core/dlg/CDlgPluginOption.cpp b/sakura_core/dlg/CDlgPluginOption.cpp index 42418c4f04..11e2c79219 100644 --- a/sakura_core/dlg/CDlgPluginOption.cpp +++ b/sakura_core/dlg/CDlgPluginOption.cpp @@ -730,7 +730,7 @@ void CDlgPluginOption::SelectDirectory( int iLine ) WCHAR sTitle[MAX_LENGTH_VALUE+10]; auto_sprintf( sTitle, LS(STR_DLGPLUGINOPT_SELECT), buf); - if (SelectDir( GetHwnd(), (const WCHAR*)sTitle /*L"ディレクトリの選択"*/, szDir, szDir, _countof(szDir) )) { + if (SelectDir( GetHwnd(), (const WCHAR*)sTitle /*L"ディレクトリの選択"*/, szDir, szDir )) { // 末尾に\マークを追加する. AddLastChar( szDir, _countof(szDir), L'\\' ); ::DlgItem_SetText( GetHwnd(), IDC_EDIT_PLUGIN_OPTION_DIR, szDir ); diff --git a/sakura_core/dlg/CDlgTagsMake.cpp b/sakura_core/dlg/CDlgTagsMake.cpp index 40fe075b1e..06a7e62d81 100644 --- a/sakura_core/dlg/CDlgTagsMake.cpp +++ b/sakura_core/dlg/CDlgTagsMake.cpp @@ -125,7 +125,7 @@ void CDlgTagsMake::SelectFolder( HWND hwndDlg ) /* フォルダ */ ::DlgItem_GetText( hwndDlg, IDC_EDIT_TAG_MAKE_FOLDER, szPath, _MAX_PATH ); - if( SelectDir( hwndDlg, LS(STR_DLGTAGMAK_SELECTDIR), szPath, szPath, _countof(szPath) ) ) + if( SelectDir( hwndDlg, LS(STR_DLGTAGMAK_SELECTDIR), szPath, szPath ) ) { //末尾に\\マークを追加する. ::PathAddBackslashW( szPath ); diff --git a/sakura_core/macro/CMacro.cpp b/sakura_core/macro/CMacro.cpp index bb47eaf95d..3e822a1f35 100644 --- a/sakura_core/macro/CMacro.cpp +++ b/sakura_core/macro/CMacro.cpp @@ -1939,7 +1939,7 @@ bool CMacro::HandleFunction(CEditView *View, EFunctionCode ID, const VARIANT *Ar } WCHAR szPath[ _MAX_PATH ]; - int nRet = SelectDir( View->GetHwnd(), sMessage.c_str(), sDefault.c_str(), szPath, _countof(szPath) ); + int nRet = SelectDir( View->GetHwnd(), sMessage.c_str(), sDefault.c_str(), szPath ); if( nRet == IDOK ){ SysString S( szPath, wcslen(szPath) ); Wrap( &Result )->Receive( S ); diff --git a/sakura_core/outline/CDlgFileTree.cpp b/sakura_core/outline/CDlgFileTree.cpp index 7b0a18c5eb..fa659c8ca5 100644 --- a/sakura_core/outline/CDlgFileTree.cpp +++ b/sakura_core/outline/CDlgFileTree.cpp @@ -537,7 +537,7 @@ BOOL CDlgFileTree::OnBnClicked( int wID ) // RADIO_GREP == folder WCHAR szDir[MAX_PATH]; DlgItem_GetText(GetHwnd(), IDC_EDIT_PATH, szDir, _countof(szDir) ); - if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir, _countof(szDir) ) ){ + if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir ) ){ DlgItem_SetText(GetHwnd(), IDC_EDIT_PATH, szDir ); } }else{ diff --git a/sakura_core/prop/CPropComBackup.cpp b/sakura_core/prop/CPropComBackup.cpp index 09529c121c..b2a33d2d5d 100644 --- a/sakura_core/prop/CPropComBackup.cpp +++ b/sakura_core/prop/CPropComBackup.cpp @@ -174,7 +174,7 @@ INT_PTR CPropBackup::DispatchEvent( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR WCHAR szFolder[_MAX_PATH]; ::DlgItem_GetText( hwndDlg, IDC_EDIT_BACKUPFOLDER, szFolder, _countof( szFolder )); - if( SelectDir( hwndDlg, LS(STR_PROPCOMBK_SEL_FOLDER), szFolder, szFolder, _countof( szFolder ) ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMBK_SEL_FOLDER), szFolder, szFolder ) ){ wcscpy( m_Common.m_sBackup.m_szBackUpFolder, szFolder ); ::DlgItem_SetText( hwndDlg, IDC_EDIT_BACKUPFOLDER, m_Common.m_sBackup.m_szBackUpFolder ); } diff --git a/sakura_core/prop/CPropComEdit.cpp b/sakura_core/prop/CPropComEdit.cpp index 4693714f55..c70bea0df4 100644 --- a/sakura_core/prop/CPropComEdit.cpp +++ b/sakura_core/prop/CPropComEdit.cpp @@ -118,7 +118,7 @@ INT_PTR CPropEdit::DispatchEvent( WCHAR szPath[_MAX_PATH]; ::DlgItem_GetText( hwndDlg, IDC_EDIT_FILEOPENDIR, szMetaPath, _countof(szMetaPath) ); CFileNameManager::ExpandMetaToFolder( szMetaPath, szPath, _countof(szPath) ); - if( SelectDir( hwndDlg, LS(STR_PROPEDIT_SELECT_DIR), szPath, szPath, _countof(szPath) ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPEDIT_SELECT_DIR), szPath, szPath ) ){ CNativeW cmem(szPath); cmem.Replace(L"%", L"%%"); ::DlgItem_SetText( hwndDlg, IDC_EDIT_FILEOPENDIR, cmem.GetStringPtr() ); diff --git a/sakura_core/prop/CPropComHelper.cpp b/sakura_core/prop/CPropComHelper.cpp index 4795daeb79..9eed90c925 100644 --- a/sakura_core/prop/CPropComHelper.cpp +++ b/sakura_core/prop/CPropComHelper.cpp @@ -168,7 +168,7 @@ INT_PTR CPropHelper::DispatchEvent( }else{ wcscpy( szPath, m_Common.m_sHelper.m_szMigemoDict ); } - if( SelectDir( hwndDlg, LS(STR_PROPCOMHELP_MIGEMODIR), szPath, szPath, _countof(szPath) ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMHELP_MIGEMODIR), szPath, szPath ) ){ wcscpy( m_Common.m_sHelper.m_szMigemoDict, GetRelPath(szPath) ); // 2015.03.03 可能なら相対パスにする ::DlgItem_SetText( hwndDlg, IDC_EDIT_MIGEMO_DICT, m_Common.m_sHelper.m_szMigemoDict ); } diff --git a/sakura_core/prop/CPropComMacro.cpp b/sakura_core/prop/CPropComMacro.cpp index 13a3136cb7..ecb1828c49 100644 --- a/sakura_core/prop/CPropComMacro.cpp +++ b/sakura_core/prop/CPropComMacro.cpp @@ -581,7 +581,7 @@ void CPropMacro::SelectBaseDir_Macro( HWND hwndDlg ) GetInidirOrExedir( szDir, folder ); } - if( SelectDir( hwndDlg, LS(STR_PROPCOMMACR_SEL_DIR), szDir, szDir, _countof(szDir) ) ){ + if( SelectDir( hwndDlg, LS(STR_PROPCOMMACR_SEL_DIR), szDir, szDir ) ){ // 末尾に\\マークを追加する. AddLastChar( szDir, _countof(szDir), L'\\' ); ::DlgItem_SetText( hwndDlg, IDC_MACRODIR, GetRelPath(szDir) ); // 2015.03.03 可能なら相対パスにする diff --git a/sakura_core/util/shell.h b/sakura_core/util/shell.h index 9a407d5a3c..c07f34c9f8 100644 --- a/sakura_core/util/shell.h +++ b/sakura_core/util/shell.h @@ -33,6 +33,13 @@ BOOL MyWinHelp(HWND hwndCaller, UINT uCommand, DWORD_PTR dwData); /* WinHelp の /* Shell Interface系(?) */ BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName, size_t nMaxCount ); /* フォルダ選択ダイアログ */ + +template +BOOL SelectDir(HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR(&strFolderName)[nMaxCount]) +{ + return SelectDir( hWnd, pszTitle, pszInitFolder, strFolderName, nMaxCount ); +} + BOOL ResolveShortcutLink(HWND hwnd, LPCWSTR lpszLinkFile, LPWSTR lpszPath);/* ショートカット(.lnk)の解決 */ HWND OpenHtmlHelp( HWND hWnd, LPCWSTR szFile, UINT uCmd, DWORD_PTR data,bool msgflag = true); From 127b8b439f579158ab23737f389d71365901ea72 Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sat, 3 Apr 2021 15:41:40 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E5=B7=AE=E5=88=86=E3=81=AE=E5=8E=9F=E5=9B=A0=E3=82=92?= =?UTF-8?q?=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/outline/CDlgFileTree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sakura_core/outline/CDlgFileTree.cpp b/sakura_core/outline/CDlgFileTree.cpp index fa659c8ca5..a72a138492 100644 --- a/sakura_core/outline/CDlgFileTree.cpp +++ b/sakura_core/outline/CDlgFileTree.cpp @@ -537,7 +537,7 @@ BOOL CDlgFileTree::OnBnClicked( int wID ) // RADIO_GREP == folder WCHAR szDir[MAX_PATH]; DlgItem_GetText(GetHwnd(), IDC_EDIT_PATH, szDir, _countof(szDir) ); - if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir ) ){ + if( SelectDir(hwndDlg, LS(STR_DLGGREP1), szDir, szDir) ){ DlgItem_SetText(GetHwnd(), IDC_EDIT_PATH, szDir ); } }else{ From 5c5cdeff63e644fc63d3c0a5af9e126989e17d06 Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sun, 4 Apr 2021 22:06:16 +0900 Subject: [PATCH 5/8] =?UTF-8?q?SetInitialDir()=20=E3=81=AE=E5=BC=95?= =?UTF-8?q?=E6=95=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/util/shell.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index 20d310501b..0bd626770e 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -38,12 +38,9 @@ #include "env/CShareData.h" #include "env/DLLSHAREDATA.h" #include "extmodule/CHtmlHelp.h" -<<<<<<< HEAD -#include -======= #include "config/app_constants.h" #include "String_define.h" ->>>>>>> master +#include /*! @@ -52,11 +49,11 @@ @param [in] pDialog 設定対象のダイアログ @param [in] pszInitFolder 初期フォルダに設定したいパス */ -static void SetInitialDir( Microsoft::WRL::ComPtr pDialog, const WCHAR* pszInitFolder ) +static void SetInitialDir( IFileDialog* pDialog, std::wstring_view pszInitFolder ) { WCHAR szInitFolder[MAX_PATH]; - wcscpy_s( szInitFolder, _countof(szInitFolder), pszInitFolder ); + wcscpy_s( szInitFolder, _countof(szInitFolder), pszInitFolder.data() ); // フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない CutLastYenFromDirectoryPath( szInitFolder ); @@ -96,7 +93,7 @@ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WC } // 初期フォルダを設定 - SetInitialDir( pDialog, pszInitFolder ); + SetInitialDir( pDialog.Get(), pszInitFolder ); // タイトル文字列を設定 hres = pDialog->SetTitle( pszTitle ); From 1b6628eaa3f19dc47f41ecb457822608bc6ab063 Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Wed, 7 Apr 2021 21:34:48 +0900 Subject: [PATCH 6/8] =?UTF-8?q?=E6=96=87=E5=AD=97=E5=88=97=E9=95=B7?= =?UTF-8?q?=E3=81=8C=20MAX=5FPATH=20=E3=82=92=E8=B6=85=E3=81=88=E3=82=8B?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=80=81=E5=AE=9F=E8=A1=8C=E6=99=82=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=81=AE=E3=82=92=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/util/shell.cpp | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index 0bd626770e..9dc609c83d 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -43,29 +43,6 @@ #include -/*! - @brief IFileDialog の初期フォルダを設定する - - @param [in] pDialog 設定対象のダイアログ - @param [in] pszInitFolder 初期フォルダに設定したいパス -*/ -static void SetInitialDir( IFileDialog* pDialog, std::wstring_view pszInitFolder ) -{ - - WCHAR szInitFolder[MAX_PATH]; - wcscpy_s( szInitFolder, _countof(szInitFolder), pszInitFolder.data() ); - - // フォルダの最後が半角かつ'\\'の場合は、取り除く "c:\\"等のルートは取り除かない - CutLastYenFromDirectoryPath( szInitFolder ); - - // 初期フォルダを設定 - Microsoft::WRL::ComPtr psiFolder; - HRESULT hres = SHCreateItemFromParsingName( szInitFolder, nullptr, IID_PPV_ARGS(&psiFolder) ); - if ( SUCCEEDED(hres) ) { - pDialog->SetFolder( psiFolder.Get() ); - } -} - /* フォルダ選択ダイアログ */ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName, size_t nMaxCount ) { @@ -93,7 +70,11 @@ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WC } // 初期フォルダを設定 - SetInitialDir( pDialog.Get(), pszInitFolder ); + ComPtr psiFolder; + hres = SHCreateItemFromParsingName( pszInitFolder, nullptr, IID_PPV_ARGS(&psiFolder) ); + if ( SUCCEEDED(hres) ) { + pDialog->SetFolder( psiFolder.Get() ); + } // タイトル文字列を設定 hres = pDialog->SetTitle( pszTitle ); From 54bc397b318a301a2780decde2288cf66e2c230a Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Thu, 8 Apr 2021 21:04:12 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=E6=A0=BC=E7=B4=8D=E5=85=88=E3=81=AE?= =?UTF-8?q?=E3=83=90=E3=83=83=E3=83=95=E3=82=A1=E3=82=B5=E3=82=A4=E3=82=BA?= =?UTF-8?q?=E3=81=8C=E8=B6=B3=E3=82=8A=E3=81=AA=E3=81=84=E5=A0=B4=E5=90=88?= =?UTF-8?q?=E3=80=81=E5=AE=9F=E8=A1=8C=E6=99=82=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=99=E3=82=8B=E3=81=AE=E3=82=92?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/util/shell.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index 9dc609c83d..d888a8a5f7 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -101,6 +101,11 @@ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WC return FALSE; } + if ( nMaxCount < wcslen(pszResult) ) { + CoTaskMemFree( pszResult ); + return FALSE; + } + wcscpy_s( strFolderName, nMaxCount, pszResult ); CoTaskMemFree( pszResult ); From 38e017ab4f3f0574fe8b264d47d9a228e71cd844 Mon Sep 17 00:00:00 2001 From: Ocelot <16898663+Ocelot1210@users.noreply.github.com> Date: Sat, 10 Apr 2021 14:38:56 +0900 Subject: [PATCH 8/8] =?UTF-8?q?Security=20Hotspot=20=E3=81=8C=E6=A4=9C?= =?UTF-8?q?=E5=87=BA=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E8=A9=A6=E3=81=BF=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/util/shell.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sakura_core/util/shell.cpp b/sakura_core/util/shell.cpp index d888a8a5f7..72adb3f02b 100644 --- a/sakura_core/util/shell.cpp +++ b/sakura_core/util/shell.cpp @@ -46,6 +46,10 @@ /* フォルダ選択ダイアログ */ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WCHAR* strFolderName, size_t nMaxCount ) { + if ( nullptr == strFolderName ) { + return FALSE; + } + using namespace Microsoft::WRL; ComPtr pDialog; HRESULT hres; @@ -101,15 +105,15 @@ BOOL SelectDir( HWND hWnd, const WCHAR* pszTitle, const WCHAR* pszInitFolder, WC return FALSE; } - if ( nMaxCount < wcslen(pszResult) ) { - CoTaskMemFree( pszResult ); - return FALSE; + BOOL bRet = TRUE; + if ( 0 != wcsncpy_s( strFolderName, nMaxCount, pszResult, _TRUNCATE ) ) { + wcsncpy_s( strFolderName, nMaxCount, L"", _TRUNCATE ); + bRet = FALSE; } - wcscpy_s( strFolderName, nMaxCount, pszResult ); CoTaskMemFree( pszResult ); - return TRUE; + return bRet; } /*! 特殊フォルダのパスを取得する