Skip to content

Commit

Permalink
work around separator menu item deletion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-André Moreau committed Aug 6, 2024
1 parent e460508 commit 1a5975c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
49 changes: 48 additions & 1 deletion dll/ApiHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ LRESULT CALLBACK Hook_TscShellContainerWndProc(HWND hWnd, UINT uMsg, WPARAM wPar

CMsRdpExInstance* instance = MsRdpEx_InstanceManager_FindByTscShellContainerWnd(hWnd);

//MsRdpEx_LogPrint(DEBUG, "TscShellContainerWnd: %s (%d)", MsRdpEx_GetWindowMessageName(uMsg), uMsg);

result = Real_TscShellContainerWndProc(hWnd, uMsg, wParam, lParam);

if (instance)
Expand Down Expand Up @@ -724,7 +726,7 @@ LRESULT CALLBACK Hook_IHWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
AppendMenu(hExtraMenu, MF_STRING, SYSMENU_RDP_SEND_CTRL_ALT_END_ID, L"Send Ctrl+Alt+End");

AppendMenu(hSystemMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSystemMenu, MF_POPUP, (UINT_PTR)hExtraMenu, L"Extra");
AppendMenu(hSystemMenu, MF_POPUP, (::UINT_PTR)hExtraMenu, L"Extra");

instance->AttachTscShellContainerWindow(hParentWnd);
}
Expand Down Expand Up @@ -1245,6 +1247,41 @@ LSTATUS Hook_RegCloseKey(HKEY hKey)
return lstatus;
}

typedef BOOL(WINAPI* Func_DeleteMenu)(HMENU hMenu, UINT uPosition, UINT uFlags);

static Func_DeleteMenu Real_DeleteMenu = DeleteMenu;

BOOL Hook_DeleteMenu(HMENU hMenu, UINT uPosition, UINT uFlags)
{
BOOL success = TRUE;
BOOL skipDelete = FALSE;

if (!(uFlags & MF_BYPOSITION) && MsRdpEx_IsAddressInRdpExeModule(_ReturnAddress()))
{
MENUITEMINFOA mii = { 0 };
mii.cbSize = sizeof(MENUITEMINFOA);
mii.fMask = MIIM_ID | MIIM_STRING;
char itemName[256] = { 0 };
mii.dwTypeData = itemName;
mii.cch = sizeof(itemName);

if (GetMenuItemInfoA(hMenu, uPosition, FALSE, &mii))
{
if ((mii.wID == 0) && (mii.cch == 0)) {
MsRdpEx_LogPrint(DEBUG, "DeleteMenu hMenu: %p uPosition: 0x%04X uFlags 0x%04X", hMenu, uPosition, uFlags);
MsRdpEx_LogPrint(DEBUG, "Skipping deletion of menu item ID: 0x%04X, name: %s, cch: %d", mii.wID, itemName, mii.cch);
skipDelete = TRUE; // work around bug where separator menu items are deleted by CContainerWnd::SyncClipboardMenu()
}
}
}

if (!skipDelete) {
success = Real_DeleteMenu(hMenu, uPosition, uFlags);
}

return success;
}

bool MsRdpEx_IsAddressInModule(PVOID pAddress, LPCTSTR pszModule)
{
bool result;
Expand Down Expand Up @@ -1296,6 +1333,12 @@ bool MsRdpEx_IsAddressInRdclientAxModule(PVOID pAddress)
return MsRdpEx_IsAddressInModule(pAddress, L"rdclientax.dll");
}

bool MsRdpEx_IsAddressInRdpExeModule(PVOID pAddress)
{
return MsRdpEx_IsAddressInModule(pAddress, L"mstsc.exe") ||
MsRdpEx_IsAddressInModule(pAddress, L"msrdc.exe");
}

void MsRdpEx_GlobalInit()
{
MsRdpEx_NameResolver_Get();
Expand Down Expand Up @@ -1368,6 +1411,8 @@ LONG MsRdpEx_AttachHooks()
//MSRDPEX_DETOUR_ATTACH(Real_RegOpenKeyExW, Hook_RegOpenKeyExW);
//MSRDPEX_DETOUR_ATTACH(Real_RegQueryValueExW, Hook_RegQueryValueExW);
//MSRDPEX_DETOUR_ATTACH(Real_RegCloseKey, Hook_RegCloseKey);

MSRDPEX_DETOUR_ATTACH(Real_DeleteMenu, Hook_DeleteMenu);

MsRdpEx_AttachSspiHooks();
error = DetourTransactionCommit();
Expand Down Expand Up @@ -1425,6 +1470,8 @@ LONG MsRdpEx_DetachHooks()
//MSRDPEX_DETOUR_DETACH(Real_RegOpenKeyExW, Hook_RegOpenKeyExW);
//MSRDPEX_DETOUR_DETACH(Real_RegQueryValueExW, Hook_RegQueryValueExW);
//MSRDPEX_DETOUR_DETACH(Real_RegCloseKey, Hook_RegCloseKey);

MSRDPEX_DETOUR_DETACH(Real_DeleteMenu, Hook_DeleteMenu);

MsRdpEx_DetachSspiHooks();
error = DetourTransactionCommit();
Expand Down
1 change: 1 addition & 0 deletions dll/MsRdpEx.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ bool MsRdpEx_IsAddressInModule(PVOID pAddress, LPCTSTR pszModule);
bool MsRdpEx_IsAddressInRdpAxModule(PVOID pAddress);
bool MsRdpEx_IsAddressInMstscAxModule(PVOID pAddress);
bool MsRdpEx_IsAddressInRdclientAxModule(PVOID pAddress);
bool MsRdpEx_IsAddressInRdpExeModule(PVOID pAddress);

LONG MsRdpEx_GetRectWidth(LPRECT rect);
LONG MsRdpEx_GetRectHeight(LPRECT rect);
Expand Down

0 comments on commit 1a5975c

Please sign in to comment.