Skip to content

Commit

Permalink
Support background context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
rikka0w0 committed Oct 13, 2018
1 parent ae9f4d7 commit 7b9206e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 150 deletions.
2 changes: 2 additions & 0 deletions FileContextMenuExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ extern LPCITEMIDLIST g_pidl;

extern void StartBuildPIDLArray(DWORD length);
extern void AddToPIDLArray(LPCITEMIDLIST pidl);
extern void MarkAsBackgroundMenu();
extern void SetCurrentPIDL();
// Initialize the context menu handler.
IFACEMETHODIMP FileContextMenuExt::Initialize(
Expand All @@ -131,6 +132,7 @@ IFACEMETHODIMP FileContextMenuExt::Initialize(
// Background
StartBuildPIDLArray(1);
AddToPIDLArray(pidlFolder);
MarkAsBackgroundMenu();
SetCurrentPIDL();
}
return S_OK;
Expand Down
4 changes: 4 additions & 0 deletions MenuPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ void AddToPIDLArray(LPCITEMIDLIST pidl) {
g_PIDLArray_curId++;
}

void MarkAsBackgroundMenu() {
g_PIDLArray[0] = NULL;
}

void SetCurrentPIDL() {
HMODULE hPayload = GetShellPayload();
LPTHREAD_START_ROUTINE funcAddr = (LPTHREAD_START_ROUTINE)GetProcAddress(hPayload, "__SetCurrentPIDL");
Expand Down
78 changes: 0 additions & 78 deletions ShellPayload/MinCRT.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions ShellPayload/MinCRT.h

This file was deleted.

20 changes: 12 additions & 8 deletions ShellPayload/ShellPayload.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@
</ClCompile>
<Lib />
<Link>
<IgnoreSpecificDefaultLibraries>msvcrt.lib;msvcrtd.lib</IgnoreSpecificDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<SubSystem>Windows</SubSystem>
<MinimumRequiredVersion>5.02</MinimumRequiredVersion>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -122,8 +124,9 @@
</ClCompile>
<Lib />
<Link>
<IgnoreSpecificDefaultLibraries>msvcrt.lib;msvcrtd.lib</IgnoreSpecificDefaultLibraries>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
<SubSystem>Windows</SubSystem>
</Link>
</ItemDefinitionGroup>
Expand All @@ -148,9 +151,11 @@
</ClCompile>
<Lib />
<Link>
<IgnoreSpecificDefaultLibraries>msvcrt.lib;msvcrtd.lib</IgnoreSpecificDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<SubSystem>Windows</SubSystem>
<MinimumRequiredVersion>5.02</MinimumRequiredVersion>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -176,22 +181,21 @@
</ClCompile>
<Lib />
<Link>
<IgnoreSpecificDefaultLibraries>msvcrt.lib;msvcrtd.lib</IgnoreSpecificDefaultLibraries>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<SubSystem>Windows</SubSystem>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="MinCRT.cpp" />
<ClCompile Include="minihook\buffer.c" />
<ClCompile Include="minihook\hde\hde32.c" />
<ClCompile Include="minihook\hde\hde64.c" />
<ClCompile Include="minihook\hook.c" />
<ClCompile Include="minihook\trampoline.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="MinCRT.h" />
<ClInclude Include="minihook\buffer.h" />
<ClInclude Include="minihook\hde\hde32.h" />
<ClInclude Include="minihook\hde\hde64.h" />
Expand Down
2 changes: 0 additions & 2 deletions ShellPayload/ShellPayload.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="MinCRT.cpp" />
<ClCompile Include="minihook\buffer.c">
<Filter>minihook</Filter>
</ClCompile>
Expand All @@ -20,7 +19,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MinCRT.h" />
<ClInclude Include="tmt.h" />
<ClInclude Include="minihook\buffer.h">
<Filter>minihook</Filter>
Expand Down
137 changes: 88 additions & 49 deletions ShellPayload/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "tmt.h"
#include "minihook\MinHook.h"
#include <shlobj.h>
#include <shlwapi.h>

static HMODULE gLibModule = 0;

Expand All @@ -17,51 +18,77 @@ HRESULT FillContextMenuFromPIDL(LPCITEMIDLIST* pidl, DWORD pidl_len, HMENU hTopM
IContextMenu *pcm;
HRESULT hr;

IShellFolder *psfDesktop;
if (SUCCEEDED(SHGetDesktopFolder(&psfDesktop))) {
if (pidl_len == 0) {
IShellFolder *psf;
LPCITEMIDLIST pidlChild;
hr = SHBindToParent(pidl[0], IID_IShellFolder, (void**)&psf, &pidlChild);
if (FAILED(hr))
return hr;

IShellView *psw;
hr = psf->CreateViewObject(NULL, IID_IShellView, (void**)&psw);
if (FAILED(hr))
return hr;

hr = psw->GetItemObject(SVGIO_BACKGROUND, IID_IContextMenu, (void**)&pcm);

if (FAILED(hr))
return hr;

psw->Release();
psf->Release();
}
else {
IShellFolder *psfDesktop;
SHGetDesktopFolder(&psfDesktop);
if (FAILED(hr))
return hr;

hr = psfDesktop->GetUIObjectOf(NULL, pidl_len, pidl, IID_IContextMenu, NULL, (void**)&pcm);

psfDesktop->Release();
}

if (SUCCEEDED(hr)) {
hr = pcm->QueryContextMenu(hTopMenu, 0, 1, 0x7FFF, uFlags);

if (SUCCEEDED(hr)) {
hr = pcm->QueryContextMenu(hTopMenu, 0, 1, 0x7FFF, uFlags | CMF_ITEMMENU);
IContextMenu3* hContextMenu3 = NULL;
IContextMenu2* hContextMenu2 = NULL;

/*
if (SUCCEEDED(pcm->QueryInterface(IID_IContextMenu3, (void**)&hContextMenu3))) {
LRESULT lres;
if (SUCCEEDED(hr)) {
IContextMenu3* hContextMenu3 = NULL;
IContextMenu2* hContextMenu2 = NULL;
hContextMenu3->HandleMenuMsg2(WM_INITMENUPOPUP, (WPARAM)hTopMenu, 0, &lres);
/*
if (SUCCEEDED(pcm->QueryInterface(IID_IContextMenu3, (void**)&hContextMenu3))) {
LRESULT lres;
for (int pos = 0; pos < GetMenuItemCount(hTopMenu); pos++) {
HMENU subMenu = GetSubMenu(hTopMenu, pos);
if (subMenu != NULL) {
LPARAM lparam = (LPARAM)(MAKELONG(pos, FALSE));
hContextMenu3->HandleMenuMsg2(WM_INITMENUPOPUP, (WPARAM)subMenu, lparam, &lres);
}
}
//hContextMenu3->Release();
}*/

hContextMenu3->HandleMenuMsg2(WM_INITMENUPOPUP, (WPARAM)hTopMenu, 0, &lres);
if (SUCCEEDED(pcm->QueryInterface(IID_IContextMenu2, (void**)&hContextMenu2))) {
hContextMenu2->HandleMenuMsg(WM_INITMENUPOPUP, (WPARAM)hTopMenu, FALSE);

for (int pos = 0; pos < GetMenuItemCount(hTopMenu); pos++) {
HMENU subMenu = GetSubMenu(hTopMenu, pos);
if (subMenu != NULL) {
LPARAM lparam = (LPARAM)(MAKELONG(pos, FALSE));
hContextMenu3->HandleMenuMsg2(WM_INITMENUPOPUP, (WPARAM)subMenu, lparam, &lres);
}
}
//hContextMenu3->Release();
}*/

if (SUCCEEDED(pcm->QueryInterface(IID_IContextMenu2, (void**)&hContextMenu2))) {
hContextMenu2->HandleMenuMsg(WM_INITMENUPOPUP, (WPARAM)hTopMenu, FALSE);

for (int pos = 0; pos < GetMenuItemCount(hTopMenu); pos++) {
HMENU subMenu = GetSubMenu(hTopMenu, pos);
if (subMenu != NULL) {
LPARAM lparam = (LPARAM)(MAKELONG(pos, FALSE));
hContextMenu2->HandleMenuMsg(WM_INITMENUPOPUP, (WPARAM)subMenu, lparam);
}
HMENU subMenu = GetSubMenu(hTopMenu, pos);
if (subMenu != NULL) {
LPARAM lparam = (LPARAM)(MAKELONG(pos, FALSE));
hContextMenu2->HandleMenuMsg(WM_INITMENUPOPUP, (WPARAM)subMenu, lparam);
}
//hContextMenu2->Release();
}

ret = MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
//hContextMenu2->Release();
}

//pcm->Release();
ret = MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
}

//pcm->Release();
}

return ret;
Expand Down Expand Up @@ -138,29 +165,39 @@ FPT_TrackPopupMenu fpTrackPopupMenu;
LONG_PTR g_oldWndProc = NULL;

LRESULT CALLBACK HookedWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HMENU root;
static HMENU hMenuRoot;
static int level;

LRESULT ret = WNDPROC(g_oldWndProc)(hwnd, uMsg, wParam, lParam);
if (uMsg == WM_INITMENUPOPUP) {

if (uMsg == WM_INITMENU) {
hMenuRoot = NULL;
level = -1;
} else if (uMsg == WM_INITMENUPOPUP) {
HMENU hMenu = (HMENU)wParam;
WORD pos = LOWORD(lParam);

if (pos == 0) {
ClassicMenuShell(hMenu);
root = hMenu;
if (level == -1) {
if (pos == 0) {
hMenuRoot = hMenu;
level = 0;

ClassicMenuShell(hMenu);
}
}
else {/*
HMENU iconSource = CreatePopupMenu(); // Create an empty menu
FillContextMenuFromPIDL(g_last_pidl, g_last_pidl_len, iconSource, g_last_QCMFlags); // And fill it
if (GetMenuItemCount(iconSource) != GetMenuItemCount(root)) {
int a = GetMenuItemCount(iconSource);
int b = GetMenuItemCount(root);
for (int i = 0; i < GetMenuItemCount(iconSource); i++) {
char text[256];
GetMenuStringA(iconSource, i, text, sizeof(text), MF_BYPOSITION);
MessageBoxA(0, text, "", 0);
}
else {
level++;
ClassicMenuShell(hMenu);
if (level == 1) {

}
DestroyMenu(iconSource);*/
}
}
else if (uMsg == WM_UNINITMENUPOPUP) {
level--;
if (level == -1) {
// Menu closing

}
}

Expand All @@ -176,8 +213,10 @@ BOOL WINAPI HookedTrackPopupMenu(HMENU hMenu, UINT uFlags, int x, int y, int nRe
}

BOOL ret = fpTrackPopupMenu(hMenu, uFlags, x, y, nReserved, hWnd, prcRect);
//free(g_last_pidl);
g_last_pidl = NULL;


if (g_oldWndProc != NULL) {
SetWindowLongPtr(hWnd, GWLP_WNDPROC, g_oldWndProc);
g_oldWndProc = NULL;
Expand Down
Binary file modified install.reg
Binary file not shown.
Binary file modified uninstall.reg
Binary file not shown.

0 comments on commit 7b9206e

Please sign in to comment.