CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2005
    Posts
    140

    Invoking a dialog from MFC dll.

    I have coded a function like below hoping that it will show a dialog on a key press. but for some reasons it is not showing up. This code is written inside a MFC Dll. Can you help me in pointing out the problem?

    The installhook is called from a mfc dialog. Also, when i give SetParent as foreground window it ends up in an assertion failure.

    Code:
    LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, 
                                LPARAM lParam)
    {
    	AFX_MANAGE_STATE(AfxGetModuleState());
    	AfxSetResourceHandle(AfxGetInstanceHandle());
    
        char ch;            
         if (HC_ACTION==nCode)
        {        
            if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100)) 
            {
               /
    			
    	 // HWND hWnd = GetForegroundWindow();
    		   //dlg.SetParent(CWnd::FromHandle(hWnd));
    		  
                  CMfcDialog dlg;
                 dlg.SetParent(CWnd::FromHandle(hWnd));
    		   dlg.DoModal();
    				
            }
        }
    
        LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
        return  RetVal;
    }
    
    extern "C" void __declspec(dllexport) installhook()
    {
      
    	hkb=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KeyboardProc,AfxGetInstanceHandle(),0);
      
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Invoking a dialog from MFC dll.

    Quote Originally Posted by reachb4 View Post
    Code:
    LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, 
                                LPARAM lParam)
    {
    	AFX_MANAGE_STATE(AfxGetModuleState());
    	AfxSetResourceHandle(AfxGetInstanceHandle());
    
        ...
    		  
                  CMfcDialog dlg;
                 dlg.SetParent(CWnd::FromHandle(hWnd));
    		   dlg.DoModal();
    				
         ...
        return  RetVal;
    }
    1. Well, you cannot SetParent for the CMfcDialog dlg; because this dlg is not a window yet!
    You should instead set a parent from within CMfcDialog::OnInitDialog method and pass parent handle as a parameter or member variable of CMfcDialog class.

    2. You are calling
    Code:
    AFX_MANAGE_STATE(AfxGetModuleState());
    from within LRESULT CALLBACK KeyboardProc(...). Is this pocedure in a MFC Regular dll?
    Last edited by VictorN; October 9th, 2010 at 11:52 AM.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2005
    Posts
    140

    Re: Invoking a dialog from MFC dll.

    Thank you so much for your help.

    I first tried normal win32 dll and secondly the MFC Dll. Neither proved a success.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Invoking a dialog from MFC dll.

    Quote Originally Posted by reachb4 View Post
    Thank you so much for your help.

    I first tried normal win32 dll and secondly the MFC Dll. Neither proved a success.
    You haven't answer my question:
    Quote Originally Posted by VictorN View Post
    ...
    2. You are calling
    Code:
    AFX_MANAGE_STATE(AfxGetModuleState());
    from within LRESULT CALLBACK KeyboardProc(...). Is this pocedure in a MFC Regular dll?
    Otherwise it is not clear why you are calling AFX_MANAGE_STATE(AfxGetModuleState()) there...
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2005
    Posts
    140

    Re: Invoking a dialog from MFC dll.

    Sorry,

    Yes it is in a MFC Regular Dll(MFC Shared).


    Thanks,
    Joe.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Invoking a dialog from MFC dll.

    Well, did you try to debug it? What does
    Code:
    	AfxSetResourceHandle(AfxGetInstanceHandle());
    return? Is it the dll instance where your CMfcDialog is defined?
    Last edited by VictorN; October 9th, 2010 at 12:00 PM.
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2005
    Posts
    140

    Re: Invoking a dialog from MFC dll.

    Nope, lpszcurrentname is the name of the Dialog application i created. Why is it so??


    Thanks and Regards,
    John.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Invoking a dialog from MFC dll.

    Quote Originally Posted by reachb4 View Post
    Nope, lpszcurrentname is the name of the Dialog application i created. Why is it so??
    Ask MSDN about it!
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2005
    Posts
    140

    Re: Invoking a dialog from MFC dll.

    Sure.. Thank you for your help so far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured