Code-Beispiel
Install Script
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
LGPL | croco97 | 09.12.2007 |
' *******************************************************************
' Example for install scripts with freebasic
'
' By Croco and some clever guy from forum.qbasic.at, who
' provided the basic routine for registry access.
'
' Compiled with fbc 0.17b
'
' November 2007
' *******************************************************************
' *******************************************************************
' Introduction
' -------------
' I wrote the following code "quick and dirty" for an install script to do
' the installation of a web application server (Apache Tomcat), a web service and
' the application itself (a dynamic pdf created with Crystal Xcelsius) at once
' without too much bothering the user.
'
' Install scripts are needed when the facilities of standard installer build programs (I used
' www.gksetup.com) are not sufficient. In this case the installer had to start an additional
' setup, find the current version of java runtime, fetch the appropriate path to java home
' and write it to system environment variable before launching the just installed server
' program and the application itself.
' It are these kind of tasks - read and write registry things and fire up external commands -
' where freebasic proved to be quite smart.
OPTION EXPLICIT
OPTION ESCAPE
' Some shotcuts
CONST HKEY_CLASSES_ROOT=&H80000000
CONST HKEY_CURRENT_USER=&H80000001
CONST HKEY_LOCAL_MACHINE=&H80000002
CONST HKEY_USERS=&H80000003
CONST HKEY_CURRENT_CONFIG=&H80000005
CONST HKCR=&H80000000 'Vbs-Abkürzung
CONST HKCU=&H80000001 'Vbs-Abkürzung
CONST HKLM=&H80000002 'Vbs-Abkürzung
CONST HKUS=&H80000003 'Meine Abkürzung
CONST HKCC=&H80000005 'Meine Abkürzung
CONST READ_CONTROL AS INTEGER=&H20000
CONST KEY_NOTIFY AS INTEGER=&H10
CONST KEY_ENUMERATE_SUB_KEYS AS INTEGER=&H8
CONST KEY_QUERY_STRING AS INTEGER=&H1
CONST KEY_QUERY_VALUE AS INTEGER=&H1
CONST STANDARD_RIGHTS_READ AS INTEGER=(READ_CONTROL)
CONST SYNCHRONIZE AS INTEGER=&H100000
CONST KEY_READ AS INTEGER=((STANDARD_RIGHTS_READ OR KEY_QUERY_VALUE OR KEY_ENUMERATE_SUB_KEYS OR KEY_NOTIFY) AND (NOT SYNCHRONIZE))
'Registry access functions
DECLARE FUNCTION RegOpenKeyEx LIB "advapi32.dll" ALIAS "RegOpenKeyExA" (BYVAL hKey AS INTEGER, BYVAL lpSubKey AS STRING, BYVAL ulOptions AS INTEGER, BYVAL samDesired AS INTEGER, BYREF phkResult AS INTEGER) AS INTEGER
DECLARE FUNCTION RegQueryValueEx LIB "advapi32.dll" ALIAS "RegQueryValueExA" (BYVAL hKey AS INTEGER, BYVAL ValueName AS STRING, BYVAL Reserved AS INTEGER, lpType AS INTEGER, BYVAL lpString AS STRING, lpDataSize AS INTEGER) AS INTEGER
DECLARE FUNCTION RegCloseKey LIB "advapi32.dll" ALIAS "RegCloseKey" (BYVAL hKey AS INTEGER) AS INTEGER
DECLARE FUNCTION ReadRegKey(BYVAL OpenKey AS INTEGER, BYVAL SubKey AS STRING, BYVAL ValueName$="") AS STRING
' -----------------------------------------------------------
Sub plog(s as String)
'Prints messages on screen and in the log file
? s
? #8,s
End Sub
' -----------------------------------------------------------
FUNCTION ReadRegKey(BYVAL HKey AS INTEGER, BYVAL SubKey AS STRING, BYVAL ValueName AS STRING="") AS STRING
'Routine to read registry entries via direct access
'Got this from qbasic.at
DIM AS INTEGER i, lpType=0, ReturnKey, DataSize=255
DIM keyvalue AS ZSTRING*1024
DIM TOMWTB AS STRING="", ESC AS STRING="\"
IF INSTR(SubKey &ValueName, ESC) THEN
TOMWTB=ESC
ELSE
KeyValue=SPACE$(1024)
IF RegOpenKeyEx(HKey, SubKey, 0, KEY_READ, ReturnKey) THEN
TOMWTB="ERROR: RegOpenKeyEx(" & HEX$(HKey) & SubKey & ValueName &")\r\n"
ELSE
IF RegQueryValueEx(ReturnKey, ValueName, 0, lpType , KeyValue, Datasize) THEN
TOMWTB=TOMWTB+"ERROR: RegQueryValueEx\r\n"
END IF
IF RegCloseKey(ReturnKey) THEN TOMWTB="ERROR: RegCloseKey\r\n"
END IF
END IF
IF TOMWTB<>"" THEN
FUNCTION="Null"
?TOMWTB
ELSE
FUNCTION=LEFT$(KeyValue, DataSize-1)
END IF
END FUNCTION
FUNCTION ExistRegKey(BYVAL HKey AS INTEGER, BYVAL SubKey AS STRING) AS INTEGER
'Checks, whether the given registry path (HKey + SubKey) exists.
DIM AS INTEGER i, lpType=0, ReturnKey, DataSize=255
DIM keyvalue AS ZSTRING*1024
DIM TOMWTB AS STRING="", ESC AS STRING="\"
FUNCTION=1
IF RegOpenKeyEx(HKey, SubKey, 0, KEY_READ, ReturnKey) THEN FUNCTION=0
END FUNCTION
' -----------------------------------------------------------
Function getjavahome as String
'Gives path to current java runtime
dim as integer HK
dim as String key, keyvalue, retval
'Look first in Web Start for the current version
'(At least in my system this current version overwrites thisone in Runtime Env)
HK=HKLM
key="Software\\JavaSoft\\Java Web Start\\"
keyvalue="CurrentVersion"
retval=ReadRegKey(HK, key, keyvalue)
if (retval="Null") then
key="Software\\JavaSoft\\Java Runtime Environment\\"
keyvalue="CurrentVersion"
retval=ReadRegKey(HK, key, keyvalue)
end if
if (retval="Null") then
plog( "Error: Path for Java Runtime Environment not found")
plog( "Please install Java Runtime")
plog( "Installation terminates here.")
end
else
'Look up in the Runtime Environment to extract the path
key="Software\\JavaSoft\\Java Runtime Environment\\"+retval
keyvalue="JavaHome"
retval=ReadRegKey(HK, key, keyvalue)
end if
'Check on Backslash at the end of the JRE path
'Has to have one, because tomcat 5.5 does expect one.
if (mid$(retval,len(retval),1)<>"\\") then
retval=retval+"\\\\"
end if
Function=retval
End Function
' -----------------------------------------------------------
Function gettomcathome(tomcat_key as String) as String
'Fetch the path to tomcat home.
'The registry path to tomcat is given in tomcat_key
'Example: "Software\\Apache Software Foundation\\Tomcat 5.5"
dim as integer HK
dim as String key, keyvalue, retval
'Look first in Web Start for the current version
'(At least in my system this current version overwrites thisone in Runtime Env)
HK=HKLM
if (len(tomcat_key)=0) then
plog( "Error: Tomcat registry entry inconsistent")
plog( "Installation terminates now.")
sleep
end
end if
key=tomcat_key
keyvalue="InstallPath"
retval=ReadRegKey(HK, key, keyvalue)
if (retval="Null") then
plog( "Error: Tomcat registry entry for path not found")
plog( "Installation terminates now.")
sleep
end
end if
Function=retval
End Function
' -----------------------------------------------------------
Sub insertstr(byref s as String, ins1 as String, pos1 as integer)
'Inserts a char into a string at position pos1
s=left$(s,pos1-1)+ins1+right$(s,len(s)-pos1+1)
End Sub
' -----------------------------------------------------------
Sub insert_allchar(byref s as String, finds as String, repl as String)
'Serves to double backslashes.
'Whereever in the string appears <finds>, insert <repl> behind.
dim as integer i, slen, slen2
slen=len(s)
slen2=len(repl)
for i=1 to slen
if mid$(s,i,1)=finds then
insertstr(s,repl,i)
i=i+slen2
end if
next i
End Sub
' -----------------------------------------------------------
Sub write_reg_javahome(path as String)
'Write a .reg-file to disk
'Executed it will write the given path as system environment variable "JAVA_HOME",
'needed by Tomcat.
dim as String s
insert_allchar(path,"\\","\\")
open "a.reg" for output encoding "UTF-16" as #1
s="Windows Registry Editor Version 5.00"
? #1,s
? #1,""
s="[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment]"
? #1,s
s=chr$(34)+"JAVA_HOME"+chr$(34)+"="+chr$(34)+path+chr$(34)
? #1,s
close(1)
End Sub
' -----------------------------------------------------------
Function look_tomcat(tomcat_key as String)
'Finds the current tomcat version
'and returns the registry path in tomcat_key
'Example: "Software\\Apache Software Foundation\\Tomcat 5.5"
dim as integer i,j,found1
dim as String key, ver1
plog( "Looking for Tomcat Installation ...")
tomcat_key=""
found1=0
if (ExistRegKey(HKLM, "Software\\Apache Software Foundation\\Tomcat")=1) then
for i=3 to 8
for j=0 to 1
if (j=0) then ver1=ltrim(str$(i))+".0" else ver1=ltrim(str$(i))+".5"
key="Software\\Apache Software Foundation\\Tomcat\\"+ver1
'? key
if (ExistRegKey(HKLM, key)=1) then
found1=1
plog( "Tomcat Ver. "+ver1+" found")
tomcat_key=key
end if
next j
next i
end if
Function=found1
End Function
' -----------------------------------------------------------
Function look_adobereader as String
'Looks for the current acrobat reader version
'returns the version as string
dim as integer i,j,found1
dim as String key, ver1,acrobatver
acrobatver=""
found1=0
if (ExistRegKey(HKLM, "Software\\Adobe\\Acrobat Reader")=1) then
for i=3 to 10
for j=0 to 1
if (j=0) then ver1=ltrim(str$(i))+".0" else ver1=ltrim(str$(i))+".5"
key="Software\\Adobe\\Acrobat Reader\\"+ver1
'? key
if (ExistRegKey(HKLM, key)=1) then
found1=1
plog( "Adobe Ver. "+ver1+" found")
acrobatver=ver1
end if
next j
next i
end if
Function=acrobatver
End Function
' -----------------------------------------------------------
Sub main
dim as String s, tomcat_key, tomcat_path, drive,_QU,_CR
_QU=chr(34)
_CR=chr(13)+chr(10)
'Open logfile
open "install_log.txt" for output as #8
plog( "==========================================================================")
plog( "Installation Script Demo)")
plog( "==========================================================================")
plog( "")
? "The routine looks for the following files in the current directory:"
? "* apache-tomcat-5.5.25.exe"
? "* demo.war"
? "* appl.pdf"
plog( "")
if (look_tomcat(tomcat_key)=0) then
plog( "No tomcat installation found")
plog( "Install tomcat 5.5")
'Shell("cmd /c instdummy.exe")
Shell("cmd /c apache-tomcat-5.5.25.exe")
plog( "Tomcat installed...")
plog( "Look for JAVA_HOME system variables")
if (ExistRegKey(HKLM, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\JAVA_HOME")=0) then
plog( "No JAVA_HOME found")
s=getjavahome()
plog( "Java Runtime path found:")
plog(s)
plog( "Write it to JAVA_HOME")
write_reg_javahome(s)
shell("cmd /c a.reg")
end if
look_tomcat(tomcat_key)
end if
plog( "Get Tomcat Home")
tomcat_path=gettomcathome(tomcat_key)
plog( "Stop Tomcat Service")
s="cmd /c net stop tomcat5"
shell(s)
plog( "Wait for stop...")
sleep 5
plog( "Copy WAR-File")
's="cmd /c copy ffashion_testmodel.wa2 "+chr(34)+tomcat_path+"\\webapps"+chr(34)
s="cmd /c copy demo.war "+chr(34)+tomcat_path+"\\webapps"+chr(34)
plog( s)
shell(s)
plog( "Start Tomcat Service")
s="cmd /c net start tomcat5"
shell(s)
plog( "Start Tomcat Service Monitor")
's=chr(34)+"cmd /c "+tomcat_path+"\\bin\\tomcat5w //MS//"+chr(34)
drive=mid$(tomcat_path,1,2)
's="cmd /c cd "+chr(34)+tomcat_path+"\\bin\\"+chr(34)+" && "+drive+" && start tomcat5w.exe //MS//"
open "start_tommonitor.cmd" for output as #1
? #1,"cd "+_QU+tomcat_path+"\\bin\\"+_QU
? #1,drive
? #1,"start tomcat5w.exe //MS//"
close #1
shell ("start_tommonitor.cmd")
plog( "Check for Adobe Acrobat Reader 8 or newer")
if (val(mid$(look_adobereader(),1,1))<8) then
plog( "No Acrobat Reader or too old version installed.")
plog( "Mininum Acobrat Reader Version 8.0 required.")
plog( "This analysis didn't check for Acrobat alternatives")
plog( "Installation is so far finished. Please start IBE-pdf manually.")
plog( "Press key")
sleep
end
else
plog( "Start application" )
s="start appl.pdf"
shell(s)
end if
plog( "Installation successfully finished.")
close(8)
? "Press key."
sleep
End Sub
'test4
main
'test
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|