PocketMatrix forums

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

Directory woes

Hi.

Got a problem with my app. What I want is to be able to get the current dir from wherever the program is run.

At the moment I declare the file paths with a char at the beginning:

CHAR filepath[] ="\\paintapp\\icons"

Now this aint too good, because I want someone to be able to cut and paste the app into any dir, and in any storage card. (the sub dir will always be the same though).

Any help or code would be cool :). I got a great idea for a little app, and probably want someone far better than me to code it (carpe, freddy, dan the list goes on).

Anyways, hope someone can help.

Johnny 5.
Check your e-mail John
Here you go, with an example of how to call the function. This will return the path with a trailing backslash.

Code:

DWORD GetModuleDirectory (HMODULE hModule, LPTSTR szPath, DWORD dwLength) {
DWORD dwLen;
LPTSTR p;

if (0 == (dwLen = GetModuleFileName(hModule, szPath, dwLength)))
return(0);
p = szPath + dwLen;

while (p != szPath) {
if (TEXT('\\') == *--p) {
*(++p) = 0;
break;
}
}
return(p - szPath);
}

int main(int argc, char* argv[]) {
TCHAR szPath[MAX_PATH];

GetModuleDirectory(GetModuleHandle(NULL), szPath, sizeof(szPath) / sizeof(TCHAR));
return 0;
}
awesome, thanks dudes.

Ive only tried freddys and it worked a charm, but I'm sure yours does to digby.

J.
Digby....you rock.  Thanks from me too, since I have been doing it the hack way for some time now...

Why isn't the Win32 GetApplicationDirectory (i think that's it) supported in CE?  Its a standard API....
I might aswell post it here...

GetFilePath( "\\filerelativetoyourappdir.txt" );
GetFilePath( L"\\filerelativetoyourappdir.txt" );
(wide character version. Add boh to your code and it will choose the correct one depending on the stuff you send to it)

Code:

char* GetFilePath(char *sFilename) {
TCHAR sTempPath[MAX_PATH];
char sFullPath[MAX_PATH];
char *sEndPath;
char *sPath;
memset(sFullPath, 32, MAX_PATH);

GetModuleFileName(hInst, sTempPath, MAX_PATH);
for(int i = 0; i < MAX_PATH; i++) *(sFullPath+i) = (char)sTempPath[i];

sEndPath = strrchr(sFullPath, '\\');
sPath = new char[MAX_PATH];

for(i = 0; i < sEndPath-sFullPath; i++) sPath[i] = sFullPath[i];
sPath[i] = NULL;
strcat(sPath, sFilename);

return sPath;
}

unsigned short* GetFilePath(unsigned short *sFilename) {
HINSTANCE hMyInst = GetModuleHandle(NULL);
unsigned short *sPath = new unsigned short[MAX_PATH];

TCHAR szMyFile[ MAX_PATH ];
GetModuleFileName (hMyInst, szMyFile, MAX_PATH);

unsigned short *pchLast = szMyFile;
for (unsigned short *pch = szMyFile; *pch; ++pch)
if (*pch == TEXT('\\'))
pchLast = pch;

lstrcpy (pchLast, sFilename);

for(int i = 0; i < MAX_PATH; i++) *(sPath+i) = (unsigned short)szMyFile[i];
return sPath;
}


This can probably be made into nice looking code, but I'm too lacy...it gets the job done.
No, it's not supported. In order to save a whopping 972 bytes of storage space in Windows CE, MS decided not to include the concept of a Current Directory. All a program can do is what Fredrik illustrated, which is find where the executable is physically located. You'll find that quite a few API functions are not supported. Often MS will have an original function (that originated pre-Windows 95), like RegSetValue, which years later was superceeded by a routine with more capability, like RegSetValueEx. Often in that case MS only ported over the newer "Ex" value to Windows CE. That of course makes it harder to port applications of to CE. The underlying functionality is still there, it's just a matter of calling the newer function and changing the parameters around a bit. There are cases where a function is not supported, and there is no good alternative. In that case you just have to do the work yourself.

Dan East