Tuesday, February 3, 2009

[VC++] Programmatically locking your machine

For locking your machine programmatically you can use the API LockWorkStation(). See the below code for locking a system.

if( !LockWorkStation())
{
// Failed to lock the machine
}

Please note that _WIN32_WINNT should be defined >= 0x0500


Monday, February 2, 2009

[VC++] Performance Tuning with Visual Studio Profiler (VC 6)

Performance tuning is analyzing and improving your application's performance. Visual studio provides a built in profiler for analyzing the performance (you can have a detailed list of time each function takes, hit counts etc so that you can figure out where is the performance bottle neck). Below is a brief overview of how to profile using VC 6 profiler.

Build your project for profiling
For function profiling

  • In the Visual Studio IDE take Project->Settings and it will display the Project Settings dialog box.
  • Select the Link tab of Project Settings dialog box.
  • Choose General in the Category drop-down list box.
  • Check Enable Profiling check box and press OK button.
  • Now build the project.
For line profiling

  • Enable the function profiling as explained above.
  • In the Link tab select the Generate Debug Info check box.
  • Select the C/C++ tab and select the category drop-down list box as General.
  • In the Debug Info drop-down list box, choose Program Database or Line Numbers Only.
  • Click OK and re-build the project
Running Profiler

Choose Build->Profile... from Visual Studio IDE menubar. In the Profile dialog displayed you can choose any of the available profiling types then application will run. After the operation quit the application and check the Visual Studio Output window. Following are different types of profiling,

  • Function Timing - The Function Timing option in the Profile dialog box profiles the current project, recording how many times each function was called (hit count) as well as how much time was spent in each function and called functions.
  • Function Coverage - profiling is useful for determining which sections of your code are not being executed. The profiler lists all profiled functions, with an asterisk (*) marking those that were executed.
  • Function Counting - records how many times each function was called (hit count).
  • Function Attribution - Function attribution records information about who called a particular function by taking a snapshot of the stack on each function call. See more details here.
  • Line Counting - records how many times each line was called (hit count). To start a line count profile, use a custom batch file that specifies the source module and the lines to profile.
  • Line Coverage - Line coverage profiling is useful for determining which sections of your code are not being executed. The profiler lists all profiled lines, with an asterisk (*) marking those that were executed.

Visual Studio 6.0 Profiler



Function Timing Profile sample output

        Func          Func+Child           Hit
Time % Time % Count Function
---------------------------------------------------------
53153.728 99.5 53314.866 99.8 56 CWinThread::PumpMessage(void) (mfc42.dll)
136.579 0.3 136.964 0.3 176 CWnd::DefWindowProcA(unsigned int,unsigned int,long) (mfc42.dll)
Function Coverage Profile sample output
Covered Function
----------------
* AfxEnableControlContainer(class COccManager *) (mfc42.dll)
* AfxFindResourceHandle(char const *,char const *) (mfc42.dll)
* AfxInitialize(int,unsigned long) (appmodul.obj)
. AfxMessageBox(char const *,unsigned int,unsigned int) (mfc42.dll)
Note:By default Profiler profiles the entire dlls loaded and all .obj files linked. We can restrict the same by specifying exclude for unwanted libraries and obj files to be profiled. For that what you need to do is take the Profile.ini from Visual Studio installed location,
Microsoft Visual Studio\VC98\Bin\Profile.ini
and add 'exclude:libraryname.lib' or 'ObjectFileName.obj'.
See more profiling tips here.

Sunday, February 1, 2009

[VC++] Preventing multiple instances of your application running at same time

Suppose you do not want to have more than one instance running at a time in your program then here is how can you do that. Use the below code at the begining of your application class' Initinstance() function. Now if you try to start a second copy it will show a message box saying "Application already running" and will exit.


HANDLE hMutex = 0;
hMutex = CreateMutex( 0, TRUE, "SomeUniqueNameHere" ));
if( ERROR_ALREADY_EXISTS == GetLastError())
{
CloseHandle( hMutex );
AfxMessageBox( "Application already running" );
return FALSE;
}

Idea is very simple, create a mutex with some name and if you try to create mutex with the same name (from same or any other application) then it will fail. Only thing you need to bother is of choosing a unique name for mutex !

[VC++] Getting your application's full file path name

For getting the path name to your application you can use GetModuleFileName() API. Below code snippet shows how to obtain the same and display the same in a messagebox !

TCHAR szModulePath[MAX_PATH];
GetModuleFileName( 0, szModulePath, MAX_PATH );
MessageBox( szModulePath );

[VC++] Preventing your dialog disappear on ESC/Enter key press

If you are new to MFC dialog applications one thing you find irritating will be your application getting close on pressing ESC key or Enter key ! For preventing this what you need to do is over ride virtual OnOK() and OnCancel() in your dialog class.

Add below code to your dialog class(say CMyTestDlg) header file for overriding OnOK() and OnCancel(),
virtual void OnOK();
virtual void OnCancel();


Add empty implementation for the declared functions,
void CMyTestDlg::OnOK()
{
}
void CMyTestDlg::OnCancel()

{
}

Then its done ! You don't need to worry any more about your dialog application disappearing on pressing ESC or Enter key.