My exe wants to know its name!

Blitz3D Forums/Blitz3D Programming/My exe wants to know its name!

bytecode77(Posted 2008) [#1]
hello .)
my executable i created is very nosey and wants to know its own name! does anybody here know how my exe can find out about its own name?
thanks ;)


PowerPC603(Posted 2008) [#2]
You can use a trick if there is only 1 exe in your directory.
You can find out from where your program is running using:
MyDir$ = SystemProperty("appdir")

Then browse that directory to find a file with extention ".exe".
Then you've got your entire filename.
Cut the ".exe" from the filename and your program will know it's name.


bytecode77(Posted 2008) [#3]
hm. what if an even stupider user renames the exe file and there are other exes in the directory?


bytecode77(Posted 2008) [#4]
there must be some tricks using the window api, right? i mean - it's even possible to self-destruct the used executable...


BlitzSupport(Posted 2008) [#5]
You can do this:

; -----------------------------------------------------
; Add next two lines to kernel32.decls if needed...
; -----------------------------------------------------
; .lib "kernel32.dll"
; GetModuleFileNameA% (module, name*, size)
; -----------------------------------------------------

; Bank for path name...

Const MAX_PATH = 260 ; Windows max path size!
pbank = CreateBank (MAX_PATH)

; Write executable name to bank...

psize = GetModuleFileNameA (0, pbank, MAX_PATH)

; Read characters in bank...

program$ = ""
For byte = 0 To psize - 1
	program$ = program$ + Chr (PeekByte (pbank, byte))
Next

FreeBank pbank

; Ta-da...

RuntimeError "Path: " + Chr (34) + program$ + Chr (34)


If it doesn't run then you need to create a plain text file called kernel32.decls in Blitz3D\userlibs and add the two lines at the top of this code. If it already exists, just add the GetModuleHandleA line to it.

(It'll show blitzcc.exe while running from the IDE, but the real name when run as a standalone executable.)


bytecode77(Posted 2008) [#6]
hm, kernel :) ... works well!!!
thank you very much ;)