Page 1 of 1

Get current app name and command line switches

Posted: Thu Aug 16, 2001 11:23 am
by BackupUser
Restored from previous forum. Originally posted by PB.

Code updated for 5.20+

Here's a two-line quicky that puts your exe name in app$ and command line
switches into cmd$ (I just learnt it thanks to Mr Skunk). It assumes that
you're using the latest version of PB because the default type is a long.
If not, change all "r" to "r.l" before using it.

Code: Select all

r=GetCommandLine_() : app$=PeekS(r) : r=FindString(app$," ",1)
If r>0 : cmd$=Mid(app$,r+1,255) : app$=Left(app$,r-1) : EndIf
Debug app$
Debug cmd$

Edited by - PB on 20 September 2001 04:24:32

Posted: Thu Aug 16, 2001 12:01 pm
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Just becareful one thing.

The path or name of the App can have blank spaces (chr(32)), then the string (PATH+EXE name) is inside (").

"c:\Program Files\MyProg\Myprog.exe" command1 command2

It is more secure to test (") before than space or else a part of your path will be considered as a part of the command line...


Mr Skunk

Edited by - mr.skunk on 16 August 2001 13:03:14

Posted: Thu Aug 16, 2001 6:18 pm
by BackupUser
Restored from previous forum. Originally posted by PB.
The path or name of the App can have blank spaces [snip]
The routine above only returns the app name, and not the path.
Is it meant to return the path also? It doesn't for me...

But yes, the name could have spaces which would cause problems.
I based my tip on my own app which is a single-word name. :)

Posted: Thu Aug 16, 2001 10:18 pm
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

I'm under windows 98, and GetCommandLine() always returns the PATH+NAME+ARGUMENTS string.

it perhaps works differently under another windows version. What windows version are you using?

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Posted: Fri Aug 17, 2001 4:29 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

I use Windows 2000 Pro at home, and it definitely returns just NAME+PARAMS.

However, I bought an old laptop today with Windows 95 on it, and that returns PATH+NAME+PARAMS.

As such, a check of the OS before using it will be needed...


Edited by - PB on 20 September 2001 04:24:41

Posted: Fri Aug 17, 2001 5:18 pm
by BackupUser
Restored from previous forum. Originally posted by Paul.

I too am using Windows 2000 Pro and GetCommandLine_() returns PATH+NAME+ARGUMENTS for me. I also just tested this on Win95 and Win98 with the same results. Not sure how you're getting just NAME+ARGUMENTS ??

Paul.

Posted: Sat Aug 18, 2001 5:10 am
by BackupUser
Restored from previous forum. Originally posted by PB.
I too am using Windows 2000 Pro and GetCommandLine_() returns PATH+NAME+ARGUMENTS for me. I also just tested this on Win95 and Win98 with the same results. Not sure how you're getting just NAME+ARGUMENTS ??
Okay, here's the deal: Make a new exe (called test.exe) from this code:

r=GetCommandLine_() : app$=peeks(r) : r=FindString(app$," ",1)
If r>0 : cmd$=mid(app$,r+1,255) : app$=left(app$,r-1) : EndIf
MessageRequester("Test","AppName: "+app$+chr(13)+"Cmdline: "+cmd$,0)
End


Next, put this exe in C:\Program Files\PureBasic\test.exe

Now, open a DOS box in C:\ (without CD'ing to another folder) and enter: "C:\Program Files\PureBasic\test.exe" wow. You'll get an AppName of C:\Program and Cmdline of Files\PureBasic\test.exe" wow.

Now, in the DOS box, CD to the actual exe (so that when you type dir you see the exe itself. Then, enter: test wow. This time, you'll get an AppName of test and Cmdline of wow.

So, it would appear that the result returned depends highly on where you launch the exe from, ie. the same or a remote folder.


Edited by - PB on 20 September 2001 04:25:04

Posted: Sat Aug 18, 2001 1:28 pm
by BackupUser
Restored from previous forum. Originally posted by Mr.Skunk.

Sorry, but if i launch the example from "c:\" or "c:\program files\purebasic\" i have always the complete path displayed on w98...
strange

See you soon in september...

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Posted: Sat Aug 18, 2001 4:14 pm
by BackupUser
Restored from previous forum. Originally posted by Paul.

After trying your test, I did get the same results as you. It does seem to depend on where you launch your app from and on what platform.
To eliminate this problem, why not try a different approach to finding the command line 'switch'?

Something like this:

Code: Select all

cmd.l=GetCommandLine_()
cli.s=peeks(cmd)
lencli=len(cli)
tmp=2
currentpos=0
While tmp<=lencli    
  If mid(cli,tmp,1)="/"
    currentpos=tmp
    tmp=lencli
  EndIf
  tmp=tmp+1
Wend
cli=mid(cli,currentpos,lencli)

messagerequester("Result",cli,0)

End 
Save it as 'TEST.EXE' and then pass a variable using TEST.EXE /wow
The program will always return /wow no matter what folder you are in.

Posted: Sat Aug 18, 2001 4:28 pm
by BackupUser
Restored from previous forum. Originally posted by Paul.

If you want to get fancy you could add this after the above code and it will seperate multiple switches and remove the '/' symbol:

Code: Select all

Dim res.s(20)
tmp=0
res(tmp)=""
For tmploop=1 To Len(cli)
  t.s=mid(cli,tmploop,1)
  If t="/"
    tmp=tmp+1
    res(tmp)=""
    Else
    res(tmp)=res(tmp)+t
  EndIf
Next
messagerequester("Result",res(1)+chr(13)+res(2)+chr(13)+res(3),0)
End
Save the file as 'TEST.EXE" and pass variables using
TEST.EXE /hello/this works/goodbye

The results are stored in 'res(#)'
res(1)=hello
res(2)=this works
res(3)=goodbye

(this example will hold up to 20 variables)