Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

*** If you want a challenge, here it is *** Command routing nightmare

Ah hello.

I have a serious issue that is causing major problems.

I have a command handler for a range of IDs on a menu:

ON_COMMAND_RANGE(ID_VIEW_FILTER, MAX_COMMAND_RANGE,  OnViewFilterByPack)

with

#define MAX_COMMAND_RANGE ID_VIEW_FILTER + 31 + NUMBER_OF_ADDITIONAL_ITEMS
#define NUMBER_OF_ADDITIONAL_ITEMS 4

#define ID_VIEW_FILTER                              30000

I have a tree control on my app.  Whenever I complete a drag drop operation on the tree (drag to correct place and release left mouse button), I get a message generated from somewhere that calls OnViewFilterByPack.  This message has ID 30035; obviously falling into the range for this handler.  Now, if I change the value of ID_VIEW_FILTER to 40000, this message has ID 40035.  This in itself might be a clue.

My call stack is (easier to read in Notepad with word wrap off)

>      MyApp.exe!CMyView::OnViewFilterByPack(unsigned int nID=30035)  Line 2025      C++
       mfc71d.dll!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x00c5b1a8, unsigned int nID=30035, int nCode=0, void (void)* pfn=0x0043f8d4, void * pExtra=0x00000000, unsigned int nSig=55, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 103      C++
       mfc71d.dll!CCmdTarget::OnCmdMsg(unsigned int nID=30035, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 396 + 0x27      C++
       mfc71d.dll!CView::OnCmdMsg(unsigned int nID=30035, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 160 + 0x18      C++
       MyApp.exe!CMyView::OnCmdMsg(unsigned int nID=30035, int nCode=0, void * pNewData=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 1151      C++
       mfc71d.dll!CWnd::OnCommand(unsigned int wParam=30035, long lParam=0)  Line 2550      C++
       mfc71d.dll!CWnd::OnWndMsg(unsigned int message=273, unsigned int wParam=30035, long lParam=0, long * pResult=0x0012fcf0)  Line 1759 + 0x1c      C++
       mfc71d.dll!CWnd::WindowProc(unsigned int message=273, unsigned int wParam=30035, long lParam=0)  Line 1745 + 0x1e      C++
       mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00c5b1a8, HWND__ * hWnd=0x006c0c0e, unsigned int nMsg=273, unsigned int wParam=30035, long lParam=0)  Line 241 + 0x1a      C++
       mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x006c0c0e, unsigned int nMsg=273, unsigned int wParam=30035, long lParam=0)  Line 389      C++
       mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x006c0c0e, unsigned int nMsg=273, unsigned int wParam=30035, long lParam=0)  Line 209 + 0x15      C++
       USER32.DLL!77e3a420()       
       USER32.DLL!77e14605()       
       mfc71d.dll!CWnd::AttachControlSite(CHandleMap * pMap=0x00c58008)  Line 445 + 0x16      C++
       mfc71d.dll!CWnd::FromHandle(HWND__ * hWnd=0x002f0d36)  Line 311      C++
       mfc71d.dll!CWnd::GetTopLevelParent()  Line 2623      C++
       00007553()      
       USER32.DLL!77e15b77()       
       mfc71d.dll!AfxInternalPumpMessage()  Line 188      C++
       mfc71d.dll!CWinThread::PumpMessage()  Line 916      C++
       mfc71d.dll!CWinThread::Run()  Line 637 + 0xb      C++
       mfc71d.dll!CWinApp::Run()  Line 701      C++
       mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00142db5, int nCmdShow=1)  Line 49 + 0xb      C++
       MyApp.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00142db5, int nCmdShow=1)  Line 25      C++
       MyApp.exe!WinMainCRTStartup()  Line 390 + 0x39      C
       KERNEL32.DLL!7c59893d()       
       mfc71d.dll!CRichEditView::OnKeyDown(unsigned int nChar=, unsigned int nRepCnt=, unsigned int nFlags=)  Line 1030 + 0x8      C++


I cannot see at all where this message is coming from, and am pretty desperate to sort this out ASAP.  The confusing thing is it is generated by the drag drop-LButton up operation.

Does anyone have any ideas on this ?

TIA
Avatar of mrwad99
mrwad99
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

More fiddling has revealed the following:

with ID ID_VIEW_FILTER as 30001 instead of 30000, OnViewFilterByPack gets called but with a parameter of 30036

If I change

#define NUMBER_OF_ADDITIONAL_ITEMS 4

to

#define NUMBER_OF_ADDITIONAL_ITEMS 3

I have no problems.

So what ****is**** the problem ?
Avatar of AlexFM
AlexFM

Override OnCommand function in the window which receives this message, and get additional information about message source there. See CWnd::OnCommand MSDN topic for details.
BTW, last line of your call stack looks strange:

     MyApp.exe!WinMainCRTStartup()  Line 390 + 0x39     C
      KERNEL32.DLL!7c59893d()      
      mfc71d.dll!CRichEditView::OnKeyDown(unsigned int nChar=, unsigned int nRepCnt=, unsigned int nFlags=)  Line 1030 + 0x8     C++

What does CRichEditView::OnKeyDown do here?? Possibly you have some idea knowng your application context.
Avatar of AndyAinscow
Clarify please.
Are you saying
#define NUMBER_OF_ADDITIONAL_ITEMS 4
will result in the COMMAND message but
#define NUMBER_OF_ADDITIONAL_ITEMS 3
does not?
Is the define MAX_COMMAND_RANGE used anywhere else apart from for this ON_COMMAND_RANGE message map entry?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Thanks for the reply AlexFM.

>> What does CRichEditView::OnKeyDown do here?? Possibly you have some idea knowng your application context.

I dont have a rich edit view in my application.  The application is an MDI app with a splitter pane split into three.  Two of the views are treeviews, one is a fileview.  Essentially Windows Explorer with an additional treeview on the left hand side.

From MSDN:

"The low-order word of wParam identifies the command ID of the menu item, control, or accelerator. The high-order word of wParam specifies the notification message if the message is from a control. If the message is from an accelerator, the high-order word is 1. If the message is from a menu, the high-order word is 0."

Right.  I breakpoint on OnCommand in the view that handles this message (view that contains OnViewFilterByPack) and when

LOWORD(wParam) == 30036 (the message getting passed to OnViewFilterByPack)
HIWORD(wParam) = 0, so the message is from a menu
lParam is 0, so it is not a control that sent the message.

But how on earth is the menu sending the message ?  I have not clicked it !
Does it happen in release build?
Avatar of mrwad99

ASKER

Hi Andy

Yes, it does happen in release build.

<Trying to sort this by gradually changing old code to have the new changes.  Make changes, it breaks.  Undo exact same changes, it still breaks.  Nice.>

ARGHHHHHHHHHHHHH...........................
And my first three comments?
Can you find ID_VIEW_FILTER value which doesn't produce his effect?
Avatar of mrwad99

ASKER

Dammit.

I had just typed a big response there when I decided to double check on where I was using ID_VIEW_FILTER in my code.

And guess what.

I  have a diagnostic function, called from within OnLButtonUp, that does something wonderful.  Yes, it does a

::PostMessage(this->m_hWnd, WM_COMMAND, ID_VIEW_FILTER + 35,0);

which should have been commented out long ago.

This explains what I am seeing.

<blushes bigstyle>

So, I have just wasted some of your time (sorry) and a lot of my time.  And even had a bit of a haircut for free (well, decreased the need for a haircut, if you get me).

Oh well.

But this has raised interesting issues.

1) mfc71d.dll!CRichEditView::OnKeyDown.....

why on earth was that being called when I don't have a CRichEditView in my app ?

2) Is there any way I could have determined from the call stack, or any other way, that OnViewFilterByPack was being called as a result of a PostMessage (grrrrr!) *from within one of my diagnostic functions ?*  'Cause if there was, and I would have known about it, this whole issue would not have come about.

Sorry again for being dumb :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No problems.

1) ? - no idea
2) Not that I know of, especially as the call stack 'thought' it was coming from a richedit view
Avatar of mrwad99

ASKER

Right, thanks for that.  Andy's comment was the closest to an answer as that indirectly hinted at me searching for ID_VIEW_FILTER which gave the answer.

Thanks again :)
I agree with Alex re "Search in Files", hence my first set of three comments.

I have a rule which is roughly - Don't assume this is the problem, check first.   (Many years ago I spent ages attempting to improve the peformance of some code,  nothing I did helped.  Eventually I found >90% of the time it used was in one API call, so I quickly rewrote part of the app so as to not depend on that call - it then took about 20% of the original time.  I wished I had checked first)