Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

    • 14,897

Archive for the ‘MFC’ Category

Event logging using event viewer..

Posted by kiraninbng on July 14, 2006

Event logging is a way for applications /operating system  to log important events.The event-logging service stores events from
various sources in a single collection called an event log.The Event Viewer enables you to view logs.

Below are the steps to create an entry in the event viewer,

1. Create a message file
Ex :
; /*
; This is a sample message file. It contains a comment block, followed by a
; header section, followed by messages in two languages.
; */

; // This is the header section.





; // The following are message definitions.
The is a sample message file.

2. Using message compiler compile it into a resource script file
Ex : mc -U 

3. Using resource compiler compile the rc file into a binary res file
Ex: rc -r Sample.rc

4. Create a reource only dll using the res file
Ex: link -dll -noentry Sample.res

5. Add a event source to registry,also put an entry for message file

-Create a EventMessageType value under MYAPPLICATION ,and set the value to the  message dll generated using step 4

6. Get the handle to the eventlog source in the registry
7. Log the event into the log
8. Release the handle

Sample code for steps 6 to 8

h = RegisterEventSource(NULL,”MYAPPLICATION”);           // Step 6

if (!ReportEvent(h,EVENTLOG_ERROR_TYPE,0,100,NULL,0,0,NULL,NULL))                // Step 7
   printf(“Could not report the event.”);

DeregisterEventSource(h); //Step 8

9. Read/open the log programatically or view it using eventvwr.
   -Run EventVwr and check the for the event entry

Posted in MFC | Leave a Comment »

Custom Draw and Owner draw

Posted by kiraninbng on July 6, 2006

Custom draw is a service provided by some of the common controls.Custom draw allows you to make minor changes to the way the items are displayed in the screen.
Currently, the following controls support custom draw functionality:

Header controls
List-view controls
Rebar controls
Toolbar controls
ToolTip controls
Trackbar controls
Tree-view controls

All common controls that support custom draw send NM_CUSTOMDRAW notification messages at specific points during drawing operations. These notifications
describe drawing operations that apply to the entire control as well as drawing operations specific to items within the control. Like many notification messages,
NM_CUSTOMDRAW notifications are sent as WM_NOTIFY messages.
Custom draw feature is somewhere in between the controls default drawing behaviour and the programs owner draw functionality

Owner draw feature allows the customizing the existing controls or lets drawing the control from the scratch.

Currently, the following controls support OwnerDraw functionality:

ListView controls
Static controls
Tab controls

Whenever the item must be drawn Windows sends the WM_DRAWITEM message to the window procedure of the item’s owner window. This message contains a pointer to
a DRAWITEMSTRUCT structure.The application can customize the controls by handling this message.

Posted in MFC | 5 Comments »

Deleting a NULL pointer..

Posted by kiraninbng on June 29, 2006

Whats wrong with the below code,

void CTestDelDlg::OnOK()
    CTry* pTry = NULL;
    delete pTry;

Nothing, except the function is pointless and without any logic.The point is that deleting null pointer will not throw an exception.If the pointer is NULL/0 delete assumes that allocation or new has failed.The C++ language guarantees that delete p will do nothing if p is equal to NULL.

The best way of deleting a pointer is to call delete first and set the pointer to NULL. delete. 

Ex :

void CTestDelDlg::OnOK()
    CTry* pTry = new CTry;
    delete pTry;
    pTry = NULL;
For more info on delete chk out

Posted in MFC | 1 Comment »

Works in debug..but not in release

Posted by kiraninbng on June 29, 2006

So Why does my program run in debug but not in release ? Well it could be for any of the following reasons

1.Variables are initalized in debug build but not in release
    Soln : Initialize every variable before using them in the code.
2. Assert statements are ignored in the release build
    Soln : Use verify instead of assert,verify behaves like assert in debug build and in release it will just execute the condition in the verify.Better explicitly handle the error conditions,it is good to not to rely on verify.

3. Give complete and correct declarations in the message map entries(basically avoid copy/paste,use class wizard).It can hurt real bad in the release version.

Posted in MFC | Leave a Comment »

Debug Vs Release

Posted by kiraninbng on June 29, 2006

Below points list some of the differences between Release and Debug builds,

1.Debug Information(Symbols) by default is generated only in Debug build

2.Debug Version by default does not use any optimizations

3.Memory management in the debug version is different from that of release.All the pointers and variables are initialized to some hex value and 0 respectively in the debug version.

Posted in MFC | Leave a Comment »

Initializing class members..

Posted by kiraninbng on June 28, 2006

Is there any difference between

      m_csText = _T(“”); // Assigning Empty string to m_csText CString variable  



The Answer is yes.The second example is sometimes the only way of initializing variables,used for const and reference variables.Apart from that in case of the first, before executing the code in the body of the constructor,all the variables needs to be initalized.Basically there is one intialization call by default and an assignment call in the body.The second case achieves the same with one initalization call.So it is a good practice to use intialization list whenever possible.

Posted in MFC | 1 Comment »

Setting the ReadOnly Style of the Edit control

Posted by kiraninbng on June 28, 2006

Couple of ways to change the readonly style of an edit control on the fly.

1. Send a EM_READONLY Message to the edit control with aprropriate wparam(true to set and false to remove) value.
2. Use Setreadonly method of edit control

Note : Modfiystyle for readonly style may not work for Edit control,so it is safer to use one of the above calls.

Posted in MFC | Leave a Comment »