_chdir() pitfall – error while opening file.

4 10 2008

I still remember that bug! Because it grabbed my two days and delayed the delivery. Well, the story goes like this – It was during late summer of 2004. I was working in a DVD writer project. Internally we were using a third party DVD writer library named BHA Gold. Well, my project was a wrapper library for the BHA Gold library.

Well, the bug was this – There is a config file in my project, which exist in the same folder, together with exe. Before writing DVD, I’ve to read some settings from the config file. But the bug is this. During startup, I could open the file. But if I write one DVD using BHA gold, then when i tried to open the same file once again, it shows error!

At last its been found that the culprit is the api – _chdir(). The DVD writing library was calling this api internally to prepare the directory tree, and once this api is called, the default directory will be changed to the specified directory and during next time, while opening the setting file, it shows an error that “file not found”! Since the setting file does not exist in the new default directory.

Well, in such cases you could use the api – SetCurrentDirectory(). See the code snippet to set the current directory as exe’s folder path.

#include "Shlwapi.h"
// Get the full path of current exe file.
TCHAR FilePath[MAX_PATH] = { 0 };
GetModuleFileName( 0, FilePath, MAX_PATH );

// Strip the exe filename from path and get folder name.
PathRemoveFileSpec( FilePath );    

// Set the current working directory.
SetCurrentDirectory( FilePath );

Don’t forget to add Shlwapi.lib to project settings. 😉

Targeted Audience – Advanced.

Interesting bug in VisualStudio!

15 07 2008

Once in technical forum, a guy asked how to add ampersand( & ) symbol to control captions? Because the ampersand( & ) symbol is used to specify the keyboard accelerator for the control. Have a look at my previous post –

Visual Studio, the ultimate creator – recommends to add && to make ampersand visible in control captions. But, does the creator himself suffer due to ampersand problem? 😉 Read on…

Have a look at the properties of Auto variable item in Visual Studio window. It suffers the same bug. The variable name is “&lResult”. But in the properties window, its displayed as lResult”. See the screenshot.

Pretty funny, nah? BTW, Was that guy from Microsoft? 😉 (Just kidding)

But I always bow my head in front this legendary compiler suite. Its a classic. I tried a lot, but my heart won’t allow me to switch to the new generation Visual Studio IDEs. Still 6.0 is my favorite. What about you guys? Comment on!

Targeted Audience – Beginners.

Copy-Paste of source files; Compilation Guard Band’s worst enemy.

10 05 2008

In big projects, sometimes you want to write almost similar classes, which is more or less similar. For instance, assume you have a request base class, and the derived classes should implement some common virtual functions.

Usually one easy method is to copy-paste one of the existing derived class so that by making minor change, we can start the next derived class. While doing so, just remember – you are making one of the dangerous, hard to detect bug. When you try to compile your source, the second derived class you created, will show the error – “error C2065: ‘CDerived2’ : undeclared identifier”

The real culprit here is the compilation guard band. You might notice for avoiding multiple include, your header file contains a macro as follows,

#if !defined(AFX_DIALOGDLG_H__75E88A77_A1A2_4417_9815_D8F2DFB44506__INCLUDED_)
#define AFX_DIALOGDLG_H__75E88A77_A1A2_4417_9815_D8F2DFB44506__INCLUDED_

#endif // !defined(AFX_DIALOGDLG_H__75E88A77_A1A2_4417_9815_D8F2DFB44506__INCLUDED_)

This means, the compiler will go through the declarations inside the compilation guard bands, only once. If you create a class by using class wizard, the class wizard will automatically generate unique compilation guard band for each classes.

Can you assume what happens when you create new classes by copy pasting? While copy-pasting, we usually forgot to change the compilation guard bands and which result in two different classes with same compilation guard band. So while compiling; only one of the classes will be declared.

Its safe to avoide file copy-paste to create new classes. Even if you do, don’t forget the compilation guard bands. 😉

Targeted Audience – Intermediate.