void CCoolMenu::Clear() { // ToDo: Determine if it still crashes inside Windows Shell SetSite() function SafeRelease( m_pContextMenuCache ); SetWatermark( NULL ); EnableHook( false ); }
bool CFidgetApp::Initialize(HINSTANCE hInstance, int nCmdShow) { m_hInstance = hInstance; TIME_Initialize(); init_timer(); SeedRandomNumberGenerator(); if (!EnableHook()) { return false; } m_hRichEdit = ::LoadLibrary(L"Msftedit.dll"); if (NULL != m_hRichEdit) { m_bMsftEdit = true; } else { m_hRichEdit = ::LoadLibrary(L"Riched20.dll"); if (NULL != m_hRichEdit) { m_bMsftEdit = false; } else { return false; } } m_pMainFrame = new (std::nothrow) CMainFrame; if ( NULL == m_pMainFrame || !RegisterClasses()) { return false; } int xSize = ::GetSystemMetrics(SM_CXSCREEN); int ySize = ::GetSystemMetrics(SM_CYSCREEN); int cx = (9*xSize)/10; int cy = (9*ySize)/10; int x = (xSize - cx)/2; int y = (ySize - cy)/2; if (!m_pMainFrame->Create(x, y, cx, cy)) { UnregisterClasses(); return false; } m_pMainFrame->ShowWindow(nCmdShow); m_pMainFrame->UpdateWindow(); return true; }
void CCoolMenu::EnableHook() { ASSERT( m_hMsgHook == NULL ); ASSERT( m_bUnhook == FALSE ); m_bEnable = IsModernVersion(); if ( ! m_bEnable ) return; m_bUnhook = TRUE; EnableHook( TRUE ); }
BOOL CHookAPI::HookAPI(LPCTSTR pstrDllName,LPCSTR pstrFuncName,FARPROC pfnNewFunc,HOOKSTRUCT& HookInfo) { HMODULE hModule=LoadLibrary(pstrDllName); //纪录函数地址 HookInfo.pfnFuncAddr=GetProcAddress(hModule,pstrFuncName); FreeLibrary(hModule); if(HookInfo.pfnFuncAddr==NULL) return FALSE; //备份原函数的前5个字节,一般的WIN32 API以__stdcall声明的API理论上都可以这样进行HOOK memcpy(HookInfo.OldCode,HookInfo.pfnFuncAddr,5); HookInfo.NewCode[0]=0xe9;//构造JMP DWORD dwJmpAddr=(DWORD)pfnNewFunc-(DWORD)HookInfo.pfnFuncAddr-5;//计算JMP地址 memcpy(&HookInfo.NewCode[1],&dwJmpAddr,4); EnableHook(HookInfo,TRUE);//开始进行HOOK return TRUE; }
CCoolMenu::~CCoolMenu() { SetWatermark( NULL ); if ( m_bUnhook ) EnableHook( FALSE ); }
//------------------------------------------------------------------------- MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget) { return EnableHook(pTarget, FALSE); }
//------------------------------------------------------------------------- MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget) { return EnableHook(pTarget, TRUE); }
BOOL CCoolMenu::AddMenu(CMenu* pMenu, BOOL bChild) { EnableHook(); if ( ! Settings.Interface.CoolMenuEnable ) return FALSE; for ( int i = 0 ; i < (int)pMenu->GetMenuItemCount() ; i++ ) { TCHAR szBuffer[128] = {}; CString strText; MENUITEMINFO mii = {}; mii.cbSize = sizeof( mii ); mii.fMask = MIIM_DATA|MIIM_ID|MIIM_FTYPE|MIIM_STRING|MIIM_SUBMENU; mii.dwTypeData = szBuffer; mii.cch = sizeof( szBuffer ) / sizeof( TCHAR ) - 1; GetMenuItemInfo( pMenu->GetSafeHmenu(), i, TRUE, &mii ); if ( mii.wID >= ID_SHELL_MENU_MIN && mii.wID <= ID_SHELL_MENU_MAX ) break; // Bypass shell menu items // Non-XML parsed menu items const int nItemID = pMenu->GetMenuItemID( i ); if ( nItemID == ID_SEARCH_FILTER || nItemID == -1 && ! m_sFilterString.IsEmpty() && m_sFilterString == szBuffer ) { CResultFilters* pResultFilters = new CResultFilters; pResultFilters->Load(); if ( nItemID > 0 ) { m_sOldFilterString = szBuffer; m_sFilterString = szBuffer; m_sFilterString.TrimRight( L".\x2026" ); } else if ( pResultFilters->m_nFilters == 0 ) { CMenu* pSubMenu = pMenu->GetSubMenu( i ); if ( pSubMenu ) pSubMenu->DestroyMenu(); mii.hSubMenu = NULL; mii.wID = ID_SEARCH_FILTER; m_sFilterString = m_sOldFilterString; SetMenuItemInfo( pMenu->GetSafeHmenu(), i, TRUE, &mii ); } if ( pResultFilters->m_nFilters ) { HMENU pFilters = CreatePopupMenu(); DWORD nDefaultFilter = pResultFilters->m_nDefault; for ( DWORD nFilter = 0 ; nFilter < pResultFilters->m_nFilters ; nFilter++ ) { AppendMenu( pFilters, MF_STRING|( nFilter == nDefaultFilter ? MF_CHECKED : 0 ), 3000 + nFilter, pResultFilters->m_pFilters[ nFilter ]->m_sName ); } ReplaceMenuText( pMenu, i, &mii, m_sFilterString.GetBuffer() ); mii.hSubMenu = pFilters; mii.fMask |= MIIM_SUBMENU; strText = m_sFilterString; } else { ReplaceMenuText( pMenu, i, &mii, m_sOldFilterString.GetBuffer() ); mii.hSubMenu = NULL; mii.fMask ^= MIIM_SUBMENU; strText = m_sOldFilterString; } delete pResultFilters; } if ( mii.fType & (MF_OWNERDRAW|MF_SEPARATOR) ) { mii.fType |= MF_OWNERDRAW; if ( mii.fType & MF_SEPARATOR ) mii.dwItemData = 0; SetMenuItemInfo( pMenu->GetSafeHmenu(), i, TRUE, &mii ); continue; } mii.fType |= MF_OWNERDRAW; mii.dwItemData = ( (DWORD_PTR)pMenu->GetSafeHmenu() << 16 ) | ( mii.wID & 0xFFFF ); if ( strText.IsEmpty() ) strText = szBuffer; m_pStrings.SetAt( mii.dwItemData, strText ); if ( bChild ) SetMenuItemInfo( pMenu->GetSafeHmenu(), i, TRUE, &mii ); if ( mii.hSubMenu != NULL ) AddMenu( pMenu->GetSubMenu( i ), TRUE ); } return TRUE; }