Archive for August, 2008
How to parse the parent directory from a given path – More easily?
0![]()
Yesterday one of my friend called me to solve the mystery behind his crash! When i checked the code, i saw a couple of string operations by pointers, memory allocations, memset - All evils under one hood.
Actually the memset caused the crash which writes beyond the buffer and corrupt other stack objects.
But suddenly i noticed one thing – what he is trying to do? He have a path and he wants to get the parent path. E.g. If his path is “C:\Application\Config”, then he want to get - “C:\Application” the just parent path. Well, the guy brushed up old string lessons and handled everything by himself which resulted in crash. I reviled him a secret api to do the stuff painlessly. Mmmm… I can tell that secret to you too…
![]()
The api is – PathRemoveFileSpec(). You can use it to remove the last directory or filename from the given path. Have a look at the code snippet.
#include "Shlwapi.h"
...
// Main path.
TCHAR pBinPath[] = _T("C:\\MyApplication\\bin");
// Get the parent path.
PathRemoveFileSpec( pBinPath );
// Now the pBinPath contains - "C:\\MyApplication"
![]()
Don’t forget to add Shlwapi.lib to project settings.
![]()
Targeted Audience – Beginners.
How to calculate the checksum of data?
4![]()
Long back, I own a Cyrix machine and periodically it stops booting by showing the message – “Checksum failure“. I still remember those long beeps.
Well, Checksum is digest generated from a long data which can be used to check the integrity of data.

For instance, if you want to know whether your data file is corrupted, generate and keep the checksum and afterwards for verification again generate the checksum and compare. So how to generate the checksum?
![]()
You can use the function – CheckSumMappedFile(). Actually its used to calculate the checksum of mapped file. Well, you can use it to calculate the checksum of your data too! Just see the code snippet.
// The data.
char* pString = "Hello";
DWORD HeaderSum = 0;
DWORD Checksum = 0;
// Calculate the checksum.
CheckSumMappedFile( pString,
strlen( pString),
&HeaderSum,
&Checksum );
![]()
The return of the function is PIMAGE_NT_HEADERS and in this case, it will be 0 and just ignore it. Since our data provided is not a valid PE file, its obvious. If you check the Checksum variable, you can find it contains a valid checksum always. So Take care!
![]()
Targeted Audience – Intermediate.
How to iterate all MFC objects allocated on heap?
0![]()
MFC is famous for memory leaks. Usually we add icing and cherrys to those leaks by adding our own leaks. Is there any mechanism to track those?
Well, if you’re object is a mfc object, which is derived from CObject, then MFC provides an iteration mechanism to iterate through your objects. You can use it to track the count of your objects in heap, you can dump selective objects… etc… etc… Well, how can you do it?
![]()
Use the api – AfxDoForAllObjects(). While calling we’ve to pass a function pointer of expected syntax, and for each CObject derived object in heap, the function will be called. See the sample to iterate all CDynLinkLibrary objects and to find its count.
// This function will be called as callback
// from AfxDoForAllObjects.
void DoForAllObjects(CObject* pObject, void* pContext)
{
// Here context is the pointer to count variable.
int* pCount = (int*) pContext;
// Check whether the object is of type CDynLinkLibrary
if( dynamic_cast<CDynLinkLibrary*>( pObject))
{
// Increment count.
++(*pCount);
}
}
// Iterate objects.
void CDialogDlg::Iterate()
{
// Iterate all CObject instances and get the
// count of instances of CDynLinkLibrary objects.
int DynLinkLibCount = 0;
AfxDoForAllObjects( DoForAllObjects, &DynLinkLibCount );
}
![]()
You can utilize this for finding memory leaks, to find the count of instances of a particular class, you can dump objects of particular class, etc…
Well, one thing to remember – its only available in debug build. So take care!
![]()
Targeted Audience – Intermediate.