[comp.windows.ms.programmer] Finding full pathname of Windows Executable

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (03/11/91)

How can a Windows program figure out the pathname of its .exe file?

Why I ask:  I have a Windows program that needs to load a data file that
is stored in the same directory with the .exe.  I don't want to hard-code
the data file's filename...

In MS-DOS, a program could get this information by looking at argv[0].  How
is it accomplished in Windows?


ADV thanks ANCE


Terrell

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (03/11/91)

Well, (I think) that I've answered my question:

Here's how I'm opening a file which exists in the same directory as my
program:


BOOL file_exists(char *filename)

/*
Returns TRUE iff the specified filename exists.
*/

{
OFSTRUCT	of;

return OpenFile((LPSTR) filename, &of, OF_EXIST) != -1;
}


void open_file(HWND hwnd, const char *filename)

/*
Attempt to open the specified file.
*/

{
char	pathname[MAXPATH], drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE],
	ext[MAXEXT];

/* Get full pathname of file. */
GetModuleFileName(GetModuleHandle("program.exe"), pathname, MAXPATH);
fnsplit(pathname, drive, dir, file, ext);
sprintf(pathname, "%s\%s\%s", drive, dir, filename);

if (file_exists(pathname))
	input_file = fopen(pathname, "r");

if (input_file == (FILE *) NULL)
	MessageBox(hwnd, "File not found", "", MB_OK | MB_ICONSTOP);
}


IS THERE A BETTER WAY?


Terrell