Procedure OwnWBStartup()

AmigaOS specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.

Hi,

here another little Procedure. It is something like an
advanced WBStartup() command. With this procedure we are
able to get the name of the program which was started
and also get the arguments given from the Workbench.

christos asked me about this, maybe this is what you need :)

Just compile and start it ones from the CLI and ones
from the Workbench. Rename the program and start again.
Try also starting the program with some other selected
Icons from Workbench (click on the program icon so that
it is selected, hold down shift and select one or more
other Icons; not devices like any disk, just choose some
project or program icons, do a doubleclick on the last one).

Silly, it took me half a day to find an enforcer hit :-((
But now it work without any error. It is tested with CyberGuard.

NOTE !!!! -- for Fred (and all other)
Something strange happens with this program if you compile it !!
I have compiled it with the 68020 option, optimize on and also
the "save icon" option is set. If you start PureBasic and load
and compile the source the resulting file is 2164 bytes (sometimes
2154 bytes) AND THIS PROGRAM WILL CRASH. After compiling a second
time the file is 2148 bytes long and this program will run without
any problems. To avoid the crash just start PureBasic, load the source,
compile it as executable and after that compile it once again as
executable and use the second executable. Then there is no problem.
I don't know why PureBasic create a bigger executable at the first time
which will crash ?!?!?!!? Fred, help !!



Procedure.l OwnWBStartup()
; This procedure replaces the PureBasic WBStartup() function.
; It is doing the same like WBStartup() (i hope! Have a look
; Fred), except that we can access the name of the program which
; is started from Workbench and also given arguments from the Workbench.
;
; parameters : none
; returns : 0 if the program ist started from CLI
; >0 returns the received wbstartupmessage send by Workbench after
; starting the program.
; You have to store this message and free it at program end
; with the Exec function "ReplyMsg_(message)". See example.
; If you do not so the memory of the message will not be freed (bad!!!).
; If we receive the return with a message then there is
; also a List available which contains the arguments given
; from the Workbench at program start. The List is called
; MyWBArg() and is declared as "Shared" from the procedure,
; so we are able to access it in the mainprogram.
; Each entry of the List contains two variables:
;
; MyWBArg()\filelock Long, contains the filelock for the
; followed argument. Simple said it
; is the directory where the followed
; argument can be found (very simple said!)
; Note that this pointer isn't a "normal"
; pointer. It is a BCPL-Pointer.
; Earlier versions of the AmigaDOS were
; written in this programming language.
; A BCPL pointer stores an address/4.
; Simple eg.: address is 4000; in BCPL it is
; 4000/4=1000. So if you want to write a
; BCPL pointer just divide the address with 4.
; If you want convert a BCPL pointer to a "normal"
; pointer just multiply it with 4. If you have
; question about BCPL pointer and also BCPL strings
; just ask me.
; MyWBArg()\namearg String, the name of the argument.
;
; The first element of the List is always our own program.
; Here we can found the name of our program and our program
; dir. The procedure declares the programdir as currentdir.
; For further informations see the comments in the procedure.
;
; How to use the filelock stored in our list:
; The filelock can be used with the dos funtion CurrentDir().
; Little example:
; You have received two arguments from the workbench. The
; first one is our program and the second one a textfile
; which icon was selected at programstart (you know: click
; at the program icon, hold down "Shift" and select
; one or more other icons and doubleclick on the last one).
; To load this files do following:
; 1. change the currentdir to the dir of the Argument
; 2. load the file
; olddir.l=CurrentDir_(MyWBArg()\filelock) ; set dir as new currentdir
; ; and save our programdir in olddir
; result.l=ReadFile(0,MyWBArg()\namearg) ; read the file
; CurrentDir_(olddir) ; change currentdir back to our programdir
;
;
; Requirements: The procedure uses functions from the dos and exec.library,
; so this libs must be open (see example). PureBasic closes
; this at the program end.
;
owntask.l=FindTask_(0) ; we use exec/FindTask() to find our own process (own process=0)
; the return is a pointer to a process structure
prcli.l=PeekL(owntask+172) ; at offset 172 of the process structure a field "pr_cli" ist present
If prcli=0 ; if here is placed a 0 the program is started from WB, otherwise it
; contains a pointer to "CommandLineInterface"
messageport.l=owntask+92 ; our process structure contains of course a messageport. the WB
; sends the needed WBstartup-Message to this port. The messageport
; is found at offset 92
WaitPort_(messageport) ; we use exec/WaitPort() for waiting until a message receives or
; one is already available
wbstartupmessage.l=GetMsg_(messageport) ; we use exec/GetMsg() to take this message (this
; will also remove the message from the list of
; waiting messages
If wbstartupmessage0 ; just testing if something real is received
Structure WBArgs ; now we declare a new structure (for the arguments)
filelock.l ; we need the filelock (the dir of the argument
namearg.s ; and also the name of the argument
EndStructure
NewList MyWBArg.WBArgs() ; now we can create a list of these, we are using
; a list because we don't know how much arguments
; are given from the WB
Shared MyWBArg ; now we "share" the List, to access the list from
; mainprogram
numargs.l=PeekL(wbstartupmessage+28) ; at offset 28 of the message we can found a long
; number which represents the number of arguments
ptrwbarg.l=PeekL(wbstartupmessage+36) ; at offset 36 of the message we can found a pointer
; to a WBArg structure. This structure is 8 bytes long
; and at offset 0 we can find a long which contains the
; the lock; at offset 4 we can find a pointer to the
; string which contains the name of the argument
; if there are more than 1 argument the next WBArg
; will follow after the first one
For i.w=1 To numargs ; with this loop we read all given arguments
AddElement(MyWBArg()) ; new entry of our list (the first one)
MyWBArg()\filelock=PeekL(ptrwbarg) ; now we store the filelock of the argument
; to our list
MyWBArg()\namearg=PeekS(PeekL(ptrwbarg+4)) ; and also the name of the argument
; normally the WBArg structure contains a
; pointer to the string, but i think it is
; better and easier if we store the name
; of the argument directly in our list
ptrwbarg+8 ; now add 8 to the pointer of the WBArg structure
; to access the next one. You remember:
; the WBArg structure is 8 bytes long
; and if there more than one then the next
; one follows directly after the first one
; and so on. so we add a offset of 8 for the next
Next i ; ok, all the same with the next WBArg structure
FirstElement(MyWBArg()) ; now we set our List to the first entry
; the first one contains our program name
; and the program dir
CurrentDir_(MyWBArg()\filelock) ; we use the dos/CurrentDir() function to declare
; our program dir as the current dir
; this because so we are able to load the ToolTypes
; of our program icon or we load some datafiles
; which are placed in our program directory etc. etc.
EndIf
Else
ProcedureReturn 0 ; we return a 0 to the mainprogram which means
; that the program is started from the CLI
EndIf
ProcedureReturn wbstartupmessage ; return the wbstartup message to the main program
; at the program end we have to reply it (see example)
EndProcedure

ExecBase.l=OpenExecLibrary_(36) ; Open the exec.library
DosBase.l=OpenDosLibrary_(36) ; open the dos.library
If ExecBase0 And DosBase0 ; both are open ?
message.l=OwnWBStartup() ; call our Procedure
If message=0 ; oh, the program is started from CLI
PrintN("This program ist started from CLI") ; blah blah
; here you can check for arguments from CLI etc. and so on ...
Else ; yeah, our program ist started from WB !
numofarg.w=CountList(MyWBArg()) ; how many arguments are available ?
; now we put some text together
text$="The program is started from WB"+Chr(10)
text$+"Program name is »"+MyWBArg()\namearg+"«"+Chr(10)
text$+"There are "+Str(numofarg)+" argument(s) (the first is the started program)."+Chr(10)+Chr(10)
text$+"Here the arguments:"+Chr(10)
Repeat ; we read arguments until the list ends
text$+MyWBArg()\namearg+Chr(10) ; some text (the arguments)
Until NextElement(MyWBArg())=0
EasyRequester("Info",text$,"ok") ; we display the requester
ReplyMsg_(message) ; now we are ready and reply the message (to free the message)
EndIf
Else
PrintN("Cannot open exec/dos.library")
EndIf
End


Maybe you will find this procedure useful or you have some hints, tips etc.
Just tell me everything :))

Greetings..
Roxxler
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

Roxxler rox!

Keep on with your Amiga-PB-Procedures!

regards,
Redacid
---
Only Amiga makes it possible!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

Thank you roxxler

its EXACTLY what i was looking for :D


christos


Edited by - plouf on 09 February 2002 19:52:59
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.
Roxxler rox!

Keep on with your Amiga-PB-Procedures!

regards,
Redacid
Hi,

:-) Yes, i will do or better i will try.

Greetings ..
Roxxler
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.
Thank you roxxler

its EXACTLY what i was looking for :D
christos
Hi,

that's fine :) Have a look at the little updated OwnWBStartup()
and also to the new one named CheckWBStartup().

Greetings ..
Roxxler
Post Reply