Gold mine of Visual C++ tricks!
Posts tagged PathRemoveFileSpec()
_chdir() pitfall – error while opening file.
Oct 4th
![]()
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.
How to parse the parent directory from a given path – More easily?
Aug 6th
![]()
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.