Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start admin command prompt in selected directory #367

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 50 additions & 41 deletions src/wfcomman.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,52 +1021,61 @@ AppCommandProc(register DWORD id)
break;

case IDM_STARTCMDSHELL:
{
BOOL bRunAs;
BOOL bUseCmd;
BOOL bDir;
DWORD cchEnv;
TCHAR szToRun[MAXPATHLEN];
LPTSTR szDir;
{
BOOL bRunAs;
BOOL bUseCmd;
BOOL bDir;
DWORD cchEnv;
TCHAR szToRun[MAXPATHLEN];
LPTSTR szDir;

#define ConEmuParamFormat TEXT(" -Single -Dir \"%s\"")
TCHAR szParams[MAXPATHLEN + COUNTOF(ConEmuParamFormat)];
#define CmdParamFormat TEXT("/k cd /d ")
TCHAR szParams[MAXPATHLEN + max(COUNTOF(CmdParamFormat), COUNTOF(ConEmuParamFormat))];

szDir = GetSelection(1|4|16, &bDir);
if (!bDir && szDir)
StripFilespec(szDir);

bRunAs = GetKeyState(VK_SHIFT) < 0;

// check if conemu exists: %ProgramFiles%\ConEmu\ConEmu64.exe
bUseCmd = TRUE;
cchEnv = GetEnvironmentVariable(TEXT("ProgramFiles"), szToRun, MAXPATHLEN);
if (cchEnv != 0) {
if (lstrcmpi(szToRun + cchEnv - 6, TEXT(" (x86)")) == 0) {
szToRun[cchEnv - 6] = TEXT('\0');
}
// NOTE: assume ProgramFiles directory and "\\ConEmu\\ConEmu64.exe" never exceed MAXPATHLEN
lstrcat(szToRun, TEXT("\\ConEmu\\ConEmu64.exe"));
if (PathFileExists(szToRun)) {
wsprintf(szParams, ConEmuParamFormat, szDir);
bUseCmd = FALSE;
}
}
szDir = GetSelection(1 | 4 | 16, &bDir);
if (!bDir && szDir)
StripFilespec(szDir);

// use cmd.exe if ConEmu doesn't exist or we are running admin mode
if (bUseCmd || bRunAs) {
// NOTE: assume system directory and "\\cmd.exe" never exceed MAXPATHLEN
if (GetSystemDirectory(szToRun, MAXPATHLEN) != 0)
lstrcat(szToRun, TEXT("\\cmd.exe"));
else
lstrcpy(szToRun, TEXT("cmd.exe"));
szParams[0] = TEXT('\0');
}
bRunAs = GetKeyState(VK_SHIFT) < 0;

ret = ExecProgram(szToRun, szParams, szDir, FALSE, bRunAs);
LocalFree(szDir);
}
break;
// check if conemu exists: %ProgramFiles%\ConEmu\ConEmu64.exe
bUseCmd = TRUE;
cchEnv = GetEnvironmentVariable(TEXT("ProgramFiles"), szToRun, MAXPATHLEN);
if (cchEnv != 0) {
if (lstrcmpi(szToRun + cchEnv - 6, TEXT(" (x86)")) == 0) {
szToRun[cchEnv - 6] = TEXT('\0');
}
// NOTE: assume ProgramFiles directory and "\\ConEmu\\ConEmu64.exe" never exceed MAXPATHLEN
lstrcat(szToRun, TEXT("\\ConEmu\\ConEmu64.exe"));
if (PathFileExists(szToRun)) {
wsprintf(szParams, ConEmuParamFormat, szDir);
bUseCmd = FALSE;
}
}

// use cmd.exe if ConEmu doesn't exist or we are running admin mode
if (bUseCmd) {
// NOTE: assume system directory and "\\cmd.exe" never exceed MAXPATHLEN
if (GetSystemDirectory(szToRun, MAXPATHLEN) != 0)
lstrcat(szToRun, TEXT("\\cmd.exe"));
else
lstrcpy(szToRun, TEXT("cmd.exe"));

if (bRunAs) {
// Execute a command prompt and cd into the directory
lstrcpy(szParams, CmdParamFormat);
lstrcat(szParams, szDir);
}
else {
szParams[0] = TEXT('\0');
}
}

ret = ExecProgram(szToRun, szParams, szDir, FALSE, bRunAs);
LocalFree(szDir);
}
break;

case IDM_STARTPOWERSHELL:
{
Expand Down