PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Finding path of current exe

How can I find the path of the currently running application so i can use this to load images and music files from the current folder??

Thanx in advance.

Matt
I 'believe' that the current gapidraw release supplies that functionality internally (its in the docs somewhere), but I've not tried it. I'm using a pair of functions that do what you require. The first returns a TCHAR *, the second a char * (used for FMOD filespecs).

Code:
LPCTSTR getFullPath(const TCHAR *pFilename)
{
static TCHAR lpFilename[1024];

INT nStrLen;
TCHAR *pDest;
INT result;
INT ch = '\\';

for(INT i = 0; i < 1024; i++) lpFilename[i] = '\0';

nStrLen = GetModuleFileName(NULL, lpFilename, 1024);
pDest = _tcsrchr(lpFilename, ch);
if (pDest != NULL) {
result = pDest - lpFilename + 1;
lpFilename[result] = '\0';
if (pFilename != NULL) _tcscat(lpFilename, pFilename);
return lpFilename;
} else return NULL;
}



Code:
char * c_getFullPath(TCHAR *filename)
{
static TCHAR lpFilename[1024];
static char cFilename[1024];

INT nStrLen;
TCHAR *pDest;
INT result;
INT ch = '\\';

for(INT i = 0; i < 1024; i++) lpFilename[i] = '\0';

nStrLen = GetModuleFileName(NULL, lpFilename, 1024);
pDest = _tcsrchr(lpFilename, ch);
if (pDest != NULL) {
result = pDest - lpFilename + 1;
lpFilename[result] = '\0';
if (filename != NULL) _tcscat(lpFilename, filename);
#ifndef _WIN32_WCE
return lpFilename;
#else
size_t n = wcstombs(cFilename, lpFilename, 1024 - 1);
return cFilename;
#endif
} else return NULL;
}
[quote="ppcStudios"]
Code:

static TCHAR lpFilename[1024];
...

for(INT i = 0; i < 1024; i++) lpFilename[i] = '\0';
...


Couple comments, MAX_PATH is the usual index size for filenames, 1024 is a bit much. Also you might want to check out memset and ZeroMemory instead of the zeroing loop you've used, these functions will perform faster. But I question why you've even done that when lpFilename[0] = 0 will accomplish what you need.
I typically only call this code once or twice to set up the initial paths, so speed isn't an issue. In fact, I most likely scarfed this code from some other source and most likely never changed it.