You might want to try this: have your program call GetCurrentDirectory()
and output the result somewhere to that you can confirm that the program's current directory is truly what you think it is. You could use OutputDebugString()
if you are running under a debugger or MessageBox()
if not.
When I need to be sure I'm opening a co-located file, I call GetModuleFileName( NULL, szExeFqn, sizeof(szExeFqn))
to get the FQN of the EXE file, strip off the EXE file name and add on the name of the file I need. That way, I'm not making any assumptions that might be false -- after all, it is trivial to have a shortcut set the default directory for a program different from the directory containing the EXE file.
Also, if you run your program from a command line by entering the full path to your program, then the program's current directory is the one you were in when you ran it, not the one where the EXE was found. That's also true if your program is found by searching the PATH
environment variable (yes, it still exists in Windows.)
Here's an example of what I've used for more than a decade to do what you've described:
char szHelpFileName[_MAX_FNAME];
char *cp;
GetModuleFileName( NULL, szHelpFileName, sizeof(szHelpFileName) );
cp = strrchr( szHelpFileName, '\\' );
if( cp )
{
cp++; // Point to the char just following the final slash.
}
else
{ // No backslash, is there a colon?
cp = strrchr( szHelpFileName, ':' );
if( cp )
{
cp++; // Point to the char just following the colon.
}
else
{ // I give up. I'll have no path information.
cp = szHelpFileName;
}
}
strcpy( cp, "program.hlp" );
The final result is the name of a help file that is co-located with the EXE. Unless the help file is missing (or its permissions have been scrogged somehow) this always works. I've never had it follow the "paranoia" path where the comment says "I give up."