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

    Unhappy Call MFC DLL from normal C++ app. and load a UI from it

    Hi all,

    I create a MFC DLL "Static" and with wizard I add a dialog and "Microsoft web browser" ActiveX in this dialog, also I create a class for this Dialog.

    All I want here to call this Dll from a Win32 app "normal c++" and load this Dialog into the Main UI, But I got an Assertion error.

    Snippet of Code:

    //////////////
    // DLL
    //////////////
    //.h
    class CShofhaMFCStaticApp : public CWinApp
    {
    public:
    CShofhaMFCStaticApp();
    void CreateBrowser(HWND hWnd);

    // Overrides
    public:
    virtual BOOL InitInstance();
    DECLARE_MESSAGE_MAP()

    };
    extern "C" __declspec(dllexport) void CreateBrowserEx(HWND hWnd);
    //.cpp
    ShofhaBrowser *m_pMyDlg;
    extern "C" __declspec(dllexport) void CreateBrowserEx(HWND hWnd);
    CShofhaMFCStaticApp* thisDLL;
    __declspec(dllexport) void CreateBrowserEx(HWND hWnd)
    {
    thisDLL->CreateBrowser(hWnd);
    }
    CShofhaMFCStaticApp::CShofhaMFCStaticApp()
    {
    thisDLL = this;
    m_pMyDlg = NULL;
    }
    class tempRoutingFrame {

    CFrameWnd* m_pFrame ;

    public:

    tempRoutingFrame(CFrameWnd * pWnd= NULL)
    {
    // Save current value
    m_pFrame = AfxGetThreadState()->m_pRoutingFrame;
    // Set to value passed in. NULL by default.
    AfxGetThreadState()->m_pRoutingFrame = pWnd;
    }
    ~tempRoutingFrame()
    {
    // Restore m_pRoutingFrame to original value.
    AfxGetThreadState()->m_pRoutingFrame = m_pFrame;
    }

    };

    void CShofhaMFCStaticApp::CreateBrowser(HWND hWnd)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    if(m_pMyDlg == NULL)
    {
    CWnd *pParent = CWnd::FromHandle(hWnd);
    m_pMyDlg = new ShofhaBrowser(pParent);
    m_pMyDlg->Create(IDD_MAIN, pParent);
    m_pMyDlg->ShowWindow(SW_SHOW);
    }
    }
    //and The dialog
    class ShofhaBrowser : public CDialog
    {
    DECLARE_DYNAMIC(ShofhaBrowser)

    public:
    ShofhaBrowser(CWnd* pParent = NULL); // standard constructor
    virtual ~ShofhaBrowser();

    virtual void OnFinalRelease();

    // Dialog Data
    enum { IDD = IDD_MAIN };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

    DECLARE_MESSAGE_MAP()
    DECLARE_DISPATCH_MAP()
    DECLARE_INTERFACE_MAP()
    public:
    CExplorer1 m_browser;
    };
    //////////////
    // Exe
    //////////////
    void CMFCtestApp::OnAppAbout()
    {
    /* get handle to dll */
    HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("..\\ShofhaMFCStatic.dll"));

    /* get pointer to the function in the dll*/
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"CreateBrowserEx");

    /*
    Define the Function in the DLL for reuse. This is just prototyping the dll's function.
    A mock of it. Use "stdcall" for maximum compatibility.
    */
    typedef BOOL (__stdcall * pICFUNC)(HWND);

    pICFUNC MyFunction;
    MyFunction = pICFUNC(lpfnGetProcessID);

    HWND fWindow; // Window handle of the window you want to get

    fWindow = FindWindow(NULL, _T("Window_title")); // Find the window

    ///* The actual call to the function contained in the dll */
    BOOL intMyReturnVal = MyFunction(fWindow);

    /* Release the Dll */
    FreeLibrary(hGetProcIDDLL);
    }

    Please Help
    Thanks in advance

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

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    1. Where did you get an "Assertion error"?
    2. Please, edit your post adding Code tags around code snippet. Otherwise your code is very hard to read/understand. See Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    Thanks for your replay

    My Assertion in "winocc.cpp" in:

    Code:
    BOOL CWnd::ShowWindow(int nCmdShow)
    {
    	ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
    
    	if (m_pCtrlSite == NULL)
    		return ::ShowWindow(m_hWnd, nCmdShow);
    	else
    		return m_pCtrlSite->ShowWindow(nCmdShow);
    }
    and bellow my Snippet of Code with code format:

    Code:
    //////////////
    // DLL
    //////////////
    //.h
    class CShofhaMFCStaticApp : public CWinApp
    {
    public:
    	CShofhaMFCStaticApp();
    	void CreateBrowser(HWND hWnd);
    
    // Overrides
    public:
    	virtual BOOL InitInstance();
    	DECLARE_MESSAGE_MAP()
    
    };
    extern "C" __declspec(dllexport) void CreateBrowserEx(HWND hWnd);
    //.cpp
    ShofhaBrowser *m_pMyDlg;
    extern "C" __declspec(dllexport) void CreateBrowserEx(HWND hWnd);
    CShofhaMFCStaticApp* thisDLL;
    __declspec(dllexport) void CreateBrowserEx(HWND hWnd)
    {
    	thisDLL->CreateBrowser(hWnd);
    }
    CShofhaMFCStaticApp::CShofhaMFCStaticApp()
    {
    	thisDLL = this;
    	m_pMyDlg = NULL;
    }
    class tempRoutingFrame {
    
       CFrameWnd* m_pFrame ;
    
    public:
    
       tempRoutingFrame(CFrameWnd * pWnd= NULL)
       {
          // Save current value
          m_pFrame = AfxGetThreadState()->m_pRoutingFrame;
          // Set to value passed in. NULL by default.
          AfxGetThreadState()->m_pRoutingFrame = pWnd;
       }
       ~tempRoutingFrame()
       {
          // Restore m_pRoutingFrame to original value.
          AfxGetThreadState()->m_pRoutingFrame = m_pFrame;
       }
    
    };
    
    void CShofhaMFCStaticApp::CreateBrowser(HWND hWnd)
    {
        AFX_MANAGE_STATE(AfxGetStaticModuleState());
        
        if(m_pMyDlg == NULL)
        { 
            CWnd *pParent = CWnd::FromHandle(hWnd);
            m_pMyDlg = new ShofhaBrowser(pParent);
            m_pMyDlg->Create(IDD_MAIN, pParent); 
            m_pMyDlg->ShowWindow(SW_SHOW); 
        }
    }
    //and The dialog
    class ShofhaBrowser : public CDialog
    {
    	DECLARE_DYNAMIC(ShofhaBrowser)
    
    public:
    	ShofhaBrowser(CWnd* pParent = NULL);   // standard constructor
    	virtual ~ShofhaBrowser();
    
    	virtual void OnFinalRelease();
    
    // Dialog Data
    	enum { IDD = IDD_MAIN };
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    	DECLARE_MESSAGE_MAP()
    	DECLARE_DISPATCH_MAP()
    	DECLARE_INTERFACE_MAP()
    public:
    	CExplorer1 m_browser;
    };
    //////////////
    // Exe
    //////////////
    void CMFCtestApp::OnAppAbout()
    {
    	   /* get handle to dll */ 
       HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("..\\ShofhaMFCStatic.dll")); 
    
       /* get pointer to the function in the dll*/ 
       FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"CreateBrowserEx"); 
    
       /* 
          Define the Function in the DLL for reuse. This is just prototyping the dll's function. 
          A mock of it. Use "stdcall" for maximum compatibility. 
       */ 
       typedef BOOL (__stdcall * pICFUNC)(HWND); 
    
       pICFUNC MyFunction; 
       MyFunction = pICFUNC(lpfnGetProcessID); 
    
       HWND fWindow; // Window handle of the window you want to get
    
       fWindow = FindWindow(NULL, _T("Window_title")); // Find the window
    
       ///* The actual call to the function contained in the dll */ 
       BOOL intMyReturnVal = MyFunction(fWindow); 
    
       /* Release the Dll */ 
       FreeLibrary(hGetProcIDDLL);
    }
    Thanks in advance

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

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    And where from in *your code* is this ShowWindow called?

    Besides:
    Quote Originally Posted by walaa_23
    Code:
        if(m_pMyDlg == NULL)
        { 
            CWnd *pParent = CWnd::FromHandle(hWnd);
            m_pMyDlg = new ShofhaBrowser(pParent);
            m_pMyDlg->Create(IDD_MAIN, pParent); 
            m_pMyDlg->ShowWindow(SW_SHOW); 
        }
    You don't check the result of m_pMyDlg->Create(...)
    But why are you sure that it will always succeed?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2010
    Posts
    3

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    No I don't use ShowWindow in any other place.

    and I checked for the create success

    Code:
            if(m_pMyDlg->Create(IDD_MAIN, pParent))
    		{
    			m_pMyDlg->ShowWindow(SW_SHOW); 
    		}
    and It is failed in creation step, so I tried DoModal as below and gives "-1"

    Code:
        if(m_pMyDlg == NULL)
        { 
            CWnd *pParent = CWnd::FromHandle(hWnd);
            m_pMyDlg = new ShofhaBrowser(pParent);
    		int res = m_pMyDlg->DoModal();
        }
    Thanks in Advance

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

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    Quote Originally Posted by walaa_23 View Post
    No I don't use ShowWindow in any other place.
    So it is called "indirectly".
    Look at the Call Stack window and try to find out the last place in your code before this assertion in CWnd::ShowWindow fails.
    Victor Nijegorodov

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    In your 'normal C++' app are you enabling MFC support?

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    I'd like to take a look at CShofhaMFCStaticApp::InitInstance. Something makes me think the problem is there. As long as the dialog fails to create, I can suppose AfxOleInit omission, or similar issue.

    PS. It would be easier to review the entire project instead of guessing about the oddly selected fragments.
    Best regards,
    Igor

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Call MFC DLL from normal C++ app. and load a UI from it

    Code:
    extern "C" __declspec(dllexport) void CreateBrowserEx(HWND hWnd);
    . . .
       typedef BOOL (__stdcall * pICFUNC)(HWND);
    I'm getting a bit tired of this "beginner's dynamic loading" stuff...
    Last edited by Igor Vartanov; April 28th, 2010 at 07:19 AM.
    Best regards,
    Igor

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