Friday, September 11, 2009

[VC++] Catching exception with debugger

You may some times feel crazy about some exceptions in your application. You have no idea where it is happening in the code and what you can do now is debugging the suspicious location in the issue happening scenario. Let me welcome you to a better approach with your VC++ debugger, so that you will get a better context of the issue causing exception. Follow the steps,

1. Debug your application (Either start with debugger or attach your process with the debugger).
2. In the VC 6 debugger choose Debug->Exceptions then you can see a dialog with various exception types.
3. Click and select all the Exceptions, now press OK.
4. Run your scenario.
5. When an exception happen a message box will be shown in the debugger and execution will break.
6. Now you can get the call stack where exception happens. For this check the Context combo box in the debugger IDE.
7. Now select each location in the call stack and interpret the cause (you can get the value of the variables).

Tuesday, September 8, 2009

Change Drive letters

Here is how to change or swap drive letter of the disk drives.

1. Take MyComputer->Manage
2. In the management console select Storage->Disk Management
3. Now Add... , Change... or Remove drive letter.
4. For swapping drive letters between disk drives first remove drive letters of both the drives and then select the needed letters one after another.

Blocking websites in your local machine

Do you want to restrict some of the sites to open up in your machine ? You can do with some browsers but same can be easily find out by others. Here is another way for blocking sites in the local machine.

1. Take C:\Windows\system32\drivers\etc
2. Open file hosts in C:\Windows\system32\drivers\etc
3. Add a new line to the hosts specifying the website name.

For example if you want to block www.youtube.com and www.orkut.com please add the below lines to the file hosts
127.0.0.1 www.youtube.com
127.0.0.1 www.orkut.com

You may need to restart your browser for making it effect.

Renaming file/folder with empty name or funny symbols

You may have tried renaming a file/folder with empty name but windows will never allow it. Here is a way to rename your folder.

1. Right click the file/folder and select Properties.
2. In the Properties dialog take General tab and delete the current name.
3. Keep the cursor in the name box, Press down Alt key and press 255 (from number pad)
4. Now press OK, selected file/folders name will be changed to empty.
5. If you use other combination such as 256, 257 etc with Alt key it will result in creating funny symbols as name of the file/folder.

I tell you there is a blank icon available with WINDOWS for setting blank icon for a folder. In the Customize tab, click Change Icon... and you can select empty icon and press Ok.

Friday, September 4, 2009

[Utility][WinDbg] !htrace for debugging handle leak

Windbg Debugger’s !htrace extension is very good at debugging handle leaks. See the below steps how you can do it.

1. Install Debugging Tool for windows from here.
2. Run WinDbg installed with Debugging Tools in Step 1.
3. Take File->Attach to a process... menu and select your application in the process list
4. Application will break and change in to debug mode
5. Take File->Symbol File path and add the path where your application's pdb file is available
6. In the debug prompt enter !htrace -enable and press enter.
7. Now take Debug->Go menu, then you can operate on your application
8. Now run your scenario where your application is assumed to have handle leak.
9. Now take Debug->Break menu, for changing the application to debug mode.
10. Now enter !htrace -diff in the debug prompt and press enter. Here it list the handle leak logs.

Below is an example of the log generated for 4 opened handle in my test app. From the log you can see where exactly the handle is leaked in the code.
0:001>
!htrace -diff
Handle tracing information snapshot successfully taken.
0x26 new stack traces since the previous snapshot.
Ignoring handles that were already closed...
Outstanding handles opened since the previous snapshot:
--------------------------------------
Handle = 0x0000070c - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
*** WARNING: Unable to verify checksum for D:\TestFolder\MiscTest\Release\MiscTest.exe
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000710 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000714 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Handle = 0x00000718 - OPEN
Thread ID = 0x00001758, Process ID = 0x00001248

0x7c8308eb: kernel32!CreateEventA+0x00000068
0x0040172b: MiscTest!CMiscTestDlg::OnButtonHandle+0x0000001a
0x73dd24c0: MFC42!_AfxDispatchCmdMsg+0x00000082
0x73dd23bf: MFC42!CCmdTarget::OnCmdMsg+0x0000010a
0x73e3dead: MFC42!CPropertySheet::OnCmdMsg+0x0000001d
0x73dd3244: MFC42!CWnd::OnCommand+0x00000053
0x73dd1bf1: MFC42!CWnd::OnWndMsg+0x0000002f
0x73dd1b9b: MFC42!CWnd::WindowProc+0x00000024
0x73dd1b05: MFC42!AfxCallWndProc+0x00000091
0x73dd1a58: MFC42!AfxWndProc+0x00000036
0x73e6847d: MFC42!AfxWndProcBase+0x00000039
--------------------------------------
Displayed 0x4 stack traces for outstanding handles opened since the previous snapshot.