Archive for March, 2008
Interlocked family of functions for simple Synchronizations.
0![]()
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.
![]()
For that you can Interlocked family of functions. Please see some of the functions below.
InterlockedIncrement()
InterlockedDecrement()
InterlockedExchange()
InterlockedCompareExchange()
InterlockedExchangePointer()
…
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 );
InterlockedExchangePointer()
Do you know what is MZ?
3![]()
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.
0![]()
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.cpl – Invokes Add new hardware wizard
Please refer http://www.dx21.com/SCRIPTING/RUNDLL32/REFGUIDE.ASP for huge list of features that can be accessed via RunDll32.
Usages:
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
0![]()
Did you ever noticed the following lines in the your header file?
#ifndef _MYHEADER_H_ #define _MYHEADER_H_ ... #endif
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.
![]()
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.
0![]()
For die-hard debugging purpouse and for learning the internals, usually we need to refer the assembly code generated by the compiler.
![]()
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
0![]()
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…
![]()
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.");
![]()
Open the exe in textpad for seeing the data. Opening in notepad will not work.
Log path informations to eventlog, Even better.
1![]()
In most of our projects, when some error occurs, it’s being logged to the evenlog with line number and filename. Its done by using __FILE__ and __LINE__ macro.
But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder – “C:\Jijo\Build\MyProduct”, the __FILE__ macro will include this full path and finally in eventlog will contain funny paths which the end-user might see. You can see some 3ed party eventlogs in event viewer which contains authors name in path.
![]()
You can use #line directive to modify __LINE__ and __FILENAME__. The syntax is as follows,
#line lineno “FileName”
Please see an example below.
#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"
![]()
While experimenting I’ve found that – Its safe to use #line after all #includes. If its used at the top of file, It can be reinitialized by the include files. At first it was not working for me. Latter works. Anyway take care.
Handy MSDN.
0![]()
In some urgent cases we need to refer function details such as .h, lib, dll name of API etc…, evenif MSDN or internet is not accessable. For that inside visual studio directory a CSV file is provided which describe complete Win32 API function descriptions.
![]()
Its here – <VisualStudioDir>\VC98\Lib\WIN32API.CSV
Open and have a look!
Hardcoding Breakpoint.
2![]()
In some cases, MSDEV wont allow to set break point event if we press F9 a number of times. In those situations you can hardcode a break point by yourself.
![]()
Just insert the following line to the point where you would like to break while executing.
__asm int 3;
![]()
VisualStudio uses Interrupt 3 for implement break points. When we put a breakpoint, actually the debugger is inserting an int 3 instruction at that point. While executing, when an interrupt 3 is raised – debugger understood that one breakpoint is reached and it breaks and shows the appropriate code.
Avoid Stepping into unwanted functions while debugging.
1![]()
While debugging we usually step into functions by using F11. But if the function have some CString parameters, the F11 will get into the CString constructor. This is a real headache while debugging. To avoid this, you can define a set of Step into ignorable functions in the autoexp.dat of Visual Studio.
![]()
1) The Autoexp.bat is in the following location.
<VisualStudioDir>\Common\MSDev98\Bin\AUTOEXP.DAT
2) For ignoring CString’s constructor and operator =, please add the following,
[ExecutionControl]
CString::CString=NoStepInto
CString::operator==NoStepInto
3) If you want to ignore all functions of a class then you can use,
[ExecutionControl]
CMyClass::*=NoStepInto
4) If you want to ignore all functions of a perticular namespace then you can use,
[ExecutionControl]
MyNameSpace::*=NoStepInto