return self exe name

New to FreeBASIC? Post your questions here.
Post Reply
lucidapogee
Posts: 19
Joined: Mar 06, 2023 2:07
Location: Arizona
Contact:

return self exe name

Post by lucidapogee »

How do you get the name of the exe, not the exepath. Is there a command for this?
If so, can it be done with -lang qb?

I want my program to check if it's filename has changed.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: return self exe name

Post by badidea »

With command ?

Code: Select all

Print "program launched via: " & Command(0)
lucidapogee
Posts: 19
Joined: Mar 06, 2023 2:07
Location: Arizona
Contact:

Re: return self exe name

Post by lucidapogee »

That's exactly what I needed. Except it's command$(0).

I will be using it like this:

Code: Select all

IF RIGHT$(COMMAND$(0), LEN(filename$)) = filename$ THEN
Thank you.
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: return self exe name

Post by hhr »

Code: Select all

print command(0)
print exepath
print right(command(0), len(command(0)) - len(exepath) - 1)
sleep

Code: Select all

#lang "qb"
print command$(0)
print __exepath
print right$(command$(0), len(command$(0)) - len(__exepath) - 1)
sleep
Please test it in a main directory and in a folder. One must make a case distinction, maybe something like this:

Code: Select all

#lang "qb"
print command$(0)
print __exepath
print right$(command$(0), len(command$(0)) - len(rtrim$(__exepath, "\")) - 1)
sleep
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: return self exe name

Post by Vortex »

Code: Select all

#include "windows.bi"
#include "win/shlwapi.bi"

Dim As Zstring*256 buffer

GetModuleFileName(0,@buffer,256)

PathStripPath(@buffer)

Print buffer
lucidapogee
Posts: 19
Joined: Mar 06, 2023 2:07
Location: Arizona
Contact:

Re: return self exe name

Post by lucidapogee »

hhr wrote: Mar 15, 2023 8:04

Code: Select all

print command(0)
print exepath
print right(command(0), len(command(0)) - len(exepath) - 1)
sleep

Code: Select all

#lang "qb"
print command$(0)
print __exepath
print right$(command$(0), len(command$(0)) - len(__exepath) - 1)
sleep
Please test it in a main directory and in a folder. One must make a case distinction, maybe something like this:

Code: Select all

#lang "qb"
print command$(0)
print __exepath
print right$(command$(0), len(command$(0)) - len(rtrim$(__exepath, "\")) - 1)
sleep
I tested all three and they work perfectly. You're removing the exepath from the command in one line. Makes perfect sense compared to what I did. I also didn't know about using #lang "qb" instead of -lang qb. That will be useful. I also have never seen rtrim used like that. I don't think.

They output:

Code: Select all

C:\Program Files\FreeBASIC-1.09.0-winlibs-gcc-9.3.0\test3.exe
C:\Program Files\FreeBASIC-1.09.0-winlibs-gcc-9.3.0
test3.exe
Vortex wrote: Mar 15, 2023 17:17

Code: Select all

#include "windows.bi"
#include "win/shlwapi.bi"

Dim As Zstring*256 buffer

GetModuleFileName(0,@buffer,256)

PathStripPath(@buffer)

Print buffer
This approach shows how to use a function from a library. Also good to know. Is there a way to use windows.bi in -lang qb? I haven't figured it out yet.


Here's the method that I came up with. The only real advantage might be portability.

Code: Select all

#LANG "QB"

LET program$ = COMMAND$(0)
LET filename$ = ""

FOR charindex = 1 TO LEN(program$)

	LET filename$ = filename$ + MID$(program$, charindex, 1)

	IF INSTR(filename$, "\") THEN

		LET filename$ = ""

	END IF

NEXT charindex

PRINT filename$

SLEEP
END
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: return self exe name

Post by caseih »

Many win32 calls expect to deal with pointers, which you can't easily do from QB dialect. But you can create a wrapper around the win32 calls using the normal FB dialect with pointers and zstring buffers etc, which wraps the final result in a a normal FB string which can be returned from a function. Then from QB dialect code you can call that wrapper function. FB limits you to a single dialect per .BAS file but there's no reason you can't use multiple .BAS files and compile them together. Just give the compiler a list of .BAS files and it will happily compile each of them and link them together, regardless of the dialect use in each one.

Here're two .bas files that can work together, illustrating the idea:

Code: Select all

#lang "fb"

#include "crt.bi"

function test_func as string
	dim a as string
	dim b as zstring*100

	sprintf(b, "%s", "This is from sprintf via FB dialect.")
	a = b
	return a
end function

Code: Select all

#lang "qb"

declare function test_func() as string 'this is in the other .bas file

print "Hello from QB."

c$ = test_func()

print c$
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: return self exe name

Post by dodicat »

You can also pull functions out of msvcrt.dll directly if you want #lang "qb".
You can also use fb functions if needed (instrrev for example)

Code: Select all


#lang "qb"

#define myname mid$(command$(0),1+__instrrev(command$(0),any"/\"))
declare function printf cdecl alias "printf"(byval as __zstring __ptr, ...) as long

printf(!"%s %f %s\n","""hello""",timer,myname)

sleep
 
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: return self exe name

Post by caseih »

I forgot how to use zstring directly from QB. In the wiki docs, it lists the alternate names for keywords, types, functions, and subs you can use from the QB dialect.
Post Reply