-
April 26th, 2010, 04:40 AM
#1
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
-
April 26th, 2010, 05:38 AM
#2
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
-
April 26th, 2010, 07:25 AM
#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
-
April 26th, 2010, 07:44 AM
#4
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:
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
-
April 26th, 2010, 09:01 AM
#5
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
-
April 26th, 2010, 09:11 AM
#6
Re: Call MFC DLL from normal C++ app. and load a UI from it
Originally Posted by walaa_23
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
-
April 27th, 2010, 07:02 PM
#7
Re: Call MFC DLL from normal C++ app. and load a UI from it
In your 'normal C++' app are you enabling MFC support?
-
April 28th, 2010, 07:06 AM
#8
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
-
April 28th, 2010, 07:13 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|