fidodido
asked on
Loading Toolbars through a DLL
This is a little lengthy, but its very urgent for me...
WHAT I AM TRYING TO ACHIEVE:
I have a MFC application with a default toolbar. I want to load an extra toolbar when a user selects a menu item. This toolbar is created and loaded through a function implemented in a separate DLL. This DLL itself is dynamically loaded on selecting the menu item.
PROBLEM:
The toolbar is not being created. I get assertion failure on call pNewToolbar->Create().
CODE:
1. The menu item command handler:
void CMyView::OnViewNewtoolbar(
{
CFrameWnd *pMainFrameWnd = (CFrameWnd *)GetParent();
//Load the DLL
HINSTANCE hLib = LoadLibrary("D:\\tmp\\NewT
//function pointer declaration
BOOL (*CreateNewTB)(CFrameWnd *);
//Get the pointer to the function which creates toolbar
CreateNewTB =(BOOL (*)(CFrameWnd *))GetProcAddress(hLib,"Cr
//Call the function to create a new toolbar
CreateNewTB(pMainFrameWnd)
}
2. The CreateNewTB() function in NewToolBar.dll
BOOL CreateNewTB(CFrameWnd *pMainFrameWnd)
{
AFX_MANAGE_STATE(AfxGetSta
//Create toolbar object
CToolBar *pNewToolBar = new CToolBar;
pNewToolBar->Create(pMainF
pNewToolBar->LoadToolBar(I
pNewToolBar->EnableDocking
pMainFrameWnd->EnableDocki
pMainFrameWnd->DockControl
return TRUE;
}
OBSERVATION:
I found that functions like AfxGetMainWnd(), AfxGetInstanceHandle() fails in the Create() call by giving an assertion failure.
ANY SOLUTIONS/SUGGESTIONS ?
Thank you
Prashanth
ASKER
Hi galkin, thanks for your suggestion!
But I am not fully clear about the second suggestion you have given. Can you be more specific, like giving an example?
But I am not fully clear about the second suggestion you have given. Can you be more specific, like giving an example?
Try to change parameter passed to CreateNewTB from Cwnd* or CFrameWnd* to HWND and inside this function in DLL call CWnd::FromHandle passing HWND parameter. Not that class you get by such a way will differ from that you have in application. Only m_hWnd member will be the same so if you want to access data member from your application frame class you must use MFC extension DLL
ASKER
hi galkin,
I am still facing problems.
As you said I modified my application as follows.
CMyView :: OnNewToolBar()
{ .......
CreateNewTB(GetParent()->m _hWnd);
}
BOOL CreateNewTB(HWND hParent)
{
AFX_MANAGE_STATE(AfxGetSta ticModuleS tate( ));
//Create toolbar object
CToolBar *pNewToolBar = new CToolBar;
CFrameWnd *pParent = (CFrameWnd *)CWnd::FromHandle(hParent );
pNewToolBar->Create(pParen t);
pNewToolBar->LoadToolBar(I DR_TOOLBAR 1);
pNewToolBar->EnableDocking (CBRS_ALIG N_ANY);
.....
}
This doesn't work since EnableDocking() expect CFrameWnd as the parent of CToolBar which in this case is not, since FromHandle() returns CWnd object ! EnableDocking() gives ASSERTION FAILURE !
I tried another method:
BOOL CreateNewTB(HWND hParent)
{ .........
AFX_MANAGE_STATE(AfxGetSta ticModuleS tate( ));
//Create toolbar object
CToolBar *pNewToolBar = new CToolBar;
CFrameWnd *pParent = new CFrameWnd;
pParent->Attach(hParent);
pNewToolBar->Create(pParen t);
pNewToolBar->LoadToolBar(I DR_TOOLBAR 1);
pNewToolBar->EnableDocking (CBRS_ALIG N_ANY);
.......
}
In this case no assertion failures occurs but toolbar does not appear on the screen. Even I tried to call
pNewToolBar->ShowWindow(SW _SHOWNORMA L).
Please HELP!
I am still facing problems.
As you said I modified my application as follows.
CMyView :: OnNewToolBar()
{ .......
CreateNewTB(GetParent()->m
}
BOOL CreateNewTB(HWND hParent)
{
AFX_MANAGE_STATE(AfxGetSta
//Create toolbar object
CToolBar *pNewToolBar = new CToolBar;
CFrameWnd *pParent = (CFrameWnd *)CWnd::FromHandle(hParent
pNewToolBar->Create(pParen
pNewToolBar->LoadToolBar(I
pNewToolBar->EnableDocking
.....
}
This doesn't work since EnableDocking() expect CFrameWnd as the parent of CToolBar which in this case is not, since FromHandle() returns CWnd object ! EnableDocking() gives ASSERTION FAILURE !
I tried another method:
BOOL CreateNewTB(HWND hParent)
{ .........
AFX_MANAGE_STATE(AfxGetSta
//Create toolbar object
CToolBar *pNewToolBar = new CToolBar;
CFrameWnd *pParent = new CFrameWnd;
pParent->Attach(hParent);
pNewToolBar->Create(pParen
pNewToolBar->LoadToolBar(I
pNewToolBar->EnableDocking
.......
}
In this case no assertion failures occurs but toolbar does not appear on the screen. Even I tried to call
pNewToolBar->ShowWindow(SW
Please HELP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is GREAT! I converted it into extension DLL and it worked perfectly.
Thank you for such a speedy response and the patience you have shown for my repeated queries.
I am very happy and now its 11:50pm and I can go home !!!
Thank you for such a speedy response and the patience you have shown for my repeated queries.
I am very happy and now its 11:50pm and I can go home !!!
Also when loading resources you must set resource handle with AfxSetResourceHandle in MFC extension DLL and explicity specify resource handle when using shared DLL