Skip to content

Commit

Permalink
Merge pull request #159 from kohenkatz/fix-context-menu
Browse files Browse the repository at this point in the history
Add registration for right-click on folder item; Add context-menu icon
  • Loading branch information
MartiUK committed Apr 10, 2014
2 parents 3a8daf5 + 447be26 commit 691000f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
Binary file modified Cmder.exe
Binary file not shown.
45 changes: 30 additions & 15 deletions launcher/src/CmderLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#define USE_TASKBAR_API (_WIN32_WINNT >= _WIN32_WINNT_WIN7)

#define MB_TITLE L"Cmder Launcher"
#define SHELL_MENU_REGISTRY_PATH L"Directory\\Background\\shell\\Cmder"
#define SHELL_MENU_REGISTRY_PATH_BACKGROUND L"Directory\\Background\\shell\\Cmder"
#define SHELL_MENU_REGISTRY_PATH_LISTITEM L"Directory\\shell\\Cmder"

#define streqi(a, b) (_wcsicmp((a), (b)) == 0)

Expand Down Expand Up @@ -154,46 +155,58 @@ HKEY GetRootKey(std::wstring opt)
return root;
}

void RegisterShellMenu(std::wstring opt)
void RegisterShellMenu(std::wstring opt, wchar_t* keyBaseName)
{
// First, get the paths we will use

wchar_t exePath[MAX_PATH] = { 0 };
wchar_t icoPath[MAX_PATH] = { 0 };

GetModuleFileName(NULL, exePath, sizeof(exePath));

wchar_t commandStr[MAX_PATH + 20] = { 0 };
swprintf_s(commandStr, L"\"%s\" \"%%V\"", exePath);

// Now that we have `commandStr`, it's OK to change `exePath`...
PathRemoveFileSpec(exePath);

PathCombine(icoPath, exePath, L"icons\\cmder.ico");

// Now set the registry keys

HKEY root = GetRootKey(opt);

HKEY cmderKey;
FAIL_ON_ERROR(
RegCreateKeyEx(root, SHELL_MENU_REGISTRY_PATH, 0, NULL,
RegCreateKeyEx(root, keyBaseName, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL));

FAIL_ON_ERROR(RegSetValue(cmderKey, L"", REG_SZ, L"Cmder Here", NULL));
FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"NoWorkingDirectory", 0, REG_SZ, (BYTE *)L"", 2));

FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"Icon", 0, REG_SZ, (BYTE *)icoPath, wcslen(icoPath) * sizeof(wchar_t)));

HKEY command;
FAIL_ON_ERROR(
RegCreateKeyEx(cmderKey, L"command", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &command, NULL));

wchar_t exePath[MAX_PATH] = { 0 };

GetModuleFileName(NULL, exePath, sizeof(exePath));

wchar_t commandStr[MAX_PATH + 20] = { 0 };
swprintf_s(commandStr, L"\"%s\" \"%%V\"", exePath);

FAIL_ON_ERROR(RegSetValue(command, L"", REG_SZ, commandStr, NULL));

RegCloseKey(command);
RegCloseKey(cmderKey);
RegCloseKey(root);
}

void UnregisterShellMenu(std::wstring opt)
void UnregisterShellMenu(std::wstring opt, wchar_t* keyBaseName)
{
HKEY root = GetRootKey(opt);
HKEY cmderKey;
FAIL_ON_ERROR(
RegCreateKeyEx(root, SHELL_MENU_REGISTRY_PATH, 0, NULL,
RegCreateKeyEx(root, keyBaseName, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL));
FAIL_ON_ERROR(RegDeleteTree(cmderKey, NULL));
FAIL_ON_ERROR(RegDeleteKey(root, SHELL_MENU_REGISTRY_PATH));
FAIL_ON_ERROR(RegDeleteKey(root, keyBaseName));
RegCloseKey(cmderKey);
RegCloseKey(root);
}
Expand All @@ -215,11 +228,13 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
}
else if (streqi(opt.first.c_str(), L"/REGISTER"))
{
RegisterShellMenu(opt.second);
RegisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_BACKGROUND);
RegisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_LISTITEM);
}
else if (streqi(opt.first.c_str(), L"/UNREGISTER"))
{
UnregisterShellMenu(opt.second);
UnregisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_BACKGROUND);
UnregisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_LISTITEM);
}
else
{
Expand Down

0 comments on commit 691000f

Please sign in to comment.