Interlocked family of functions for simple Synchronizations.

24 03 2008

Icon Description
In multithreaded environment we usually use Mutex to synchronize access to resources. But sometimes our resources may be a single integer member variable which holds some count or like that. For such light weight resource, using mutex is not preferred.

Icon How Can I Do It?
For that you can Interlocked family of functions. Please see some of the functions below.


For example if you need to increment a member variable threadsafely, you can use InterlockedIncrement(). That function will ensure the thread synchronization without any additional kernel objects such as mutex etc… Please see the code block below.

long m_lCount = 0;
// Increment the value.
InterlockedIncrement( &m_lCount );

Do you know what is MZ?

24 03 2008

Icon Description
If you open any windows executable in some text editor you can see the executable starts with two chars – “MZ”. Do you know what it is ?

Windows executables are in PE( Portable Executable) file format. For backward compatibility with dos, the PE file format contains a small MS-DOS stub in the starting. That’s why when we run any windows application in DOS, it just print an error message that, it needs windows to execute the application.

The dos header starts with a special signature – “MZ”. MZ stands for Mark Zbikowski. He is one of the earlier Microsoft Architects and the designer of Dos Executable file format. In 2006 he was honored for 25 years of service with the company, the first employee to reach this milestone other than Bill Gates and Steve Ballmer 🙂 Digging history is Interesting… isn’t it ?

RunDll32.exe – The key to windows treasure chest.

24 03 2008

Icon Description
While digging the C:\Windows directory, a windows enthusiast surely will encounter the application RunDll32.exe. If executed it seems to do nothing. But its a key to great windows treasures. Still don’t believe me ? okay run the following command in your command prompt.
RUNDLL32.exe user32.dll, LockWorkStation

Actually speaking, RunDll32.exe is just a program which executes the specified funciton of the specified dll, passed via command line arguments. Please see the syntax as follows.
RUNDLL32.EXE dll_name, EntryPoint <Optional Arguments>

The only requirement is that the callable function’s prototype should be as follows.
void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);

Window had implemented lot of functinalities inside which can be callable via RunDll32.exe. See some e.g. below.

RunDll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3 Shows the System Properties, Advanced tab.
RunDll32.exe shell32.dll,Control_RunDLL hdwwiz.cplInvokes Add new hardware wizard

Please refer for huge list of features that can be accessed via RunDll32.


1) You can create a shortcut for RunDll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,3 by which we can easly take the environment variables tab.
2) You can write some test functions in your dll and we can call it anytime anywhere without exe file.

The usages are many, only limitation is our imagination… 😉

Easy Compilation guards instead of #ifndef macros

24 03 2008

Icon Description
Did you ever noticed the following lines in the your header file?

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

They are protection macros to protect the header file from multiple compilation. Assume your header file is included many time in different CPP. if you are not using these protection macros, your header file will be included more than once and will give you a Multiple declaration error. To prevent that, these macros are being used. But there is a more simple method in VC++ compiler extension.

Icon How Can I Do It?
You can use #pragma once in your header file. E.g.

#pragma once

If you use #pragma once, then the header file will be included only once in your compilation.

See the assembly code generated for your source.

24 03 2008

Icon Description
For die-hard debugging purpouse and for learning the internals, usually we need to refer the assembly code generated by the compiler.

Icon How Can I Do It?
Add compiler switch /FAs in the project settings. It will generate .ASM files for each of your .cpp files with source code and inline assembly. See the following steps.

1) Take project setting by pressing Alt+F7
2) Take C/C++ tab
3) To the Project Options editbox, add /FAs.
4) Recompile the project.
5) Take the release or debug directory, you can see .asm files. 😉

Embed a copyright notice in EXE

24 03 2008

Icon Description
You can embed any strings in your exe or dlls.
1) It can be used for copyright messages etc…
2) It can be used in Export DLL header files to know which version of DLL is linked in the exe.

Apply your imagination for more usages… 😉

Icon How Can I Do It?
Use the #pragma directive. See the usage below.

#pragma comment(exestr, "This is my Exe. Don't copy it away!");
#pragma comment(exestr, "GraphicLibrary Version 1.01.");

Icon Note
Open the exe in textpad for seeing the data. Opening in notepad will not work.