Saturday, April 19, 2008

MFC Interview Question

Microsoft Technologies Interview Questions[MFC Interview Questions]

Can you explain the Document-View architecture in MFC ?
The document view architecture was introduced in MFC 2.0. In this architecture the application’s data is represented as a “document” object and the views of this data are represented as “view” objects. E.g. If you create an excel document what you see to get your job done i.e. cells, values, etc comprises the view and when you save this data it gets saved as a document. The various views of this document is as a table, graphs, bar charts, etc. The data is stored in a document object. The class representing the document object is CDocument. The sole purpose of this class is to manage an application’s data. Views server two purposes: renders visual representation of data stored in document and translate the user’s inputs into commands that operate on the data in the document. The class for view is CView. Thus the division of work in terms of data and view helps in work division, better productivity and reusability of code. There are two types of document/view applications:
SDI (Single Document Interface): This supports just a single open document at a time.
E.g. Wordpad, Notepad. If the user wants to open multiple documents then he has to open multiple instances of wordpad/notepad.
MDI (Multiple Document Interface): Supports multiple documents and multiple views of a document. An application is MDI if the user can open multiple documents in the same application instance.
E.g. Microsoft Word: The user can open multiple documents within a single instance of Ms Word.

Explain the difference between a modal and a modeless dialog box with an example of how to use them ?
A dialog box is a window that pops up in response to user action to inform the user with a message. E.g. if the user does not save the document and exists the application the window pops up asking if he wants to save the document. This window is the dialog box. Modal Dialog: A modal dialog box disables the window to which it is assigned until the user dismisses the dialog box. E.g. The dialog that gets displayed with “yes”/”no”/”cancel” when you try to exit MS Word without saving the document is a modal dialog box. A model dialog box is created by calling the doModal() function of the dialog class. This function does not return back control to the caller until the dialog is dismissed. E.g. CDialog dlg; dlg.doModal(); Modeless Dialog: A modeless dialog box is just the reverse of modal. It allows the user complete control of the application which owns it and the user can continue his work without closing the dialog. A modeless dialog box can be created using the Create () command of CDialog class. The Create() call returns as soon as the dialog box is created. Hence the control returns back to the caller and the user is able to continue using the application.

Which are the common dialogs in MFC and what is each used for ?
Common dialogs are those that appear so frequently in application programs and hence the have become a part of MFC library. MFC provides the common dialogs listed below: - CFileDialog: File Open and File Save As dialog - CPrintDialog: Print and Print Setup dialog - CPageSetupDialog: Page setup dialog - CFindReplaceDialog: File and replace dialog - CColorDialog: Color dialog box - CFontDialog: Dialog box to change font style, color, size, etc. A common dialog is invoked by filling in the fields of the data structure and then calling the doModal() of the dialog. When the dialog returns the user input values are populated in the respective fields of the data structure.E.g. Lets say we want to show the File Open dialog box. TCHAR filter [] = _T (“Text files (*.txt)*.txt”); //Specify a filer for the file type CFileDialog dlg(TRUE, _T(“txt”), _T(“*.txt”), OFN_FILEMUSTEXIST, filter); If(dlg.doModal() == IDOK){//Get the user selected file namefilename = dlg.GetFileName();}

What is GDI ?
GDI is an acronym for Graphics Device Interface. The GDI allows you to draw on your windows. It is a device independent output model in the sense that the graphics code that you write for drawing will work on any video output which has a Windows driver. In order to avoid one window interfering with another while drawing output on the screen GDI uses a mechanism called as device context to avoid this conflict. So when a window draws on a screen, printer or any other output device, it doesn’t output pixels directly on the device. Instead it draws to a logical surface represented by a “device context” (DC). A DC is a data structure that has all information that GDI needs to know. Before a Windows program draws anything on the screen it acquires a DC handle from the GDI and then passes this handle back to the GDI each time it calls the GDI output function. In MFC a DC encapsulates the GDI functions that a program uses to generate output. MFC’s CDC class wraps a Windows device context and the GDI functions into one package. CPaintDC and CClientDC are subclasses of CDC and represent the different types of device contexts that windows applications use. E.g. CDC* pDC = GetDC(); //do some drawing Release (pDC);

Which are the different ways of Thread Synchronization ?
In a multi threaded application each thread must coordinate its actions with other threads in the application. If two or more threads access a same resource then that resource needs to be synchronized so that all threads don’t try to modify it at the same time. Windows supports four types of synchronization objects that can be used to synchronize the actions performed by concurrently running multiple threads. - Critical Sections - Mutexes - Events - Semaphores Critical Section: These are used to serialize access to resources that are accessed by multiple threads. All the threads must belong to same process because critical sections won’t work across process boundaries. CCriticalSection is a class and CCriticalSection::Lock locks a critical section and CCriticalSection::UnLock unlocks it. E.g. CCriticalSection cs; //Thread T1 cs.Lock (); //access the shared resource cs.UnLock (); /Thread T2 cs.Lock (); /access the shared resource Cs.Unlock (); Mutexes: Serves the same purpose as critical sections but can be used to synchronize threads running in same process or across processes. CMutex is its corresponding class. Semaphores: They maintain resource counts representing the number of resources available. Locking a semaphore decrements resource count and unlocking increments it. A thread that tries to lock a semaphore with resource count 0 gets blocked until another thread unlocks the semaphore and increases the resource count. CSemaphore is a class representing semaphores.

What are property pages ? What are they used for ?
Property sheets are tabbed dialog boxes containing pages that the user can switch with mouse clicks. They are a part of common library of windows just like dialog and other controls. E.g. when a user adds a schedules task it walks him through a wizard which is nothing but a property sheet. The functionality of property sheets is encapsulated in MFC classes of CPropertySheet and CPropertyPage. CPropertyPage represents a page in a property sheet and is a subclass of CDialog. Like dialog boxes property sheets can be modal or modeless. Use CPropertySheet::DoModal for modal and CPropertySheet::Create for a modeless property sheet. There are four broad steps in creating a property page:
For each property page create a dialog template.
For the created dialog associate a class that derives from CPropertyPage.
Derive a property sheet class from CPropertySheet and create an object for each above created property pages.
Add the property page objects to the created property sheet using AddPage.
Call the doModal function of the property sheet to display it on the screen.


What are DLL’s and which are the different types of DLL’s ?
Dynamic link libraries (also called as DLL’s) are a very important part of windows. Most of the files associated with windows are either executables or DLL’s. Unlike executable which can be directly invokes, a DLL cannot be invoked directly. Instead a DLL is a set of files containing functions that can be called by other programs or other DLL’s to get the job done. Dynamic linking means the process that Windows uses to link a function call of one module to the actual function in the DLL. Linking is of two types: static and dynamic linking. Static Linking happens during program development time while dynamic linking happens at run time. One can create a DLL of resource only as well. DLL usually have an extension .DLL which is loaded by windows automatically when required however one can load DLL using LoadLibrary or LoadLibraryEx calls as well. The two types of DLL which one can make with MFC are:
Extension DLL: These DLL’s exposes variables and functions to the client application. It can expose C++ functions and C++ classes to be used in your application.
Regular DLL: MFC extension DLL’s can be used only with MFC applications. If you want a dll to be used by a wider range of applications then use a regular DLL.

No comments: