Aspiring programmers seeking entry-level positions in the C++/MFC domain often encounter a common hurdle interview questions. These questions delve into the intricacies of MFC assessing a candidate’s grasp of key concepts and problem-solving abilities. To help you ace your upcoming MFC interview we’ve compiled a comprehensive guide incorporating insights from two valuable resources
- VC++ Interview Questions | C++ and VC++ Tips (https://vcpptips.wordpress.com/)
- Interview Questions – Microsoft Foundation Class Library (MFC) (https://groups.google.com/g/microsoft.public.vc.mfc/c/O1aqALQnSV4/m/4CmKOeDd4fkJ)
Frequently Asked Questions (FAQs)
1. What is MFC?
MFC, or the Microsoft Foundation Class Library, is a C++ class library developed by Microsoft It provides an object-oriented wrapper around the Windows API, simplifying application development MFC also serves as an application framework, offering a structured approach to building Windows applications.
2 What is the principle base class in MFC?
The CObject class is the principal base class in MFC. Most MFC classes, directly or indirectly, inherit from CObject. It does important things like support serialization, give run-time class information, show object diagnostic output, and work with collection classes.
3 What is the difference between ASSERT and VERIFY?
ASSERT evaluates the expression only in debug mode. If the result is 0, it throws an exception, and the program terminates. In contrast, VERIFY evaluates the expression in both debug and release modes. If the result is 0, it throws an exception only in debug mode.
4. What is the difference between PostMessage and SendMessage?
PostMessage places a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message. On the other hand SendMessage sends the specified message to a window or windows and waits until the window procedure has processed the message before returning.
5. What is the difference between PeekMessage and GetMessage?
PeekMessage
allows you to examine a message queue during a lengthy operation. It’s similar to GetMessage
in that both check a message queue for a message matching the filter criteria and then copy the message to an MSG
structure. However, GetMessage
doesn’t return until a message matching the filter criteria is placed in the queue, while PeekMessage
returns immediately regardless of whether a message is in the queue.
6. What is the difference between hInstance and hWnd?
hWnd
is the window’s handle, defining the window within the operating system. hInstance
is the operating system’s handle for the program when it’s running.
7. What is the difference between Modal and Modeless Dialog?
A modal dialog box captures the message loop, while a modeless dialog doesn’t. You call DoModal
to create the dialog window and its controls for a modal dialog. If you wish to create a modeless dialog, call Create
in the constructor of your Cdialog
class.
8. What is the use of CCmdTarget?
CCmdTarget
is the base class for the MFC library’s message map architecture, which maps commands/messages to the member functions that handle them. Classes derived from this include CWnd
, CWinApp
, CFrameWnd
, CView
, and CDocument
.
9. What is the difference between hInstance and hPrevInstance in the WinMain function?
hInstance
contains the handle of the current instance, while hPrevInstance
contains the handle of the last instance. If only one instance is running, hPrevInstance
is NULL
.
10. Describe the Document/View architecture.
In MFC 1.0, an application had two main components: an application object representing the application itself and a window object representing the application’s window. The application object’s primary duty was to create a window, and the window in turn processed messages.
MFC 2.0 revolutionized Windows application development by introducing the document/view architecture. In this architecture, the application’s data is represented by a document object, and views of that data are represented by view objects. Documents and views work together to process user input and draw textual and graphical representations of the resulting data.
MFC’s CDocument
class is the base class for document objects, and CView
is the base class for view objects. The application’s main window, modeled in MFC’s CFrameWnd
and CMDIFrameWnd
classes, is no longer the focal point for message processing but serves primarily as a container for views, toolbars, status bars, and other user interface objects.
MFC supports two types of document/view applications:
- Single Document Interface (SDI): Supports only one open document at a time.
- Multiple Document Interface (MDI): Allows two or more documents to be open concurrently and also supports multiple views of a given document.
11. What is a document?
In a document/view application, data is stored in a document object. The document object is created when the framework instantiates a class derived from CDocument
. It is an abstract representation of a program’s data, drawing a clear boundary between how the data is stored and how it is presented to the user. That means the sole purpose of a document object is to manage an application’s data.
12. What is a view?
View objects exist for two purposes:
- To render visual representations of a document on the screen.
- To translate the user’s input—particularly mouse and keyboard messages—into commands that operate on the document’s data.
Thus, documents and views are tightly interrelated, and information flows between them in both directions.
13. Distinguish between Window’s OnPaint and View’s OnDraw overridable functions.
Window’s OnPaint
function executes corresponding to each WM_PAINT
message. But in the case of document-view architecture when a view receives a WM_PAINT
message, it invokes the view’s OnDraw
function. The framework fields the WM_PAINT
message, creates a CPaintDC
object, and calls the view’s OnDraw
function with a pointer to the CPaintDC
object.
The fact that the view doesn’t have to construct its own device context object is a minor convenience. The real reason the framework uses OnDraw
is so that the same code can be used for output to a window, for printing, and for print previewing. When a WM_PAINT
message arrives, the framework passes the view a pointer to a screen device context so that output will go to the window. When a document is printed, the framework calls the same OnDraw
function and passes it a pointer to a printer device context.
14. What is command routing?
One of the most remarkable features of the document/view architecture is that an application can handle command messages almost anywhere. Command messages is MFC’s term for the WM_COMMAND
messages that are generated when items are selected from menus, keyboard accelerators are pressed, and toolbar buttons are clicked. The frame window is the physical recipient of most command messages, but command messages can be handled in the view class, the document class, or even the application class by simply including entries for the messages you want to handle in the class’s message map. The flow of command messages from Active View to DefWindowProc
is known as command routine.
15. How does Message Map work in an MFC application?
The message map functionality in an MFC application works by the support of three macros: DECLARE_MESSAGE_MAP
, BEGIN_MESSAGE_MAP
, and END_MESSAGE_MAP
, and the WindowProc
function implementation.
MFC’s DECLARE_MESSAGE_MAP
macro adds three members to the class declaration:
- A private array of
AFX_MSGMAP_ENTRY
structures named_messageEntries
that contains information correlating messages and message handlers. - A static
AFX_MSGMAP
structure namedmessageMap
that contains a pointer to the class’s_messageEntries
array and a pointer to the base class’smessageMap
structure. - A virtual function named
GetMessageMap
that returnsmessageMap
‘s address.
BEGIN_MESSAGE_MAP
contains the implementation for the GetMessageMap
function and code to initialize the messageMap
structure. The macros that appear between BEGIN_MESSAGE_MAP
and END_MESSAGE_MAP
fill in the _messageEntries
array, and END_MESSAGE_MAP
marks the end of the array with a NULL
entry.
Thus, corresponding to each message, a class can traverse through its and base classes’ message map entries to find a handler until it reaches the DefWindowProc
.
16. What is the difference between thread and process?
In the Microsoft Win32 environment, every running application constitutes a process, and every process contains one or more threads of execution. A thread is a path of execution through a program’s code, plus a set of resources (stack, register state, and so on) assigned by the operating system