+ rfc: get application directory refactoring

This commit is contained in:
RaiKoHoff 2020-03-19 17:30:55 +01:00
parent ca9ece5b7a
commit 673f0f63b8
5 changed files with 61 additions and 48 deletions

View File

@ -694,6 +694,7 @@ extern "C" bool FindIniFile()
WCHAR tchModule[MAX_PATH] = { L'\0' };
GetModuleFileName(NULL, tchModule, COUNTOF(tchModule));
PathCanonicalizeEx(tchModule, COUNTOF(tchModule));
// set env path to module dir
StringCchCopy(tchPath, COUNTOF(tchPath), tchModule);

View File

@ -993,6 +993,7 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM l
if (StringCchCompareNI(arg1, COUNTOF(arg1), _W(SAPPNAME), CSTRLEN(_W(SAPPNAME))) == 0 ||
StringCchCompareNI(arg1, COUNTOF(arg1), L"notepad3.exe", CSTRLEN(L"notepad3.exe")) == 0) {
GetModuleFileName(NULL, arg1, COUNTOF(arg1));
PathCanonicalizeEx(arg1, COUNTOF(arg1));
bQuickExit = true;
}
@ -3398,9 +3399,7 @@ void DialogFileBrowse(HWND hwnd)
StringCchCopy(tchExeFile, COUNTOF(tchExeFile), Constants.FileBrowserMiniPath);
}
if (PathIsRelative(tchExeFile)) {
GetModuleFileName(NULL, tchTemp, COUNTOF(tchTemp));
NormalizePathEx(tchTemp, COUNTOF(tchTemp), true, false);
PathCchRemoveFileSpec(tchTemp, COUNTOF(tchTemp));
PathGetAppDirectory(tchTemp, COUNTOF(tchTemp));
PathAppend(tchTemp, tchExeFile);
if (PathFileExists(tchTemp)) {
StringCchCopy(tchExeFile, COUNTOF(tchExeFile), tchTemp);
@ -3476,7 +3475,7 @@ void DialogGrepWin(HWND hwnd, LPCWSTR searchPattern)
const WCHAR* const tchParamFmt = L"/portable /content %s /searchpath:\"%s\" /searchfor:\"%s\"";
GetModuleFileName(NULL, tchModulePath, COUNTOF(tchModulePath));
NormalizePathEx(tchModulePath, COUNTOF(tchModulePath), true, false);
PathCanonicalizeEx(tchModulePath, COUNTOF(tchModulePath));
// grepWin executable
if (StrIsNotEmpty(Settings2.GrepWinPath)) {
@ -3499,7 +3498,9 @@ void DialogGrepWin(HWND hwnd, LPCWSTR searchPattern)
StringCchCopy(tchGrepWinDir, COUNTOF(tchGrepWinDir), tchExeFile);
PathCchRemoveFileSpec(tchGrepWinDir, COUNTOF(tchGrepWinDir));
// relative Notepad3 path (for grepWin's EditorCmd)
PathRelativePathTo(tchModulePath, tchGrepWinDir, FILE_ATTRIBUTE_DIRECTORY, tchModulePath, FILE_ATTRIBUTE_NORMAL);
if (PathRelativePathToW(tchTemp, tchGrepWinDir, FILE_ATTRIBUTE_DIRECTORY, tchModulePath, FILE_ATTRIBUTE_NORMAL)) {
StringCchCopy(tchModulePath, COUNTOF(tchModulePath), tchTemp);
}
// grepWin INI-File
StringCchCopy(tchIniFilePath, COUNTOF(tchIniFilePath), tchGrepWinDir);
PathAppend(tchIniFilePath, L"grepwin.ini");
@ -3570,8 +3571,7 @@ void DialogAdminExe(HWND hwnd, bool bExecInstaller)
WCHAR tchExePath[MAX_PATH];
if (!SearchPath(NULL, tchExe, L".exe", COUNTOF(tchExePath), tchExePath, NULL)) {
// try Notepad3's dir path
GetModuleFileName(NULL, tchExePath, COUNTOF(tchExePath));
PathCchRemoveFileSpec(tchExePath, COUNTOF(tchExePath));
PathGetAppDirectory(tchExePath, COUNTOF(tchExePath));
PathCchAppend(tchExePath, COUNTOF(tchExePath), tchExe);
}

View File

@ -790,6 +790,19 @@ bool GetKnownFolderPath(REFKNOWNFOLDERID rfid, LPWSTR lpOutPath, size_t cchCount
return false;
}
//=============================================================================
//
// PathGetModuleDirectory()
//
void PathGetAppDirectory(LPWSTR lpszDest, DWORD cchDest)
{
GetModuleFileName(NULL, lpszDest, cchDest);
PathCanonicalizeEx(lpszDest, cchDest);
PathCchRemoveFileSpec(lpszDest, (size_t)cchDest);
}
//=============================================================================
//
// PathRelativeToApp()
@ -798,47 +811,52 @@ void PathRelativeToApp(
LPWSTR lpszSrc,LPWSTR lpszDest,int cchDest,bool bSrcIsFile,
bool bUnexpandEnv,bool bUnexpandMyDocs) {
WCHAR wchAppPath[MAX_PATH] = { L'\0' };
WCHAR wchAppDir[MAX_PATH] = { L'\0' };
WCHAR wchWinDir[MAX_PATH] = { L'\0' };
WCHAR wchUserFiles[MAX_PATH] = { L'\0' };
WCHAR wchPath[MAX_PATH] = { L'\0' };
WCHAR wchResult[MAX_PATH] = { L'\0' };
DWORD dwAttrTo = (bSrcIsFile) ? 0 : FILE_ATTRIBUTE_DIRECTORY;
DWORD dwAttrTo = (bSrcIsFile) ? FILE_ATTRIBUTE_NORMAL : FILE_ATTRIBUTE_DIRECTORY;
PathGetAppDirectory(wchAppDir, COUNTOF(wchAppDir));
GetModuleFileName(NULL,wchAppPath,COUNTOF(wchAppPath));
PathCanonicalizeEx(wchAppPath,MAX_PATH);
PathCchRemoveFileSpec(wchAppPath,COUNTOF(wchAppPath));
(void)GetWindowsDirectory(wchWinDir,COUNTOF(wchWinDir));
GetKnownFolderPath(&FOLDERID_Documents, wchUserFiles, COUNTOF(wchUserFiles));
if (bUnexpandMyDocs &&
!PathIsRelative(lpszSrc) &&
!PathIsPrefix(wchUserFiles,wchAppPath) &&
!PathIsPrefix(wchUserFiles,wchAppDir) &&
PathIsPrefix(wchUserFiles,lpszSrc) &&
PathRelativePathTo(wchPath,wchUserFiles,FILE_ATTRIBUTE_DIRECTORY,lpszSrc,dwAttrTo)) {
PathRelativePathTo(wchPath,wchUserFiles,FILE_ATTRIBUTE_DIRECTORY,lpszSrc,dwAttrTo))
{
StringCchCopy(wchUserFiles,COUNTOF(wchUserFiles),L"%CSIDL:MYDOCUMENTS%");
PathCchAppend(wchUserFiles,COUNTOF(wchUserFiles),wchPath);
StringCchCopy(wchPath,COUNTOF(wchPath),wchUserFiles);
}
else if (PathIsRelative(lpszSrc) || PathCommonPrefix(wchAppPath,wchWinDir,NULL))
StringCchCopyN(wchPath,COUNTOF(wchPath),lpszSrc,COUNTOF(wchPath));
else if (PathIsRelative(lpszSrc) || PathCommonPrefix(wchAppDir, wchWinDir, NULL)) {
StringCchCopyN(wchPath, COUNTOF(wchPath), lpszSrc, COUNTOF(wchPath));
}
else {
if (!PathRelativePathTo(wchPath,wchAppPath,FILE_ATTRIBUTE_DIRECTORY,lpszSrc,dwAttrTo))
StringCchCopyN(wchPath,COUNTOF(wchPath),lpszSrc,COUNTOF(wchPath));
if (!PathRelativePathTo(wchPath, wchAppDir, FILE_ATTRIBUTE_DIRECTORY, lpszSrc, dwAttrTo)) {
StringCchCopyN(wchPath, COUNTOF(wchPath), lpszSrc, COUNTOF(wchPath));
}
}
if (bUnexpandEnv) {
if (!PathUnExpandEnvStrings(wchPath,wchResult,COUNTOF(wchResult)))
StringCchCopyN(wchResult,COUNTOF(wchResult),wchPath,COUNTOF(wchResult));
if (!PathUnExpandEnvStrings(wchPath, wchResult, COUNTOF(wchResult))) {
StringCchCopyN(wchResult, COUNTOF(wchResult), wchPath, COUNTOF(wchResult));
}
}
else {
StringCchCopyN(wchResult, COUNTOF(wchResult), wchPath, COUNTOF(wchResult));
}
else
StringCchCopyN(wchResult,COUNTOF(wchResult),wchPath,COUNTOF(wchResult));
int cchLen = (cchDest == 0) ? MAX_PATH : cchDest;
if (lpszDest == NULL || lpszSrc == lpszDest)
StringCchCopyN(lpszSrc,cchLen,wchResult,cchLen);
else
StringCchCopyN(lpszDest,cchLen,wchResult,cchLen);
if (lpszDest == NULL || lpszSrc == lpszDest) {
StringCchCopyN(lpszSrc, cchLen, wchResult, cchLen);
}
else {
StringCchCopyN(lpszDest, cchLen, wchResult, cchLen);
}
}
@ -870,9 +888,7 @@ void PathAbsoluteFromApp(LPWSTR lpszSrc,LPWSTR lpszDest,int cchDest,bool bExpand
ExpandEnvironmentStringsEx(wchPath,COUNTOF(wchPath));
if (PathIsRelative(wchPath)) {
GetModuleFileName(NULL,wchResult,COUNTOF(wchResult));
PathCanonicalizeEx(wchResult, COUNTOF(wchResult));
PathCchRemoveFileSpec(wchResult, COUNTOF(wchResult));
PathGetAppDirectory(wchResult, COUNTOF(wchResult));
PathCchAppend(wchResult,COUNTOF(wchResult),wchPath);
}
else
@ -1004,7 +1020,6 @@ bool PathIsLnkToDirectory(LPCWSTR pszPath,LPWSTR pszResPath,int cchResPath)
//
bool PathCreateDeskLnk(LPCWSTR pszDocument)
{
WCHAR tchExeFile[MAX_PATH] = { L'\0' };
WCHAR tchDocTemp[MAX_PATH] = { L'\0' };
WCHAR tchArguments[MAX_PATH+16] = { L'\0' };
@ -1021,6 +1036,7 @@ bool PathCreateDeskLnk(LPCWSTR pszDocument)
// init strings
GetModuleFileName(NULL,tchExeFile,COUNTOF(tchExeFile));
PathCanonicalizeEx(tchExeFile, COUNTOF(tchExeFile));
StringCchCopy(tchDocTemp,COUNTOF(tchDocTemp),pszDocument);
PathQuoteSpaces(tchDocTemp);

View File

@ -280,6 +280,7 @@ inline bool IsButtonUnchecked(HWND hwnd, int iButtonID) { return (IsDlgButtonChe
bool ReadFileXL(HANDLE hFile, char* const lpBuffer, const size_t nNumberOfBytesToRead, size_t* const lpNumberOfBytesRead);
bool WriteFileXL(HANDLE hFile, const char* const lpBuffer, const size_t nNumberOfBytesToWrite, size_t* const lpNumberOfBytesWritten);
void PathGetAppDirectory(LPWSTR lpszDest, DWORD cchDest);
bool GetKnownFolderPath(REFKNOWNFOLDERID, LPWSTR lpOutPath, size_t cchCount);
void PathRelativeToApp(LPWSTR lpszSrc,LPWSTR lpszDest,int cchDest,bool,bool,bool);
void PathAbsoluteFromApp(LPWSTR lpszSrc,LPWSTR lpszDest,int cchDest,bool);

View File

@ -810,10 +810,8 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
Globals.hPrevInst = hPrevInstance;
Globals.hndlProcessHeap = GetProcessHeap();
WCHAR wchAppDir[2 * MAX_PATH + 4] = { L'\0' };
GetModuleFileName(NULL,wchAppDir,COUNTOF(wchAppDir));
PathCchRemoveFileSpec(wchAppDir, COUNTOF(wchAppDir));
PathCanonicalizeEx(wchAppDir,COUNTOF(wchAppDir));
WCHAR wchAppDir[MAX_PATH] = { L'\0' };
PathGetAppDirectory(wchAppDir, COUNTOF(wchAppDir));
if (!GetCurrentDirectory(COUNTOF(Globals.WorkingDirectory),Globals.WorkingDirectory)) {
StringCchCopy(Globals.WorkingDirectory,COUNTOF(Globals.WorkingDirectory),wchAppDir);
@ -2099,8 +2097,7 @@ static HBITMAP LoadBitmapFile(LPCWSTR path)
{
WCHAR szTmp[MAX_PATH];
if (PathIsRelative(path)) {
GetModuleFileName(NULL, szTmp, COUNTOF(szTmp));
PathCchRemoveFileSpec(szTmp, COUNTOF(szTmp));
PathGetAppDirectory(szTmp, COUNTOF(szTmp));
PathAppend(szTmp, path);
path = szTmp;
}
@ -10050,10 +10047,8 @@ bool OpenFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
ExpandEnvironmentStrings(Settings2.DefaultDirectory,tchInitialDir,COUNTOF(tchInitialDir));
if (PathIsRelative(tchInitialDir)) {
WCHAR tchModule[MAX_PATH] = { L'\0' };
GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
PathCchRemoveFileSpec(tchModule, COUNTOF(tchModule));
PathGetAppDirectory(tchModule, COUNTOF(tchModule));
PathCchAppend(tchModule,COUNTOF(tchModule),tchInitialDir);
PathCchCanonicalize(tchInitialDir,COUNTOF(tchInitialDir),tchModule);
}
}
else
@ -10094,8 +10089,9 @@ bool SaveFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
StringCchCopy(szNewFile,COUNTOF(szNewFile),lpstrFile);
Style_GetOpenDlgFilterStr(s_szFilter,COUNTOF(s_szFilter));
if (StrIsNotEmpty(lpstrInitialDir))
StringCchCopy(tchInitialDir,COUNTOF(tchInitialDir),lpstrInitialDir);
if (StrIsNotEmpty(lpstrInitialDir)) {
StringCchCopy(tchInitialDir, COUNTOF(tchInitialDir), lpstrInitialDir);
}
else if (StrIsNotEmpty(Globals.CurrentFile)) {
StringCchCopy(tchInitialDir,COUNTOF(tchInitialDir),Globals.CurrentFile);
PathCchRemoveFileSpec(tchInitialDir, COUNTOF(tchInitialDir));
@ -10104,15 +10100,13 @@ bool SaveFileDlg(HWND hwnd,LPWSTR lpstrFile,int cchFile,LPCWSTR lpstrInitialDir)
ExpandEnvironmentStrings(Settings2.DefaultDirectory,tchInitialDir,COUNTOF(tchInitialDir));
if (PathIsRelative(tchInitialDir)) {
WCHAR tchModule[MAX_PATH] = { L'\0' };
GetModuleFileName(NULL,tchModule,COUNTOF(tchModule));
PathCchRemoveFileSpec(tchModule, COUNTOF(tchModule));
PathGetAppDirectory(tchModule, COUNTOF(tchModule));
PathCchAppend(tchModule,COUNTOF(tchModule),tchInitialDir);
PathCchCanonicalize(tchInitialDir,COUNTOF(tchInitialDir),tchModule);
}
}
else
StringCchCopy(tchInitialDir,COUNTOF(tchInitialDir),Globals.WorkingDirectory);
else {
StringCchCopy(tchInitialDir, COUNTOF(tchInitialDir), Globals.WorkingDirectory);
}
ZeroMemory(&ofn,sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
@ -10451,6 +10445,7 @@ bool RelaunchElevated(LPWSTR lpNewCmdLnArgs)
ExtractFirstArgument(lpCmdLine, lpExe, szOrigArgs, (int)wlen);
// override
GetModuleFileName(NULL, lpExe, COUNTOF(lpExe)); // full path
PathCanonicalizeEx(lpExe, COUNTOF(lpExe));
if (lpNewCmdLnArgs) {
StringCchCopy(szOrigArgs, COUNTOF(szOrigArgs), lpNewCmdLnArgs);
}