Wednesday 3 August 2011

MFC Interview Questions & Answers - 3


Que: What is the difference between GetMessage and PeekMessage ?

Ans: GetMessage function waits for a message to be placed in the queue before returning where as PeekMessage function does not wait for a message to be placed in the queue before returning.



Que: What’s the difference between PostMessage and SendMessage?

Ans: The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message.



The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message.



Que: How to create a model less dialog?

Ans: m_pModeless is a variable of type CDialog or any of its descendants.

m_pModeless->Create(IDD_DIALOG1, this);

m_pModeless->ShowWindow(SW_SHOW); 

this pointer as a paramter suggest we are creating a child dialog of the current dialog/window.



Que: How to setup a timer?

Ans: Use the SetTimer function

UINT_PTR SetTimer( HWND hWnd, UINT_PTR nIDEvent, UINT uElapse, 

TIMERPROC lpTimerFunc );

To kill the timer use

BOOL KillTimer(int nIDEvent);

Where the nIDEvent is the ID returned by the SetTimer Function.



Que: Name the Synchronization objects ?

Ans: Following are the synchronization objects 

1) Critical Section 2) Event 3) Mutex 4) Semaphore

Classes provided for above synchronization objects are:

1) CCriticalSection 2) CEvent 3) CMutex 4) CSemaphore



Que: What Is CMutex ?

Ans: An object of class CMutex represents a “mutex” — a synchronization object that allows one thread mutually exclusive access to a resource. Mutexes are useful when only one thread at a time can be allowed to modify data or some other controlled resource. For example, adding nodes to a linked list is a process that should only be allowed by one thread at a time. By using a CMutex object to control the linked list, only one thread at a time can gain access to the list.

To use a CMutex object, construct the CMutex object when it is needed. Specify the name of the mutex you wish to wait on, and that your application should initially own it. You can then access the mutex when the constructor returns. Call CSyncObject::Unlock when you are done accessing the controlled resource.



An alternative method for using CMutex objects is to add a variable of type CMutex as a data member to the class you wish to control. During construction of the controlled object, call the constructor of the CMutex data member specifying if the mutex is initially owned, the name of the mutex (if it will be used across process boundaries), and desired security attributes.



To access resources controlled by CMutex objects in this manner, first create a variable of either type CSingleLock or type CMultiLock in your resource’s access member function. Then call the lock object’s Lock member function (for example, CSingleLock::Lock). At this point, your thread will either gain access to the resource, wait for the resource to be released and gain access, or wait for the resource to be released and time out, failing to gain access to the resource. In any case, your resource has been accessed in a thread-safe manner. To release the resource, use the lock object’s Unlock member function (for example, CSingleLock::Unlock), or allow the lock object to fall out of scope



Que: What is thread & process?

Ans: Threads are similar to processes, but differ in the way that they share resources. Threads are distinguished from processes in that processes are typically independent, carry considerable state information and have separate address spaces. Threads typically share the memory belonging to their parent process.



Que: what is the use of AFX_MANAGE_STATE ?

Ans: By default, MFC uses the resource handle of the main application to load the resource template. If you have an exported function in a DLL, such as one that launches a dialog box in the DLL, this template is actually stored in the DLL module. You need to switch the module state for the correct handle to be used. You can do this by adding the following code to the beginning of the function:



AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope. If all your resources lies in the single DLL you can even change the default handle to the DLL handle with the help of AfxSetResourceHandle function.

1 comments:

Unknown said...

Hi There,


So bloody thorough! Ah! So happy and blessed out! I feel redeemed by reading out MFC Interview Questions & Answers - 3 for All Keep up the good work!

how to resolve these conflicting data types in C# and C.I need to send string data
from my Graphic User Interface to the microcontroller which was programmed in C.Below are the lines of code.

CODE OBJECTIVE: To use the slider(track bar) in Visual Studio Express to vary the brightness of Leds
C# codes

But nice Article Mate! Great Information! Keep up the good work!


Regards,
Hansy

Post a Comment

Popular Posts

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls