Sign in to follow this  
GameCreator

Working Directory Changes

Recommended Posts

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:

[CODE]
#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();
}
[/CODE]

And here's the code, exe & 2008 Express project files:
[url="http://www.mediafire.com/?b73k885hxs8sx9a"]http://www.mediafire...b73k885hxs8sx9a[/url]

Any help is appreciated. Thanks!

Share this post


Link to post
Share on other sites
It probably is correctly detecting the working directory. The working directory can and will change depending on how you start a program.

Share this post


Link to post
Share on other sites
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. Edited by GameCreator

Share this post


Link to post
Share on other sites
On Windows you can use [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx]GetModuleFileName()[/url] 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.

Share this post


Link to post
Share on other sites
Thank you. Now I'm trying to figure out why the following only returns a single letter:

[CODE]TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,MAX_PATH);
printf("working directory: %s \n\n",buffer);[/CODE]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. Edited by edd²

Share this post


Link to post
Share on other sites
EDIT: sorry, accidentally quoted myself rather than editing typo in original post [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img] Edited by edd²

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this