Wednesday, September 17, 2014

Post Message



Places (posts) 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.
Syntax:
BOOL WINAPI PostMessage(

  opt_  HWND hWnd,

        UINT Msg,

        WPARAM wParam,

        LPARAM lParam

);


Parameters

hWnd [in, optional]
Type: HWND

A handle to the window whose window procedure is to receive the message. The following values have special meanings.
HWND_BROADCAST : The message is posted to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows. The message is not posted to child windows.
NULL: The function behaves like a call to "PostThreadMessage" with the dwThreadId parameter set to the identifier of the current thread.

Msg [in]
Type: UINT
The message to be posted.

wParam [in]
Type: WPARAM
Additional message-specific information.
lParam [in]
Type: LPARAM
Additional message-specific information.




Source:MSDN

Send Message


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

Syntax :
LRESULT WINAPI SendMessage(

    HWND hWnd,

    UINT Msg,

    WPARAM wParam,

    LPARAM lParam

);



Parameters:

hWnd [in]
Type: HWND
A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.

Msg [in]:
Type: UINT
The message to be sent.

wParam [in]
Type: WPARAM
Additional message-specific information.

lParam [in]
Type: LPARAM
Additional message-specific information.



Source:MSDN

Virtual table or vTable



The virtual table is a look-up table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes called by other names, such as “vtable”, “virtual function table”, or “virtual method table”.


Every class that uses virtual functions is given its own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.


The compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. 




Ref:Learncpp.com

Monday, August 25, 2014

Synchronous and Asynchronous sockets.

Whenever we perform some operation on a socket, it may not be able to complete immediately and return control back to your program.
For example:
                  A read on a socket cannot complete until some data has been sent by the remote host.If there is no data waiting to be read, one of the two things can happen,
  • The function can wait until some data has been written on the socket, OR  
  •  It can return immediately with an error that indicates that there is no data to be read.
The first case is called "Synchronous or blocking socket".
The program is "blocked" until the request for the data has been satisfied. When the remote system does write some data on the socket, the read operation will complete and execution of the program will resume. 

The second case is called an "Asynchronous or non-blocking socket", and requires that the application recognize the error condition and handle the situation properly.

Wednesday, August 20, 2014

Copy Constructor and Assignment operator

The assignment operator is used to copy the values from one object to another "already existing object".

Eg:    classA objectone(5); //Calls classA constructor.
         classA objectTwo;    //Calls classA default constructor.
         objectTwo = objectone; // Calls classA assignment operator.

  • In this case,objectTwo has already created by the time assignment is executed.consequently the classA assignment operator is called.
If the object is being copied into does not exist then "copy constructor" comes into existence.

Eg:
       ClassA object1(5) ; //calls classA constructor,
       ClassA object2 = object1;//Calls classA's copy constructor.

A "Copy constructor " is a special constructor that initializes a new object from an existing object.

  • The assignment operator copies to existing objects,and the copy constructor copies to newly created objects..
  • If a new object has to be created before copying can occur,the copy constructor is used.
Where we need "Copy constructor" : 
  • When instantiating one object and initializing it with values from another object.
  • When passing an object by value.
  • when an object is returned from a function by value. 

Thursday, August 14, 2014

MFC Threads.


MFC distinguishes between two types of threads.
  • User interface(UI) Threads and 
  • Worked Threads.
The difference between the two is that UI threads have message loops and worker threads do not.UI threads can create windows and process messages sent to those windows.

Worker threads perform background tasks that receive no direct input from the user and therefore don't need windows and message loops.

Creating worker thread: 
The best way to launch a thread in an MFC Application is to call AfxBeginThread.
MFC defines two different versions of AfxBeginThread. One that starts a UI thread and another starts a worked thread.
AfxBeginThread creates a new CWinThread object launches a thread and ttachesa it to the CWinThread object and returns CWinThread pointer.

Syntax:
CWinthread * pThread = AfxBeginThread(Threadfunc,&threadInfo)

Above statement starts a worker thread and passes it to the application-defined data structure(&threadInfo) that contains input to the thread. Threadfunc  is the thread function the function that gets executed when the thread itself begin to execute.

Simple Thread function:
                                UINT Threadfunc(LPVOID pParam)
                                {
                                     UINT nIterations = (UINT)pParam;
                                     for(UINT i=0; i < nIterations ; i++)
                                        return 0;
                                 }

AfxBeginThread accepts as many as four additional parameters that specify the threads priority,stack-size,
Creation flags and security attributes.The complete function prototype is as follows.

   CWinThread * AfxBeginThread(AFX_THREADPROC pfnThreadProc,
                                                     LPVOID pParam,
                                                     int nPriority = THREAD_PRIORITY_NORMAL,
                                                     UINT nStackSize = 0,
                                                     DWORD dwCreateFlags = 0,
                                                     LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL)

  • nPriority specifies the threads execution priority.High-priority threads are scheduled for CPU time before low-priority threads. The default is THREAD_PRIORITY_NORMAL which assigns the same priority as the process that owns it.We can change thread's priority level at any time with CWinThread::SetThreadPriority.

  • The nstackSize parameter passed to AfxBeginThread specifies the threads maximum stack size. the 0 default nStackSize value allows the stack to grow as large as 1MB.
  • dwCreateFlags can be one of two values.The default value 0 tells the system to start executing the thread immediately.If CREATE_SUSPENDED is specified instead,the thread starts out in a suspended state and doesn't begin running until another thread calls CWinthread::ResumeThread on the suspended thread.

  • lpSecurityAttrs, is a pointer to a SECURITY_ATTRIBUTES structure that specifies the new threads security attributes and also tells the system whether child process should inherit the thread handle.The NULL default value assigns the new thread the same properties the thread that created it.

The Thread Function:

A thread function is a callback function,so it must be either a static class member function or a global function declared outside a class.It is prototyped this way " UINT ThreadFunc(LPVOID pParam)".

pParam is a 32-bit value whose value equals the pParam passed to AfxBeginThread.

Wednesday, July 9, 2014

What is Typedef

We can define new data-type names using the keyword "typedef".
You are not actually creating a new data-type,rather defining a new name for an existing type.This process can help make machine dependent programs more portable.

General form of the typedef statement is
"typedef "type" Newname;
Ex: typedef float balance;

This statement tells the compiler to recognize the balance as another name for float.Next you could create a float variable using balance.

Ex: balance over_due;

Here over_due is floating point variable of the type balance,which is another word for float.

String Table

A string table is a Windows resource that contains a list of IDs, values, and captions for all the strings of your application. For exa...