Can exe detect the path that has ran it?

Community Forums/General Help/Can exe detect the path that has ran it?

Blitzplotter(Posted 2011) [#1]
Is there a way in which an exe can detect where it has been ran from? The reason is I would like for an executable I have to source a 3d image dependant upon where it is called from.

Hmmm, I'm thinking about approaching this from another angle now and allowing for 4 different version of an exe to be started, this way each of the four executables can have a unique path to the image of choice.

Still - is there anyway of an executable knowing who 'called it' if you like?

BP


Dabhand(Posted 2011) [#2]
Print "Number of arguments = "+AppArgs.length

For a$=EachIn AppArgs
Print a$
Next


First argument is always the full file path of the exe... Strip the file name out of the path and jobs a good'un! ;)

Dabz

Last edited 2011


Floyd(Posted 2011) [#3]
In Blitz3D/Plus you use SystemProperty( "AppDir" ).

BlitzMax has global variables AppDir$ and AppFile$ and the array AppArgs[]

If you are looking for a more general (non-Blitz) solution then search the web for information about command line arguments.


Yan(Posted 2011) [#4]
Assuming you're using BMax and you are looking for a programming solution, is LaunchDir$ what you're after?

First argument is always the full file path of the exe.
Afraid not...For Windows, at least.


Dabhand(Posted 2011) [#5]
Tested on Windows XP (Added 'blah' as a extra arguement in the IDE):-


Number of arguments = 2
C:/BlitzMax/tmp/untitled1.debug.exe
blah

Process complete



Seems fine to me under the circumstances?

Dabz


Yan(Posted 2011) [#6]
I'll have to start labelling the ends of these sticks... ;o)

AppArgs[0] does indeed always contain the name of the application. However, it may or may not include the full path to the application depending on how it was launched.


Dabhand(Posted 2011) [#7]

AppArgs[0] does indeed always contain the name of the application. However, it may or may not include the full path to the application depending on how it was launched.



For future reference (For myself mostly), can you explain, because I've always thought that it was always the full path, I've always known it to be the full path.. Even programming with other languages.

I'm not being funny about it, but obviously I expect it to return the path, you say it may not... I just want to know why?

Tar!

Dabz

Last edited 2011


Yasha(Posted 2011) [#8]
Because the arguments stored in AppArgs are the arguments used to launch the application. When you launch the program from the IDE or from the graphical interface (at least in Windows), it passes the full file path in order to correctly specify the executable. But if you run the program from the command line, you don't need to use the full path, if the executable is located on your %PATH%, or is in the current directory.

Try it: using your own example code, compile an executable, navigate to that folder using a command line interface, and invoke it directly, and you'll see that the first argument is just what you used to invoke it.

C:\<snip>\My Documents>test a b c d
Number of arguments = 5
test
a
b
c
d


(Compiled as "test.exe")

Most people seem to think (I have no opinion) that needing to know this information is a Design Smell, and that you can usually work out another way of doing whatever it is you need to do.


xlsior(Posted 2011) [#9]
For future reference (For myself mostly), can you explain, because I've always thought that it was always the full path, I've always known it to be the full path.. Even programming with other languages.

I'm not being funny about it, but obviously I expect it to return the path, you say it may not... I just want to know why?


If you launch it by double-clicking the executable it 'should' always contain the path. However, if you start it from within a command prompt (which commonly done for non-GUI applications) then appargs[0] does NOT contain the path -- only the executable name itself.


xlsior(Posted 2011) [#10]
note: perhaps something that should be specified in the help file?


Dabhand(Posted 2011) [#11]
Well... Glad we cleared that up... I've always presumed the path was there while playing, but, you learn something new everyday! :)

:D

Dabz


GfK(Posted 2011) [#12]
Mass debates *snigger* about AppArgs working or not working aside, you'll want to be using CurrentDir() for this.

[edit]
Build as console app, put it anywhere, run it. Larn.
Print CurrentDir()
WaitKey
End


Last edited 2011


Yan(Posted 2011) [#13]
I may have completely misunderstood what Blitzplotter is asking but, to me at least, 'Can exe detect the path that has ran it' <> CurrentDir()*.

Consider this:

App A...
Print "CurrentDir() = " + CurrentDir()
Print "LaunchDir$   = " + LaunchDir$
...Is compiled and placed in "C:\".

Now App B, that's located in "C:\blah\blah\", calls App A.
The output would be as follows:
CurrentDir() = C:\
LaunchDir$   = C:\blah\blah\




*AFAIK, CurrentDir() always equals AppDir$ initially.


Blitzplotter(Posted 2011) [#14]
Thanks for the feedback everyone.

CurrentDir$ is the one I need, due to the fact that I'll have 4 executables in four 'installed' directories. Then, what will happen is each directory will have a 3D entity saved into it that the executable will use in tandem with the running program.

Due to the fact people can install it where they want, each executable will have a 'variable' entity that can be loaded dependant on the users choice. Bit of a configuration nightmare, but nearly there thanks to the feedback folks ;)

LaunchDir$ is one I was unaware of though, thanks for pointing it out Yan.

Last edited 2011