Click here to Skip to main content
14,836,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
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:
C++
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    // Remove this if you use lpReserved
    UNREFERENCED_PARAMETER(lpReserved);
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        TRACE0("IMenuMfc.DLL Initializing!\n");
		
	// Extension DLL one-time initialization
	if (!AfxInitExtensionModule(IMenuMfcDLL, hInstance))
		return 0;
	// Insert this DLL into the resource chain
	// NOTE: If this Extension DLL is being implicitly linked to by
	//  an MFC Regular DLL (such as an ActiveX Control)
	//  instead of an MFC application, then you will want to
	//  remove this line from DllMain and put it in a separate
	//  function exported from this Extension DLL.  The Regular DLL
	//  that uses this Extension DLL should then explicitly call that
	//  function to initialize this Extension DLL.  Otherwise,
	//  the CDynLinkLibrary object will not be attached to the
	//  Regular DLL's resource chain, and serious problems will
	//  result.
	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);
	//DrawMenuBar(g_hWnd);

	g_oldProc=(WNDPROC)SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)MyWndProc);

    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
	TRACE0("IMenuMfc.DLL Terminating!\n");
	// Terminate the library before destructors are called
	AfxTermExtensionModule(IMenuMfcDLL);
	SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)g_oldProc);
    }
    return 1;   // ok
}
Posted
Updated 25-Jun-10 2:25am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900