Wednesday, October 13, 2010

How Windows 7/Vista Manage Flicker Free Drawing?

Tip – How Windows 7/Vista Manage Flicker Free Drawing?

Details – The prior version of Windows (Windows XP and 2000 doesn’t support native double buffering for drawing operations. This may cause flickering in the applications it’s drawing intensive.
In one of the previous tips we already have seen how to enable Double buffering. WS_EX_COMSPOSITED style can be used for avoiding flickering of Windows in Windows 2000 and XP. But most of the developers are using MemDC based methods for drawing. The content need to be drawn in to a bitmap and directly Blt to screen using BitBly operation, which is extremely fast and avoid flickering caused by frequent drawing operations.

Basic Details
In Windows 7, the Desktop Window Manager (DWM) manages all the Windows running in the system. DWM uses GPU available in the system. When Windows Aero is enabled, the entire desktop area is a Direct3D surface. All drawing operations are made by applications are sent directly to the offline buffer for rendering. DWM will swap this buffer to screen whenever required. Thus by default all Window operations are done through double buffering. This architecture is quite different in prior versions of Windows, where all the drawing operations are redirected to the main screen(buffer).
Even DWM handles buffered drawing, we can’t completely make sure drawings are flicker free. Microsoft has introduced buffered drawing APIs since Windows Vista. This shall be covered as an another tip. Please check this blog post, if you want to know more about double buffering and double buffering helpers in Windows Vista .
 
More DWM
To explain a bit more detailed, the Window itself being mapped as texture on the screen, the transitions on minimize maximize are handling using GPU shaders. The shaders are small programs which can control the coordinates and pixels being displayed. Since the desktop itself a 3D surface, DWM has access to all the Windows displayed on the screen. This it can effortlessly implement the new features like, aero peak, flip 3D etc. The double buffering is also provided by DWM during Window movement, repainting etc. for flicker free drawing.
The DWM double buffering methods are sometimes not compatible with real-time graphics applications and games. In this case, DWM will not do window compositing so the application can gain the desired performance and no overheads of DWM implemented double buffering. DWM also employs efficient rendering techniques called culling to avoid displaying the Windows which are not visible in the screen (minimized, small windows hidden behind other windows etc).

Posted by - Sarath C

Monday, October 11, 2010

How to Debug a child process?

Tip – Demonstrates how to debug a child process while debugging the parent process?

Details – Usually we use Visual Studio (of any version) to debug the a process. This can be done either by open the project and start debugging or attach a running process to Visual Studio. What if the debuggee (the process being debugged) is spawning another process which you may also interested to debug? Visual Studio doesn’t support this feature and we can make this happen through WinDBG. Usually developers call DebugBreak or make some purposeful exceptions to attach to debugger. But some early exceptions during the startup are hard to catch using this method.

Steps for debugging.

1.    Launch parent process and attach to WinDBG
2.    The execution of the attached process will be stopped and the command Window will be enabled.
3.    Enter “.childdbg 1” in command Window.
4.    Now press F5 or enter “g” command to continue operation.
5.    Whenever parent process creates the child process, it will break the execution and you can put break points or whatsoever you want to do with child process.

Below screenshots are taken by attaching beyond compare to WinDBG and just start notepad.exe from it. (This is just for the sake of an example)


NB: While debugging with WinDBG, it’s necessary to set properly symbols and source file for smooth debugging. Otherwise debugging could be a mess. For more help on debugging multiple process and processor context switch, please see the help file associated with WinDBG.

PS: We can enable a simple trick to use Visual Studio for Child Process Debugging. Create a new key under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\ and provide the debugger name as string value under this key. For Sample.exe, it may appear as below.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sample.exe]
"Debugger"="vsjitdebugger.exe"

But this is not really a good method. It only a simple workaround.

Posted by - Sarath C

Folder existence checking and delete a directory tree that is not empty

Tip - Folder existence checking and delete a directory tree that is not empty

Details - _waccess function determines whether the specified file or directory exists and the mode support like read only or Read-write.


In order to delete the directory which is not empty we can't use RemoveDirectory, because it only works on empty directories/folders. But can use SHFileOperation for this.



Posted by -Sobhita Mercy E.

Wednesday, October 6, 2010

Detect memory leaks in our program.

Tip -  How can we detect memory leaks in our program.

Details -  MFC provides a structure CMemoryState to detect memory leaks in our program. This diagnostics method only help to detect memory leaks caused when memory allocated using the new operator is not deallocated using delete.

Reference -
http://msdn.microsoft.com/en-us/library/0wzsd007(VS.80).aspx
http://msdn.microsoft.com/en-us/library/2ads32e2(VS.80).aspx



Posted by - Sobhita Mercy E

Terminate all the instances of a Process through code

Tip -  How to Terminate all the instances of a Process through code

Details -  By the below shared lines of codes one can terminate all the instances of a process. CreateToolhelp32Snapshot API is used to take the snapshot of all the processes and threads in the system. The process which is to be terminated is identified from the snapshot using the Process Name. Then the process object is accessed using OpenProcess API and terminated using TerminateProcess(). The CreateToolhelp32Snapshot API not only takes a snapshot of the processes, but also the heaps, modules, and threads used by the processes.



Posted by - Sobhita Mercy E

Monday, October 4, 2010

Prefer CreateFile API with FILE_FLAG_DELETE_ON_CLOSE for temporary files

Tip - Prefer CreateFile API with FILE_FLAG_DELETE_ON_CLOSE for temporary files.

Detail - The CreateFile API call accepts several flags which affect how a file is created or opened. One of these flags, FILE_FLAG_DELETE_ON_CLOSE, specifies that the file will be deleted when the last handle to it is closed. A common activity for applications is to create temporary files. If the application terminates abnormally, these files may not be deleted, potentially wasting of disk space. But if CreateFile API is called with FILE_FLAG_DELETE_ON_CLOSE that deletes the file when all handles to it are closed, which will be perfect for temporary file scenarios.

Reference -http://msdn2.microsoft.com/en-us/library/aa363858.aspx


Posted by - Sobhita Mercy E

Sunday, October 3, 2010

How to take Call graphs in VC6


Tip -  How to take Call graphs in VC6

Details - Position the cursor over any function name and hit ALT+F12. If your project was built with browse-information, a dialog will appear asking what you want to do. Select CallGraph to view a hierarchy of all sub-functions which are called by your function. This is a very useful feature often overlooked in Visual Studio. The below showed dialog is of source browser which will appear when we hit ALT+F12. The same can also taken from Menu->Tools->Source Browser.


It has several modes of operation other than Call Graph or Caller Graph which will be useful.


Posted by - Sobhita Mercy E