w32 app that recognizes itīs own name

Archives Forums/Win32 Discussion/w32 app that recognizes itīs own name

4pac(Posted 2005) [#1]
Hello folks,

I need your advice in a project that Iīm working on with BlitzPlus.
Is it possible, even with the support of C/C++/etc., to write a program that recognizes itīs own name, stores it in a string var 'xyz$', starts another app with a similar name (same name, actually, plus prefix "main_", the actual BlitzPlus app) and passes the string var 'xyz$' as a parameter on the command line?

The programm I need works as you might guess as a kind of wrapper for the main programm. It is necessary because the name of the B+ app is not known before itīs actual runtime, it is copied and renamed several times (renamed partly by random, partly by user input) - but when the app runs, itīs name is needed to be known.


LarsG(Posted 2005) [#2]

...the name of the B+ app is not known before itīs actual runtime, it is copied and randomly renamed several times...

that sounds somewhat suspecious... :/

4pac(Posted 2005) [#3]
what do you suspect? itīs a screensaver project... maybe you want to check out my website, Iīm not working on any suspicious stuff. The question I asked is the last problem that keeps me from finishing my latest project, a screensaver generator.

At the present I can only have 1 screensaver at a time in the system dir, because the ini is allways named setting.ini. I want to name it after the executable (which is itself re-named by user input befor copying it into the userīs system directory), and the executable has to be able to recongnize itīs own name to find and read the same-named ini file. But please donīt think about others ways of organizing the file structure, the implementations are to complicated. Iīd just be happy if you refer to my original question. Thanks a lot...


BlitzSupport(Posted 2005) [#4]
Try this...

Global GetModuleFileName (module_handle, fname:Byte Ptr, fname_size) "win32"

kernel32 = LoadLibraryA ("kernel32.dll")
If kernel32
	GetModuleFileName = GetProcAddress (kernel32, "GetModuleFileNameA")

Function Exe$ ()
	MAX_PATH = 260
	Local filename:Byte [MAX_PATH]
	GetModuleFileName (Null, filename, MAX_PATH)
	Return String.FromCString (filename)
End Function

Notify Exe$ ()

EDIT: Oops, BlitzMax code... see next post!

BlitzSupport(Posted 2005) [#5]
Oops, meant to be for BlitzPlus (needs addition to userlibs -- see below if unsure)...

Function Exe$ ()
	MAX_PATH = 260
	name = CreateBank (MAX_PATH)
	length = GetModuleFileName (0, name, MAX_PATH)
	If length
		For byte = 0 To length - 1
			executable$ = executable$ + Chr (PeekByte (name, byte))
	FreeBank name
	Return executable$
End Function

Notify Exe$ ()

If the userlibs file ... BlitzPlus\userlibs\kernel32.decls already exists, just add this line to it (assuming GetModuleFileName isn't listed)...
GetModuleFileName% (module_handle, fname*, fname_size) : "GetModuleFileNameA"

If ... BlitzPlus\userlibs\kernel32.decls doesn't already exist, create it using Notepad or similar (save a plain text file with that name), and add this:
; .lib "kernel32.decls"
; GetModuleFileName% (module_handle, fname*, fname_size) : "GetModuleFileNameA"