Codeproject
How to Enable Password Mask in Editbox?
2![]()
User Authentication is common in windows application. Usually the password editbox is masked and won’t show the real password. But to to enable the password masking in Editbox?

Picture Courtesy – NoteBookForums.
![]()
You have to enable ES_PASSWORD style of editbox and have to call SetPasswordChar() to set the Password masking character. You can do it in CDialog::OnInitDialog(). See the code snippet below.
BOOL CRabbitDlg::OnInitDialog()
{
...
// Get the Edit by using CtrlID.
CEdit* pEdit = (CEdit*) GetDlgItem( IDC_EDIT_PASSWORD );
// Set the password char.
pEdit->SetPasswordChar( '*' );
// Now modify the style to enable ES_PASSWORD.
pEdit->ModifyStyle( 0, ES_PASSWORD );
return TRUE;
}
![]()
Unless I’m very mistaken, it was while setting up samba server in Linux, where i had an interesting incident related to password. I was asked to type the password and save it. When I took the dialog again, the displayed password length was different. I was confused. I retyped and saved it again and again. Very lately i came to know that its a trick to fool the people who try to guess the password by length.
![]()
Targeted Audience – Beginners.
How to Watch Variables in Binary by using Visual Studio Debugger?
2![]()
In visual studio you can watch variable values in different formats. For instance, for viewing in hex add this to watch window – var,x and for octal – var,o. But its a pity that visual studio doesn’t support displaying variables in binaries. So how can you watch the value of a variable in binary, with debugger?

Image Courtesy – Pixdaus.
![]()
Well, we have to utilize the special feature of visual studio debugger. Basically the watch window is not just a tool to display the variable value. It can evaluate and execute small code snippets as well. If you add a function call to the watch window, that function will be called when debugger refreshes the watch values. So, all you have to do is – Add a global function which accepts integer value, converts it to binary string then display it to output window. Have a look at the function.
// Global function which converts integer to binary
// and dump to the output window.
void DumpBinary( DWORD Value )
{
// Buffer to hold the converted string.
TCHAR Buffer[255] = { 0 };
// Convert the value to binary string.
_itot( Value, Buffer, 2 );
// Display to output window.
CString csMessage;
csMessage.Format( _T("\n%d in binary: %s"), Value, Buffer );
OutputDebugString( csMessage );
}
Now if you want to convert the 100 to binary, break at some location and just add DumpBinary(100) to watch window and check the output in the output window. See the screenshot below.

![]()
Just now I realized – how powerful the Visual Studio Debugger is. Hats off to Visual Studio Team!
Well, please note that in visual studio 2008 and may be in siblings, When you add this to watch window, its possible to see this error – “CXX0001: Error: error attempting to execute user function”. In that case just click the “Evaluate button” which appears next to it and the expression will be re-evaluate.
![]()
Targeted Audiance – Advanced.
How to Enable XP/Vista themes in your Dialog?
8![]()
From Windows XP onwards, the basic control look and feel have improved drastically. Previously, they were merely flat style controls without any effects- the maximum you can do is to change the control color and mouse cursor icon( I hope you do remember the Jungle Theme of those old win98 days). In Vista its even more stylish. But is it possible to give the same look & feel effects to our applications as well?

![]()
Yes. You can. You just have to add a manifest file to your application. Well follow the detailed steps about how to do it. Well, we are going to apply Vista look and feel to the following dialog.

1) The manifest format is specified below. Copy it to notepad and change the “ExeName” to the name of your application. For instance if your exename is Rabbit.exe, then “ExeName” is “Rabbit”.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="ExeName"
type="win32"
/>
<description>Application Description</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
2) Now insert a new resource.

3) Now add a new Custom Resource with Resource ID as 24.

4) Now copy-paste the manifest contents to new resource and save it. Make sure you’ve changed the “ExeName” to your application name.

5) Now take the properties of our new resource – “IDR_DEFAULT1″.

6) Now change the Resource ID to “1“.

7) That’s it. Now compile and run the application. And now see the command buttons in Vista Style. Cool! isn’t it?

![]()
I searched a lot for the screenshot of the old “Jungle Theme” of Windows 98. Its a pity that I couldn’t find it.
If you find one such, please do notify. I’m getting nostalgic.
![]()
Targeted Audience – Intermediate.
How to Load Bitmap and Access the Bitmap Data?
3![]()
While doing image processing, you will be loading the bitmap, access each pixel, process it and set it back. You can get/set each pixel by calling functions which is time consuming. Well, It will be nice if its possible to access the entire bitmap buffer and process it. Since access the data by pointers is so fast compared to get/set each pixel by function calls. But how to load the bitmap, cut it and access the bitmap data?

![]()
You can load the bitmap by using LoadImage() and can call GetBitmapBits() to access the pixel data of bitmap. After processing, you can call SetBitmapBits() to set the processed data back to bitmap. Check the code snippet below.
// Load and process the bitmap data.
void LoadAndProcessBitmap( TCHAR* pBitmapPath )
{
// Load the image bitmapt.
HBITMAP hBitmap = 0;
hBitmap = (HBITMAP)LoadImage( NULL,
pBitmapPath,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE | LR_DEFAULTSIZE);
// Access bitmap data.
CBitmap Bitmap;
Bitmap.Attach( hBitmap );
// Calculate buffer for bitmap bits.
BITMAP BitmapInfo = { 0 };
Bitmap.GetBitmap( &BitmapInfo );
// Calculate the size of required buffer.
DWORD BitmapImageSize = BitmapInfo.bmHeight *
BitmapInfo.bmWidth *
( BitmapInfo.bmBitsPixel / 8 );
// Allocate memory.
BYTE* pBitmapData = new BYTE[ BitmapImageSize ];
ZeroMemory( pBitmapData, BitmapImageSize );
// Get bitmap data.
Bitmap.GetBitmapBits( BitmapImageSize, pBitmapData );
// Now access and process bitmap data
// as you wish!
// Now after processing, set the bitmap data back.
Bitmap.SetBitmapBits( BitmapImageSize, pBitmapData );
// Now you can use the processed bitmap for your purpose.
// For instance, save to disk, display in your dialog etc.
// Delete bitmap data after use.
delete pBitmapData;
pBitmapData = 0;
}
![]()
Here I’ve used CBitmap for ease. But this one can be done with pure windows apis as well.
![]()
Targeted Audience – Intermediate.
How to enable warning for Deprecated Functions?
0![]()
If you port some of your old vc6 projects to Visual Studio 2005, you might have noticed that several function have become deprecated. Well, if you are a framework writer, its handy to notify user about the function which existed but deprecated. Well, how to do it?

![]()
You can use #pragma deprecated or __declspec(deprecated) to mark the deprecated functions. Then, if the function is used, compiler will shoot a warning while compiling. Check the code snippet below for how to do it in different conditions.
1) Deprecated Functions
// If you want to set Fun1() and Fun2() as deprecated. #pragma deprecated(Fn1, Fn2) Fn1(); // C4995 Fn2(); // C4995
or
// If you use __declspec(deprecated), then you can
// provide an error message as well.
void __declspec(deprecated("Fn1 is not supported")) Fn1()
{}
2) Depricated Class
// If you want to set a class as deprecated. #pragma deprecated( CFooBar ) CFooBar obj; // C4995
or
// If you want to mark deprecated by using __declspec(deprecated)
class __declspec( deprecated("No Longer supported")) CFooBar
{};
3) Overloaded Member functions
class CFooBar
{
public:
void Fun1()
{}
// Deprecating one of overloaded function.
void __declspec(deprecated) Fun1( int a )
{}
};
...
CFooBar obj;
obj.Fun1(); // Okay.
obj.Fun1( 0 ); // Error.
4) Error on deprecated function usage
If you enable – “Warning as errors” in Project settings, then the usage deprecated functions will halt the compilation which will be even more noticeable. Check the screenshot.

![]()
Well, take care that this functionality is only available for visual studio 2005 and younger siblings.
![]()
Targeted Audience – Beginners.
How to find the Relative Path to other Common Folders – More Easily?
0![]()
Well you know what a relative path is. The “.\..\..\Folder” thing. Usually in project settings, while referring the path of other common include folders, relative path is being used. Because, it won’t break the project settings even though the entire development folder structure is moved from one location to another.
To be more specific, If you have specified absolute path like – “c:\Source\BlahBlahProject\Libs” in the project settings, if you move Source folder to D:\ your project won’t compile. Relative paths are good, but its a bit difficult to calculate it. I saw people calculating it with their memory by touching the folder structure in monitor.
Some advanced guys uses the windows explorer’s auto complete feature to get the relative path. But is there any other easy method to get it?

![]()
Well, You can use Visual Studio for it. Just follow the steps. Assume you want to get the relative path from your project folder to the common lib folder – “c:\Source\BlahBlahProject\Libs”.
1) Load you project in visual studio.
2) Create a file hello.cpp in folder – “c:\Source\BlahBlahProject\Libs”.
3) Add the file to your project by using menu – Project > Add To Project > Files.
4) Right click the file and take Properties.

5) In the properties, you can get the relative path from “Persist as” editbox.

![]()
The only limitation is our imagination. Isn’t it?
![]()
Targeted Audience – Intermediate.
Windows File Protection – How to check whether the file is protected?
0![]()
Windows have got a set of files which are essential to the working of system. Chances are lot that those files to be corrupted accidentally or intentionally. But windows have taken care about it and those files are under protection. So if your user selects the filename of a protected system file to save his data, its the duty of a responsible windows citizen application to check whether its a system file and to warn the user. Well, how to check whether the file is a protected system file?

![]()
You could use the function – SfcIsFileProtected(). Check the code snippet.
#include "Sfc.h" ... // Check whether the file is protected or not. WCHAR* pProtectedFile = L"C:\\Windows\\system32\\kernel32.dll"; BOOL bProtected = SfcIsFileProtected( NULL, pProtectedFile );
How to get the list of protected files?
Well, iterating the protected file list will be nice. isn’t it? You can use the function – SfcGetNextProtectedFile() for that. Check the code snippet below.
// Get the protected file list - one by one.
PROTECTED_FILE_DATA ProtectedFileInfo = { 0 };
while( SfcGetNextProtectedFile( NULL, &ProtectedFileInfo ))
{
// Print the filename.
cout << ProtectedFileInfo.FileName << endl;
}
![]()
Well, for some reason the SfcGetNextProtectedFile() is removed from vista. So keep in mind that it will work only on XP and 2000 machines. Well, don’t forget to include sfc.lib in your project settings.
![]()
Targeted Audience – Beginners.
How to Set one Dll as Delay Loading Dll?
0![]()
Nobody like delays. All of us want everything to be on time. May the the most hated characteristic might be the delaying nature. Well, while everyone hates delays, there is one special guy who loves delays – the Windows loader.

Well, as you know the purpose of the windows loader is to load the an application file disk to memory and start the execution. While loading an executable, loader have to make sure that all the dlls required by application are also loaded to memory. But assume you have 50 dlls. Eventhough those dlls have to do nothing in application startup, its loader’s duty to load it. And when the dll count goes on, loader have to work more and there by your application needs more time to start. Well, how to make the startup more faster in this situation?
![]()
You could mark your dlls as delay loading dlls. If you have enabled delay loading dll option for the dll, then loader loads the dll only when application make the first function call to that dll. Hence, during application startup, the loader’s “load” can be reduced and it will improve startup time. In other words, you can call it as “Load on demand”.
Well for enabling delay loading for a dll, you have to specify the linker option /DELAYLOAD:dllname to linker settings. For instance, see the screenshot for how to enable delay loading for mydll.dll.

![]()
Interesting to know that loaders love delays. Isn’t it? lazy loaders
![]()
Targeted Audience – Intermediate.
How to Remove Unreferenced Formal Parameter warning?
6![]()
Hands on legacy code base is a different kind of game! You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, most will switch the warning level to high. Well, did you ever ported your legacy codebase by updating its project settings such as by switching the warning level from Level 3 (default) to Level 4, most probably you’ll get this – “Warning C4100: ‘Param2′ : unreferenced formal parameter”.
In most of the case, it might be some unused stack variable. But in some other cases, it might be the unused function parameters as well. For instance, if base class have some pure virtual function, we have to implement that, even if we don’t have any specific implementation for it. In that case, the parameters will not be used and can cause the same warning. Well how to “kick out” those warnings easily?

![]()
Well, there are two tricks.
1) Use the macro – UNREFERENCED_PARAMETER
The common trick is to use the macro UNREFERENCED_PARAMETER(). When it expands, it just assigns the variable to itself. Just like a=a. Which will remove the warning, since the variable is used to assign to itself.
// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool Param1, bool Param2 )
{
// Here Param1 and Param2 are not used which will
// trigger warning. Now they won't.
UNREFERENCED_PARAMETER( Param1 );
UNREFERENCED_PARAMETER( Param2 );
}
2) Comment out the variable names.
Even though UNREFERENCED_PARAMETER() serves the need, its not efficient. isn’t it? It assigns the variable to itself and thus avoids the warning. Well there is another easy method. Just comment out the variable name. Check the code snippet below.
// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool /*Param1*/, bool /*Param2*/ )
{
// Since the variable name doesn't exist, how compiler can
// complain that variable is not used.
}
![]()
In one of my projects, I was forced write an empty implementation for a pure virtual function in derived class. And my client needs zero compilation warning. At that time I really struggled to remove this warning.
Now when i shared it with you, it feels good!
![]()
Targeted Audiance – Beginners.
