Hi all:
I've received a task to insert a menu to a specified process by inject a dll to target process, after inject my dll to target process succssfully and the menu displayed, but here comes the question, the submenu always gray(disabled). The problem is mainly on FrameWnd::m_bAutomMenuEnable filed, MFC CMainFrame class set this filed TRUE in default, that means any menu item without add a message handler will automatically disabled. I try to use following code:
CWnd* pMainWnd=CWnd::FromHandle(g_hWnd);
pFrameWnd=(CFrameWnd*)(pMainWnd);
pFrameWnd->m_bAutoMenuEnable=FALSE;
try to set the m_bAutoMenuEnable=FALSE. but it seemed does't worked well(the pFrameWnd is not NULL but all fields are not valid through debug). Is there any way to modify this filed or another way to add(register) a command handler in order to enable my menu item?
How can I Enable the sub menu item, Please help me. Also I want someone explain why CWnd* pMainWnd=CWnd::FromHandle(g_hWnd) can not work. Thanks
All code:
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("IMenuMfc.DLL Initializing!\n");
if (!AfxInitExtensionModule(IMenuMfcDLL, hInstance))
return 0;
new CDynLinkLibrary(IMenuMfcDLL);
HMENU hMenu=NULL;
HMENU hMyMenu=NULL;
HMENU hSubMenu=NULL;
DWORD dwPid=0;
CFrameWnd* pFrameWnd=NULL;
dwPid=GetCurrentProcessId();
g_hWnd=GetWndFromProcID(dwPid);
hMenu=::GetMenu(g_hWnd);
CWnd* pMainWnd=CWnd::FromHandle(g_hWnd);
pFrameWnd=(CFrameWnd*)(pMainWnd);
pFrameWnd->m_bAutoMenuEnable=FALSE;
hMenu=GetMenu(g_hWnd);
hMyMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU_OPTION));
hSubMenu=GetSubMenu(hMyMenu,0);
InsertMenu(hMenu,2,MF_STRING|MF_BYPOSITION|MF_POPUP|MF_ENABLED,(UINT)hSubMenu,L"Option");
hSubMenu=GetSubMenu(hMyMenu,1);
EnableMenuItem(hMyMenu,(UINT)hSubMenu,MF_BYCOMMAND|MF_ENABLED);
g_oldProc=(WNDPROC)SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)MyWndProc);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("IMenuMfc.DLL Terminating!\n");
AfxTermExtensionModule(IMenuMfcDLL);
SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)g_oldProc);
}
return 1; }