return self exe name
-
- Posts: 10
- Joined: Mar 06, 2023 2:07
- Location: Arizona
- Contact:
return self exe name
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.
If so, can it be done with -lang qb?
I want my program to check if it's filename has changed.
Re: return self exe name
With command ?
Code: Select all
Print "program launched via: " & Command(0)
-
- Posts: 10
- Joined: Mar 06, 2023 2:07
- Location: Arizona
- Contact:
Re: return self exe name
That's exactly what I needed. Except it's command$(0).
I will be using it like this:
Thank you.
I will be using it like this:
Code: Select all
IF RIGHT$(COMMAND$(0), LEN(filename$)) = filename$ THEN
Re: return self exe name
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
Code: Select all
#lang "qb"
print command$(0)
print __exepath
print right$(command$(0), len(command$(0)) - len(rtrim$(__exepath, "\")) - 1)
sleep
Re: return self exe name
Code: Select all
#include "windows.bi"
#include "win/shlwapi.bi"
Dim As Zstring*256 buffer
GetModuleFileName(0,@buffer,256)
PathStripPath(@buffer)
Print buffer
-
- Posts: 10
- Joined: Mar 06, 2023 2:07
- Location: Arizona
- Contact:
Re: return self exe name
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.hhr wrote: ↑Mar 15, 2023 8:04Code: Select all
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(__exepath) - 1) sleep
Code: Select all
#lang "qb" print command$(0) print __exepath print right$(command$(0), len(command$(0)) - len(rtrim$(__exepath, "\")) - 1) sleep
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
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.Vortex wrote: ↑Mar 15, 2023 17:17Code: Select all
#include "windows.bi" #include "win/shlwapi.bi" Dim As Zstring*256 buffer GetModuleFileName(0,@buffer,256) PathStripPath(@buffer) Print buffer
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
Re: return self exe name
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:
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$
Re: return self exe name
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)
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
Re: return self exe name
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.