- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Message Categories
- Message Handling in MFC
- Message Dispatching
- Message Handling with ClassWizard
- Creating Your Own Message Maps
- A Common Problem with MFC Message Maps
- Summary
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
A Common Problem with MFC Message Maps
The MFC message map mechanism is very flexible, and it's well integrated with tools such as MFC AppWizard and ClassWizard. However, a very common mistake made when working with message maps is to attempt to use a message-handling function with the wrong signature. Using a function that has the wrong signature will cause your application's stack to be corrupted—a problem that is very difficult to debug.
Parameters and return values are passed to and from functions on the stack. If the parameter and return value signature don't match the expected values, parts of the stack are overwritten or corrupted. These bugs can be especially difficult to find because they often appear only in Release builds. The fault is masked during Debug builds due to the way the stack frame is managed.
A common mistake is to confuse the signatures on functions used with the ON_COMMAND and ON_MESSAGE macros. Remember that the ON_COMMAND macro expects to use a function with this signature:
afx_msg void OnFileNew();
The ON_MESSAGE macro expects to see a function with this signature:
afx_msg LRESULT OnMyUserMessage(WPARAM wParam, LPARAM lParam);
You might think that the C++ compiler would catch errors like this—but it cannot, due to the use of an old-style C++ cast in the ON_COMMAND and similar message handler macros. Knowledge base article Q195032 (available on your MSDN CD-ROM) shows how you can redefine the message handler macros to be type-safe.
Summary | Next Section

Account Sign In
View your cart