I decided to post what ought to be a workable solution to this. It involves fixing the code 'properly' to use absolute paths but bear with me. It's not that hard. The answer posted by the OP is just oh so dangerous. It's not 'cheap' at all. On the contrary, it is likely to prove very expensive in the long run.
Now I assume that there are function / method calls scattered throughout the code that access files or folders via relative pathnames (most likely unqualified filenames). There may well be a lot of these but they probably all boil down to this:
do_something_with_this_file ("somefile.foo");
Now if do_something_with_this_file
is a function defined within the application the solution is obvious: change the implementation of that function to convert the parameter passed in to an absolute path name, if necessary, before doing anything with it.
But life is not so easy if do_something_with_this_file
is something like a call to CreateFile
or perhaps fopen
. Then we are stuck with the existing behaviour for that function. Or are we? Actually, no. There's a simple solution involving just a macro and a small amount of implementation work.
Time for an example. I will use fopen
since it has a nice simple API, but it applies equally well to CreateFile
or indeed anything else.
Firstly, create a header file, lets call it AbsolutePathHelpers.h
, which redefines fopen
, and whatever else you need, like so:
// AbsolutePathHelpers.h
FILE *APHfopen (const char *filename, const char *mode);
#define fopen APHfopen
// ...
Make sure that this header file gets included in all your compilation units, probably by #include
ing it in some common header file that they all use.
Now write the implementation of APHfopen
in a new .cpp file (let's call it AbsolutePathHelpers.cpp
) which you will need link in with your project:
// AbsolutePathHelpers.cpp
#include <stdio.h>
#undef fopen
FILE *APHfopen (const char *filename, const char *mode)
{
printf ("Opening %s\n", filename); // diagnostic message for testing
// Convert filename to absolute path here (if necessary) before calling the 'real' fopen
return fopen (filename, mode);
}
// ...
And finally a simple test program:
// Test program
#include <stdio.h>
int main ()
{
FILE *f = fopen ("myfile", "r");
if (f)
fclose (f);
printf ("Finished. Press any key...");
getchar ();
return 0;
}
Output:
Opening myfile
Finished. Press any key...
Run it at Wandbox.
I'll leave the details of converting relative paths to absolute paths to the OP. All you need is a global variable containing the current directory in effect when the program starts up. Or, I strongly suspect, that should actually be the directory containing the executable.
GetOpenFileName
in the context of a helper process, see: #3460374. It might even be enough to disable your main window while the open file dialog is open, maybe you don't need to resort to the level of trickery in that post. – BroughtonIFileDialog
and avoid the problem completely? Or am I missing something. – MadrieneGetModuleFileName
or friends. – Cheryl