Lately i encountered a situation where i had to find the current directory from where my application is running and all my config files reside. Question is "How do I find the current directory?" on Windows CE/Mobile devices. Desktop / PC it is just piece of cake but Windows CE / Mobile devices don't have a concept of a current directory. Which means all pats are absolute and there is no concept called relative path. Due to lack of relative paths most of the files are loaded to the "Windows" directory and that is how Windows directory is crowded. Alternatively you can hard code directory path and insist user to load files always there. But i hate to hard code values or copy files to Windows directory.
So this has triggered to find a solution to identify my current directory. Since there is no concept of a current directory on a Windows CE / Windows Mobile device how would one locate a resource for which only a relative path is known? That is what following code does
Code snippet to Find current directory:
WCHAR* wszTemp = NULL;
WCHAR wszDirectory[MAX_PATH] = L"\0";
wszTemp = wcsrchr(wszDirectory,L'\\'); // Finds last occurence and replace it with NULL.
*(wszTemp+1) = L'\0';
Let me explain how does it work:
GetModuleFileNameW API Retrieves the fully-qualified path for the file that contains the specified module. For example if you are running your applciation from \Application\MyOwn\Sample.exe location then GetModuleFileNameW API retrieves full path and stores in wszDirectory variable.
wszTemp = wcsrchr(wszDirectory,L'\\'); this step finds last occurrence of "\" and replace last occurrence with NULL .
Now wszDirectory content would be \Application\MyOwn\
Hope this helps !!! Do let me know if you need any help !!!!!!!!!