Archive for June, 2008


Microsoft is still on the workplace for making new versions of Visual studio versions. They released the first Visual studio named ‘Visual Studio 97‘ on 1997. After that, on 1998 they released ‘Visual Studio 6.0‘. On 2002 Microsoft released the ‘Visual Studio 2002‘ and in April 2003 they released the ‘Visual Studio 2003‘. After that, they released the ‘Visual Studio 2005‘ on October 2005. And the new baby  ‘Visual Studio 2008‘ on the market, from 2007 November.

While developing applications on different versions of Visual stdio, keep in mind that, a lot are changed. for example , MFC changed from version 1.0 to 9.0 🙂

So the underlying architecture too…

One simple but serious thing noticed by me is the ‘Default Character Set‘ used by the different versions of Visual Studio.

Visual Studio 2003 by deafult using the MBCS (Multi Byte Character Set)

Visual Studio 2003 Default Characet Set

and Visual Studio 2005 by default using the UNICODE character set .

Visual Studio 2005 Default Character Set

🙂

Note this thing, while you convert one project from different versions of visual studio.

Advertisements

Using timers in windows programming is common in nature. Lot of situations like calculating the time difference, calculating the execution time of a procedure or an operation etc.

Microsoft provides a lot of methods for calculating time, by using common,  SetTimer() and KillTimer() methods and High-Resolution Timer methods like QueryPerformanceCounter() and QueryPerformanceFrequency()

Here I’m just describing how to calculate the time difference by using the Microsoft High-Resolution timer methods.

    LARGE_INTEGER m_lnStart;
    LARGE_INTEGER m_lnEnd;
    LARGE_INTEGER m_lnFreq;

    QueryPerformanceFrequency(&m_lnFreq);//Retrieves the frequency of the high-resolution performance counter

    QueryPerformanceCounter( &m_lnStart);//Retrieves the current value of the high-resolution performance counter
    //Some Operations..
    //......
    //......
    QueryPerformanceCounter( &m_lnEnd );

    __int64 nDiff(0) ;
    double dMircoSecond( 0 ) ;
    if( m_lnStart.QuadPart !=0 && m_lnEnd.QuadPart != 0)
    {
        dMircoSecond = (double)(m_lnFreq.QuadPart/1000000);
        nDiff = m_lnEnd.QuadPart-m_lnStart.QuadPart ;
        dMircoSecond = nDiff/dMircoSecond;
    }

Now you got the time difference in Microsecond..

What we need above this.. 🙂

Thanks to Microsoft®..

Alternative for realloc()

During my current project, i faced one simple, but complex situation.

ie. My TestApp code use memory allocation functions such as malloc, realloc, free etc during the earlier stages. At a later stage I changed those into C++ functions such as new and delete.

for eg:

BYTE* pbKey = NULL;
if (!(pbeKey = (BYTE *)malloc(dwKeyLen)))
 {
 // Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
 }

could be replaced by

BYTE *pbKey = NULL;
 if(!(pbKey = new BYTE[dwKeyLen]))
{
// Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
}

so simple neh? 🙂

But what about realloc() ? *-)

realloc can be implemented in simple way

BYTE *pbKey = NULL;
if(!(pbKey = new BYTE[dwKeyLen]))
{
// Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
}
// Create a buffer for encrypted data
BYTE* pbReallocData = NULL;
if (!(pbReallocData = new BYTE[dwNewLen]))
{
  // Error
  TRACE(_T("malloc error 0x%x\n"), GetLastError());
  return false;
}
::ZeroMemory(pbReallocData,dwNewLen);
memcpy(pbReallocData,pbData,dwKeyLen);
delete[] pbData;
...
delete[] pbReallocData;

😛

Macros !!!

Really a wonderful thing , provides a lot of facilities to the developer.

Some useful macros in Microsoft C++ are,

__DATE__ //a string literal containing the compilation date in the form Mmm dd yyyy eg:"Jun 13 2008" of the current source file.
__TIME__ //a string literal containing the compilation time in the form hh:mm:ss eg:"10:01:07" of the current source file.
__FILE__ //a string literal containing the name of the source file being compiled
__LINE__ //a decimal integer constant containing the current line number in the source file

for example, you can use like this 🙂

        CErrorLogger::SendErrorMessage(_T("Can't load the values from Settings.ini file."),__FILE__,__LINE__);

Which produce the error message with file name and line number. During debugging these informations are so useful. 😛

Stack Unwinding

When an exception is thrown, the runtime mechanism first searches for an appropriate handler for it in the current scope. If such a handler does not exist, the current scope is exited and the function that is higher in the calling line is entered into scope.

This process is iterative; it continues until an appropriate handler has been found. An exception is considered to be handled upon its entry to a handler. At this point, all the local objects that were constructed on the path from a try block to a throw-expression have been destroyed. In other words, the stack has been unwound.

Some Optimized Codes

Different types of optimization help us to improve our code’s stability.

Tail splitting eliminates jumps by bringing common return statements into the blocks, so that

if(x) {
   b = 1;
} else {
   b = 2;
}
return b;

becomes

if(x) {
   b = 1;
   return b;
} else {
   b = 2;
   return b;
}

which is a speed optimization.

Tail merging, on the other hand, pulls duplicate instructions out of the blocks, which is a size optimization. Tail merging turns the following:

if(x) {
   bar()
   b = 1;
} else {
   foo()
   b = 1;
}

into this:

if(x) {
   bar()
} else {
   foo()
}
b = 1;