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.  
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
Yes you can do

Create the tool bar with the hWnd like


Or try CToolbarCtrl

Good Luck
TaitorAuthor Commented:

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.

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

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
            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.  
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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.

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
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?  
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.
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.  

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TaitorAuthor Commented:

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


Each application creates its own toolbar.  This is generally done in the CMainFrame class.
-- Dan
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.