PocketMatrix forums

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

Finding the app's directory

How can I find the path that the application is running in? For example, if the program is in \My Documents\app.exe I want to get either \My Documents or \My Documents\. Help?
Conan · 11 Jul 2002, 07:23 UTC — finding the file path
From an earlier post discussing the same think Kzinti wrote
PocketFrog already has a function to do just that: PocketPC::GetFullPathName();


I think this is what you are after. Note that finding the path is a standard windows CE function wrapped up in this function. See the source
Thanks!
I'm looking at the GetFullPathName() function and it doesn't take 0 parameters. Can anyone explain to me what it takes? I don't understand.
It takes two parameters:

The first one is your filename.
The second one is a buffer that will be filled with the full pathname.


Ex:

Code:

char filename[] = "myfile.bmp";
char fullpath[MAX_PATH];

PocketPC::GetFullPathName( filename, fullpath );


The resulting full path name will be in "fullpath".
It didn't work. I put a file in \My Documents\file.bmp and it returned \
Who returned "\" ? GetFullPathName()?

What parameters are you using?

Show me some code if you want me to help you.
Ok, I have my exe and the wav file in \My Documents and this is my code:

Code:

FILE* stream;
stream = fopen("myfile.txt","w");
TCHAR* filename = (TCHAR*)"omg.wav";
TCHAR fullpath[MAX_PATH];
PocketPC::GetFullPathName(filename, fullpath );
fprintf(stream,"%s%s",fullpath,"\n");
fprintf(stream,"%s",filename);
fclose(stream);


The outputted file looks like this:
\
omg.wav

What's wrong?

I would like to look like this:
\My Documents
omg.wav

or

\My Documents\
omg.wav
You are using a cast to convert an ASCII string to a wide (unicode) char string. This is really bad.

Usually, when there is a cast in a piece of code, it is the problem. Try this:

Code:

TCHAR* filename = TEXT("omg.wav");
TCHAR fullpath[MAX_PATH];
PocketPC::GetFullPathName(filename, fullpath );


Unfortunately, I'm afraid you won't be able to write this wide char string using fprintf(). Unless there is a MS extension...? I never use the <stdio.h> functions, so I wouln't know.
It still only returns "\"...
Can anyone tell me if this code works for them?

Code:

FILE* stream;
stream = fopen("\\My Documents\\myfile.txt","w");
TCHAR* filename = TEXT("omg.wav");
TCHAR fullpath[MAX_PATH];
PocketPC::GetFullPathName(filename, fullpath );
fprintf(stream,"%s",fullpath);
fclose(stream);


Of course you have to modify omg.wav to something you have in My Documents.
Can you test it with the debugger? I'm guessing that it's returning the right value, but failing when writing to the file.

This is because you are trying to write a unicode string with fprintf, which is meant for ascii strings.

Try something like this to create and write your file:

Code:

// Create the file
HANDLE hFile = CreateFile( pathname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0 );
if (hFile == INVALID_HANDLE_VALUE)
{
return false;
}

// Write the info
DWORD temp;
WriteFile(hFile, &fullpath, sizeof(fullpath), &temp, 0 );

CloseHandle( hFile );


If that works (it should) open the file with a hex editor and you'll see it start like this:
2F 00 4D 00 79.....

What happens is fprintf prints the '/' then hits the 00 which is a null termination on the string and it stops printing.

Pam
How can I convert it to ASCII so I can use it to load other files that take a filename of char*?
BurningSheep · 11 Jul 2002, 20:56 UTC
Kzinti wrote:
Unfortunately, I'm afraid you won't be able to write this wide char string using fprintf(). Unless there is a MS extension...? I never use the <stdio.h> functions, so I wouln't know.


You can convert the wide char string to an ansi string with the WideCharToMultiByte function (mucho fun... no really)

or you can just use fwprintf() :)

Code:

int fwprintf( FILE *stream, const wchar_t *format [, argument ]...);
BurningSheep · 11 Jul 2002, 21:04 UTC
come to think of it there probably is a tchar function of fprintf as well... i never had to use it though

anyways here is a small piece of code I use to convert a wide char string to ansi string

Code:

TCHAR pathname[MAX_PATH];
PocketPC::GetFullPathName(_T("file.dat"), pathname);

char s[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, pathname, -1, s, MAX_PATH, NULL, NULL);
Thanks! It worked! Sorry everyone!
and for anyone that needs it, I've made this function for myself.

Code:

char* LoadRelativeFile(TCHAR* filename){
TCHAR pathname[MAX_PATH];
PocketPC::GetFullPathName(filename, pathname);
WideCharToMultiByte(CP_ACP, 0, pathname, -1, multibytefilename, MAX_PATH, NULL, NULL);
return multibytefilename;
}


and put this at the top of your program (make it global):

char multibytefilename[MAX_PATH];
Hehe Cow games, your gonna like my next game Moo Moo Pocket! www.drcreations.net/moomoopocket.html