Attaching a CToolbar to a CWin pointer from an HWND that was created in Visual Basic 6

Sorry if the title is a little confusing.  I thought I would hit the keywords at least.  Using a C++ dll created in Microsoft Visual Studio 6, I would like to be able to attach a toolbar to a HWND object that is being passed in from a Visual Basic 6 application.  Is this possible?  Any code would be greatly appreciated if this is possible.  
Thanks.
TaitorAsked:
Who is Participating?
 
DanRollinsCommented:
In that case, It's a snap.
Just make a DLL that exports a class.  Have the class be an object derived from CToolBar.  Write all of the customizing code that you want.

Set the EXE to link to the .LIB that represents the DLL.  In the EXE source, replace the cases in the CMainFrame (or whereever) that normally create and use a CToolBar to instead refer to your customized derived toolbar.

-- Dan
0
 
Roshan DavisCommented:
Yes you can do

Create the tool bar with the hWnd like

m_wndToolbar.Create(CWnd::FromHandle(hVBDlgWnd),---)

Or try CToolbarCtrl

Good Luck
0
 
TaitorAuthor Commented:
roshmon,

when I try implementing the suggested code for creating the toolbar, I get an error when the borders for the toolbar are being set.  

---------------------------
Microsoft Visual C++
---------------------------
Unhandled exception in Tester2.exe (MFC42D.DLL): 0xC0000005: Access Violation.
---------------------------
OK  
---------------------------


CControlBar::SetBorders(int 0, int 0, int 0, int 0) line 86 + 6 bytes

The specific line that it seems to error on is in when the call is made to set m_cxLeftBorder = cxLeft;  cxLeft = 0.  
      
Is this something I can resolve or should I try going with CToolbarCtrl?   Thanks

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
TaitorAuthor Commented:
I have increased the points because I am determined to get this to work.  Here is my rountine that creates the toolbar in the dll.

CBasicToolbarApp::AddToolbarsToHWND(HWND vbHwnd)
{
      
      if (!m_wndToolBar.CreateEx(CWnd::FromHandle(vbHwnd), TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
      {
            TRACE0("Failed to create toolbar\n");
            return -1;      // fail to create
      }
      
      return 0;
}

The code in other application calls this routine and passes the forms hWnd property.  

Thanks in advance.  
0
 
DanRollinsCommented:
I think that you will need to go with CToolBarCtrl becuas CToolBar probably assumes that it is being placed onto a CFrameWnd-derived window.  For instance, it contains the logic to automatically resize itself based upon messages and/or expected variables of the parent.

Another note:
There may be a problem with ther LoadToolBar() fn becasue the code might look into the wrong resource file.

Finally,
I am not at all confident that the scheme will work.  The toolbar typically sends WM_COMMAND message to the parent window which then knows how to handle them.  I have no idea if that will hold together when the parent window is managed by the VB-generated code.

Why don't you just use the toolbar that is fully-supported by VB?

-- Dan
0
 
TaitorAuthor Commented:
How about I just start over with the question, Does anyone know how to create a CToolBar in a c++ dll that will show up in any kind of window of a c++ exe that calls the dll?  
0
 
DanRollinsCommented:
Does the targetted C++ EXE *know about* the toolbar?  If not, you need to kludge a system of floating a toolbar window over the EXE's window and then work out how to handle the clicks.  It's not gunna happen automatically -- it's like asking "Can put a toilet in my office?" without thinking about the pumbing...

Unless of course, the EXE provides a snap-in capability, as does IE.
0
 
TaitorAuthor Commented:
The C++ exe will be aware of the toolbar.  I was kind of hoping that the event handling would happen on the dll side.  I understand that it is not real common or easy do and that is why I am asking the experts.  

Thanks.
0
 
TaitorAuthor Commented:
Dan,

Where is the toolbar suppose to actually be created?  Is the toolbar getting a window from the calling application?  

Thanks,

Tait
0
 
DanRollinsCommented:
Each application creates its own toolbar.  This is generally done in the CMainFrame class.
-- Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.