Working Directory Changes

Started by
8 comments, last by the_edd 11 years, 12 months ago
I wrote a very small program to check the working directory of itself. It works fine when I just run the exe. However, when I drag a file on the exe to run it, the program thinks the working directory is C:\Documents and Settings\myusername

Does anyone know why this is and how I can have it correctly detect the working directory even when I drag a file onto the exe?

Here's the code I'm using:


#include "direct.h"
#include "stdio.h"
#include "conio.h"

void main()
{
char* buffer;
buffer=_getcwd(NULL,0);
printf("working directory: %s \n\n",buffer);
getch();
}


And here's the code, exe & 2008 Express project files:
http://www.mediafire...b73k885hxs8sx9a

Any help is appreciated. Thanks!
Advertisement
It probably is correctly detecting the working directory. The working directory can and will change depending on how you start a program.
Do you want the working directory, or the directory in which the executable resides?
Sorry, I forgot to mention one critical detail: it doesn't matter where the file being dragged onto the exe comes from! Even if it's from the same directory as the exe, it will always say C:\Documents...

And yes, I need the directory where the exe is as I need assets loaded from subdirectories.
On Windows you can use GetModuleFileName() to get the fully qualified name of the executable and then manually strip off the file name portion of the string to get the directory.
Thank you. Now I'm trying to figure out why the following only returns a single letter:

TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,MAX_PATH);
printf("working directory: %s \n\n",buffer);
Probably because you're compiling with UNICODE enabled, but trying to use narrow character functions to output the data. Some options include changing your project properties from using Unicode to Multi-byte, explicitly using GetModuleFileNameA() or switching to a wide-character output function.
One thing is probably worth clarifying: the working directory for a process isn't necessarily the directory in which the executable resides. It is merely a directory that is logically prepended to relative paths in order to form an absolute path for the filesystem.

A process can change it's own working directory as it runs, and it's up to whatever initiates a process to set its initial working directory. When you run a program from the Windows command shell, that program's working directory will typically inherit the command shell's working directory (often displayed as part of the prompt). Windows Explorer and your IDE also set the working directory of the processes you ask them to spawn, but their rules for what it should be differ from the command prompt.
Thank you both again. SiCrane, your solutions worked (the two I tried). I really appreciate it!!
EDIT: sorry, accidentally quoted myself rather than editing typo in original post rolleyes.gif

This topic is closed to new replies.

Advertisement