AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help, my script won't wait for commands to finish

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Aero98



Joined: 08 Jan 2009
Posts: 81
Location: Oklahoma City

PostPosted: Thu Jan 08, 2009 11:29 pm    Post subject: Help, my script won't wait for commands to finish Reply with quote

I am trying to write a script that Automatically downloads and installs SQL '05 express with some command line options. I have broken it into to different scripts for multitasking purposes (one file, the gui, starts the process and calls for the downloads/installations, the second file just downloads and saves whatever it is told to)

What I have setup is a function that detects a 32 or 64 bit os and starts the download/Install for that OS, it calls for the download of sp1 and monitors the download, launches it and then starts the download os SP2 while SP1 is installing, it should Then STOP and wait for SP1 to finsh installing, once it is finished installing it should launch the install for SP2 and start the download for the next file, wait for the sp2 to finish and then start the install for the next process, what it is doing is downloading each file, running it, downloading the next file and immediately running it, it is not waiting for the first file to finish like it should and I cannot figure out why.

Can anyone help me, I am new at this by the way so if it is simple I am sorry. Laughing

Main file:
Code:
#NoTrayIcon
#NoEnv
SendMode Input 
SetWorkingDir %A_ScriptDir% 
SetTitleMatchMode, Slow

Gui, Add, Text, x26 y160 w350 h20 VStatus,
Gui, Add, Button, x26 y115 w110 h30 , Install SQL
Gui, Add, Text, x26 y10 w470 h100 , This utility will automatically download and install the correct version of Microsoft SQL Server 2005 Express for the alamode instance`n`nOnce the install is complete this utility will stop the service and install Blank databases`n`nIf you need to install a different set of database please wait for this utility to finish and replace the current databases with ones of your choosing.
Gui, Color, White
Gui, Show, h203 w545, a la mode's Microsoft SQL server express installer
GuiControl,, Status, Ready . . .
Return

GuiClose:
ExitApp


ButtonInstallSQL:
GuiControl,, Status, Warning . . .
msgbox, Starting the installation process`n`nOnce begun the process cannot be interupted`n`nPlease do not use the pc once the process has begun
GuiControl,, Status, Detecting if the OS is 64 or 32 bit
ThisProcess := DllCall("GetCurrentProcess")
; If IsWow64Process() fails or can not be found,
; assume this process is not running under wow64.
; Otherwise, use the value returned in IsWow64Process.
if !DllCall("IsWow64Process", "uint", ThisProcess, "int*", IsWow64Process)
    IsWow64Process := false
gosub % IsWow64Process ? "win64" : "win32"

return



win32:
Gui +OwnDialogs
msgbox, Your running a 32 bit system`n`nInstalling SQL for 32 Bit systems
download(55128,"http://download.microsoft.com/download/1/d/8/1d8461a7-4434-4d42-a7e1-246f3f9f7c3b/SQLEXPR.EXE","SQLEXPR.exe", "Service Pack 1")
GuiControl,, Status, Installing Microsoft SQL server Service Pack 1
Run, SQLexpr.exe /qb ADDLOCAL=ALL INSTANCENAME=ALAMODE SECURITYMODE=SQL SAPWD=alamode DISABLENETWORKPROTOCOLS=0,,,OPID1
msgbox, SQL SP PID = %OPID1%
sleep, 500
WinWait, ahk_pid %OPID1%,,120,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 1 failed to download and launch in a timely fasion. `n`nPlease download and install SQL Manually.
    return
}

sleep, 1000
download(55128,"http://download.microsoft.com/download/e/a/4/ea4b699b-bec4-4722-96d3-254580ed7f9e/SQLEXPR.EXE","SQLEXPR-SP2.exe", "Service Pack 2")
SP1:
WinWaitClose, ahk_pid %OPID1%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 1 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}
msgbox, did %OPID1% close?
IfWinExist, ahk_pid %OPID1%
gosub SP1


GuiControl,, Status, Installing Microsoft SQL server Service Pack 2
sleep, 1000
Run, SQLexpr-sp2.exe /qb INSTANCENAME=ALAMODE UPGRADE=SQL_Engine SAPWD=alamode,,,OPID2
WinWait, ahk_pid %OPID2%,,120,,
download(55128,"http://download.microsoft.com/download/4/4/D/44DBDE61-B385-4FC2-A67D-48053B8F9FAD/SQLServer2005_BC.msi","SQLServer2005_BC.msi", "MSDE Support")
SP2:
WinWaitClose, ahk_pid %OPID2%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 2 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}

IfWinExist, ahk_pid %OPID2%
gosub SP2

GuiControl,, Status, Installing Microsoft SQL server MSDE Support
sleep, 1000
Run, SQLServer2005_BC.msi /passive,,,OPID
WinWait, ahk_pid %OPID%,,120,,
BW:
WinWaitClose, ahk_pid %OPID%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server MSDE Support failed to install in a timely fasion. `n`nPlease install the MSDE Support Manually.
    return
}

IfWinExist, ahk_pid %OPID%
gosub BW

Service_Stop(MSSQL$ALAMODE)
sleep, 100
runwait, regedit /s fix.reg
filecopy, %A_WorkingDir%\wintotal.*, %A_ProgramFiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data
sleep, 1000
Service_Start(MSSQL$ALAMODE)
sleep, 2000
GuiControl,, Status, Verifying WinTOTAL's Databases
Runwait, %wt%\utils\wtverifydb.exe /attachdb
GuiControl,, Status, Updating WinTOTAL's Databases
Runwait, %wt%\utils\wtverifydb.exe /ver=46
GuiControl,, Status, Done . . .

Msgbox, The installation of Microsoft SQL server Completed Successfully
Return





win64:

MSGbox, you are running a 64 bit system
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                 ;;
;; All Functions are listed below  ;;
;;                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Download(w,x,y,z)   ;;v = step #, w = filesize, x = url, y = save as name, z = Download window text
{
Progress, b1 w500 CWFFFFFF Range0-%w%, Downloading Microsoft SQL Server 2005 %z%,
run, SQLdownloader.exe %x% %y%

sleep, 1000

SQLEXPR:
FolderSizeMB = 0
Loop, %y%
    Progress, %A_LoopFileSizeKB% 
     


Loop, %y%
FolderSizeMB += %A_LoopFileSizeKB%

IfGreater, FolderSizeMB, %w%, goto exit

sleep, 50
gosub SQLEXPR
return

EXIT:
sleep, 100
Progress, Off
return
}


/* Windows Service Control Functions
-heresy

- Return Values
     1 : Success
     0 : Failure
    -4 : Service Not Found

- State codes from Service_State()
    SERVICE_STOPPED (1) : The service is not running.
    SERVICE_START_PENDING (2) : The service is starting.
    SERVICE_STOP_PENDING (3) : The service is stopping.
    SERVICE_RUNNING (4) : The service is running.
    SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
    SERVICE_PAUSE_PENDING (6) : The service pause is pending.
    SERVICE_PAUSED (7) : The service is paused.
*/

MsgBox % Service_State("Alerter")


Service_List(State="", Type="", delimiter="`n"){
    if !State
        ServiceState := 0x3 ;SERVICE_STATE_ALL (0x00000003)
    else if (State="Active")
        ServiceState := 0x1 ;SERVICE_ACTIVE (0x00000001)
    else if (State="Inactive")
        ServiceState := 0x2 ;SERVICE_INACTIVE (0x00000002)
    else
        ServiceState := 0x3
   
    if !Type
        ServiceType := 0x30 ;SERVICE_WIN32 (0x00000030)
    else if (Type="Driver")
        ServiceType := 0xB ;SERVICE_DRIVER (0x0000000B)
    else if (Type="All")
        ServiceType := 0x3B ;sum of both
    else
        ServiceType := 0x30
       
    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0
                        , "Int", 0
                        , "UInt", 0x4) ;SC_MANAGER_ENUMERATE_SERVICE (0x0004)   

    DllCall("advapi32\EnumServicesStatusA"
        , "UInt", SCM_HANDLE
        , "UInt", ServiceType
        , "UInt", ServiceState
        , "UInt", 0
        , "UInt", 0
        , "UIntP", bSize ;get required buffer size first
        , "UIntP", 0
        , "UIntP", 0)
   
    VarSetCapacity(ENUM_SERVICE_STATUS, bSize, 0) ;prepare struct
   
    DllCall("advapi32\EnumServicesStatusA" ;actual enumeration
        , "UInt", SCM_HANDLE
        , "UInt", ServiceType
        , "UInt", ServiceState
        , "UInt", &ENUM_SERVICE_STATUS
        , "UInt", bSize
        , "UIntP", 0
        , "UIntP", ServiceCount
        , "UIntP", 0)
   
    Loop, %ServiceCount%
        result .= DllCall("MulDiv"
                        , "Int", NumGet(ENUM_SERVICE_STATUS, (A_Index-1)*36+4)
                        , "Int", 1
                        , "Int", 1, "str") . delimiter

    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    Return result
   
}

Service_Start(ServiceName)
{
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x10)) ;SERVICE_START (0x0010)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\StartServiceA"
                        , "UInt", SC_HANDLE
                        , "Int", 0
                        , "Int", 0)

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_Stop(ServiceName)
{
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x20)) ;SERVICE_STOP (0x0020)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\ControlService"
                        , "UInt", SC_HANDLE
                        , "Int", 1
                        , "Str", "")

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_State(ServiceName)
{ ; Return Values
; SERVICE_STOPPED (1) : The service is not running.
; SERVICE_START_PENDING (2) : The service is starting.
; SERVICE_STOP_PENDING (3) : The service is stopping.
; SERVICE_RUNNING (4) : The service is running.
; SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
; SERVICE_PAUSE_PENDING (6) : The service pause is pending.
; SERVICE_PAUSED (7) : The service is paused.
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
                           
    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x4)) ;SERVICE_QUERY_STATUS (0x0004)
        result := -4 ;Service Not Found

    VarSetCapacity(SC_STATUS, 28, 0) ;SERVICE_STATUS Struct

    if !result
        result := !DllCall("advapi32\QueryServiceStatus"
                         , "UInt", SC_HANDLE
                         , "UInt", &SC_STATUS)
                         ? False : NumGet(SC_STATUS, 4) ;-1 or dwCurrentState

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_Add(ServiceName, BinaryPath, StartType=""){
    if !A_IsAdmin
        Return False

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0
                        , "Int", 0
                        , "UInt", 0x2) ;SC_MANAGER_CREATE_SERVICE (0x0002)
   
    StartType := !StartType ? 0x3 : 0x2
    ;SERVICE_DEMAND_START(0x00000003) vs SERVICE_AUTO_START(0x00000002)
   
    SC_HANDLE := DllCall("advapi32\CreateServiceA"
                   , "UInt", SCM_HANDLE
                   , "Str", ServiceName
                   , "Str", ServiceName
                   , "UInt", 0xF01FF ;SERVICE_ALL_ACCESS (0xF01FF)
                   , "UInt", 0x110 ;SERVICE_WIN32_OWN_PROCESS(0x00000010) | SERVICE_INTERACTIVE_PROCESS(0x00000100)
    ;interactable service with desktop (requires local account)
    ;http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
                   , "UInt", StartType
                   , "UInt", 0x1 ;SERVICE_ERROR_NORMAL(0x00000001)
                   , "Str", BinaryPath
                   , "Str", "" ;No Group
                   , "UInt", 0 ;No TagId
                   , "Str", "" ;No Dependencies
                   , "Int", 0 ;Use LocalSystem Account
                   , "Str", "")
    result := A_LastError ? SC_HANDLE "," A_LastError : 1
    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    Return result
}

Service_Delete(ServiceName)
{
    if !A_IsAdmin ;Requires Administrator rights
        Return False
    ServiceName := _GetName_(ServiceName)   

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0xF01FF)) ;SERVICE_ALL_ACCESS (0xF01FF)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\DeleteService", "Uint", SC_HANDLE)

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    Return result   
}

_GetName_(DisplayName)
{ ;Internal, Gets Service Name from Display Name,
    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA", "Int", 0, "Int", 0, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    DllCall("advapi32\GetServiceKeyNameA" ;Get Buffer Size
            , "Uint", SCM_HANDLE
            , "Str", DisplayName
            , "Int", 0
            , "UintP", Len)

    VarSetCapacity(Buffer, Len) ;Prepare Buffer

    DllCall("advapi32\GetServiceKeyNameA" ;Get Actual Service Name
        , "Uint", SCM_HANDLE
        , "Str", DisplayName
        , "Uint", &Buffer
        , "UintP", Len)

    Loop, % Len//2   
        Output .= Chr(NumGet(Buffer, A_Index-1, "Char"))

    return !Output ? DisplayName : Output
}


Downloader code:
Code:

#NoEnv SendMode Input 
SetWorkingDir %A_ScriptDir% 
DetectHiddenText, On
DetectHiddenWindows, On

if %0% = 0
{
Msgbox, invalid parameter
exitapp
return
}


UrlDownloadToFile, %1%, %2%
exitapp
Back to top
View user's profile Send private message Visit poster's website
Sivvy



Joined: 21 Jul 2008
Posts: 726
Location: Calgary, AB, Canada

PostPosted: Fri Jan 09, 2009 3:38 pm    Post subject: Reply with quote

Though your code seems clean and easy to read, there was too much for me to read through... I think you might want to look up "OnMessage()" and "Critical" though, since it will allow you to pass data between scripts and designate currently running code as Uninterruptable.
Back to top
View user's profile Send private message
Aero98



Joined: 08 Jan 2009
Posts: 81
Location: Oklahoma City

PostPosted: Fri Jan 09, 2009 3:59 pm    Post subject: Reply with quote

Thank you, I'll look into that today and post anything I find that fixes the issue.
Back to top
View user's profile Send private message Visit poster's website
Aero98



Joined: 08 Jan 2009
Posts: 81
Location: Oklahoma City

PostPosted: Fri Jan 09, 2009 8:44 pm    Post subject: Still not working Reply with quote

Ok I HAVE GOT to be missing something simple, I have tried Critical, Onmessage(), well I have no idea how to use it yet, sorry, I have even tried checing for the process ID twice after a short pause and initiating a loop incause the pid was temporarily undetectable and still nothing.

Can some one help, the part of the code I think having the problem I have Highlighted below

Code:

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, Slow ;Allows the detection of more information in a window


Gui, Add, Text, x26 y160 w350 h20 VStatus,
Gui, Add, Button, x26 y115 w110 h30 , Install SQL
Gui, Add, Text, x26 y10 w470 h100 , This utility will automatically download and install the correct version of Microsoft SQL Server 2005 Express for the alamode instance`n`nOnce the install is complete this utility will stop the service and install Blank databases`n`nIf you need to install a different set of database please wait for this utility to finish and replace the current databases with ones of your choosing.
Gui, Color, White
Gui, Show, h203 w545, a la mode's Microsoft SQL server express installer
GuiControl,, Status, Ready . . .
Return

GuiClose:
ExitApp


ButtonInstallSQL:
GuiControl,, Status, Warning . . .
msgbox, Starting the installation process`n`nOnce begun the process cannot be interupted`n`nPlease do not use the pc once the process has begun
GuiControl,, Status, Detecting if the OS is 64 or 32 bit
ThisProcess := DllCall("GetCurrentProcess")
; If IsWow64Process() fails or can not be found,
; assume this process is not running under wow64.
; Otherwise, use the value returned in IsWow64Process.
if !DllCall("IsWow64Process", "uint", ThisProcess, "int*", IsWow64Process)
    IsWow64Process := false
gosub % IsWow64Process ? "win64" : "win32"

return

;
;; ;;    I think it is in this section here   
;;

win32:
Gui +OwnDialogs +Disabled

Step1:      ; Downloads SP1, starts SP1 and then starts download of SP2


msgbox, Your running a 32 bit system`n`nInstalling SQL for 32 Bit systems
GuiControl,, Status, Downloading Microsoft SQL server Service Pack 1
download(55128,"http://download.microsoft.com/download/1/d/8/1d8461a7-4434-4d42-a7e1-246f3f9f7c3b/SQLEXPR.EXE","SQLEXPR.exe", "Service Pack 1")
GuiControl,, Status, Installing Microsoft SQL server Service Pack 1
Run, SQLexpr.exe /qb ADDLOCAL=ALL INSTANCENAME=ALAMODE SECURITYMODE=SQL SAPWD=alamode DISABLENETWORKPROTOCOLS=0,,,OPID1
msgbox, SQL SP PID = %OPID1%
sleep, 500
WinWait, ahk_pid %OPID1%,,120,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 1 failed to download and launch in a timely fasion. `n`nPlease download and install SQL Manually.
    return
}

sleep, 1000
download(55128,"http://download.microsoft.com/download/e/a/4/ea4b699b-bec4-4722-96d3-254580ed7f9e/SQLEXPR.EXE","SQLEXPR-SP2.exe", "Service Pack 2")

Step1b:
sleep, 100
WinWaitClose, ahk_pid %OPID1%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 1 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}
else
sleep, 1000
WinWaitClose, ahk_pid %OPID1%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 1 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}
else
{
msgbox, did %OPID1% close?
goto Step2
}
gosub Step1b
return

;IfWinExist, ahk_pid %OPID1%

Step2:    ;starts install of SQL '05 and download of msde support


GuiControl,, Status, Installing Microsoft SQL server Service Pack 2
sleep, 1000
Run, SQLexpr-sp2.exe /qb INSTANCENAME=ALAMODE UPGRADE=SQL_Engine SAPWD=alamode,,,OPID2
WinWait, ahk_pid %OPID2%,,120,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 2 failed to download and launch in a timely fasion. `n`nPlease download and install SQL Manually.
    return
}

sleep, 1000

download(11218,"http://download.microsoft.com/download/4/4/D/44DBDE61-B385-4FC2-A67D-48053B8F9FAD/SQLServer2005_BC.msi","SQLServer2005_BC.msi", "MSDE Support")

Step2b:
sleep, 100
WinWaitClose, ahk_pid %OPID2%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 2 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}
else
sleep, 1000
WinWaitClose, ahk_pid %OPID2%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 2 failed to install in a timely fasion. `n`nPlease install SQL Manually.
    return
}
else
{
Msgbox, did %OPID2% close?
gosub step3
}
gosub Step2b
Return





Step3:   ;Starts install of MSDE support

GuiControl,, Status, Installing Microsoft SQL server MSDE Support
sleep, 1000
Run, SQLServer2005_BC.msi /passive,,,OPID
WinWait, ahk_pid %OPID%,,120,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server Service Pack 2 failed to download and launch in a timely fasion. `n`nPlease download and install SQL Manually.
    return
}

sleep, 1000

Step3b:
sleep, 100
WinWaitClose, ahk_pid %OPID1%,,700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server MSDE Support failed to install in a timely fasion. `n`nPlease install this Manually.
    return
}
else
sleep, 1000
WinWaitClose, ahk_pid %OPID1%,,700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server MSDE Support failed to install in a timely fasion. `n`nPlease install this Manually.
    return
}
else
{
Msgbox, did %OPID% close?
gosub step4
}
gosub Step3b
Return




Step4:   ;Finalizes setting



BW:
WinWaitClose, ahk_pid %OPID%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server MSDE Support failed to install in a timely fasion. `n`nPlease install the MSDE Support Manually.
    return
}
else

sleep, 1000
WinWaitClose, ahk_pid %OPID%,,2700,,
if ErrorLevel
{
    MsgBox, The installation of Microsoft SQL server MSDE Support failed to install in a timely fasion. `n`nPlease install the MSDE Support Manually.
    return
}

IfWinExist, ahk_pid %OPID%
{
gosub BW
Return
}



Service_Stop(MSSQL$ALAMODE)
sleep, 1000
runwait, regedit /s fix.reg
filecopy, %A_WorkingDir%\wintotal.*, %A_ProgramFiles%\Microsoft SQL Server\MSSQL.1\MSSQL\Data
sleep, 1000
Service_Start(MSSQL$ALAMODE)
sleep, 2000
GuiControl,, Status, Verifying WinTOTAL's Databases
Runwait, %wt%\utils\wtverifydb.exe /attachdb
GuiControl,, Status, Updating WinTOTAL's Databases
Runwait, %wt%\utils\wtverifydb.exe /ver=46
GuiControl,, Status, Done . . .



Msgbox, The installation of Microsoft SQL server Completed Successfully
Return

;;; somewhere to here



win64:

MSGbox, you are running a 64 bit system
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                 ;;
;; All Functions are listed below  ;;
;;                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Download(w,x,y,z)   ;;v = step #, w = filesize, x = url, y = save as name, z = Download window text
{
Progress, b1 w500 CWFFFFFF Range0-%w%, Downloading Microsoft SQL Server 2005 %z%,
run, SQLdownloader.exe %x% %y%

sleep, 1000

SQLEXPR:
FolderSizeMB = 0
Loop, %y%
    Progress, %A_LoopFileSizeKB% 
     


Loop, %y%
FolderSizeMB += %A_LoopFileSizeKB%

IfGreater, FolderSizeMB, %w%, goto exit

sleep, 50
gosub SQLEXPR
return

EXIT:
sleep, 100
Progress, Off
return
}


/* Windows Service Control Functions
-heresy

- Return Values
     1 : Success
     0 : Failure
    -4 : Service Not Found

- State codes from Service_State()
    SERVICE_STOPPED (1) : The service is not running.
    SERVICE_START_PENDING (2) : The service is starting.
    SERVICE_STOP_PENDING (3) : The service is stopping.
    SERVICE_RUNNING (4) : The service is running.
    SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
    SERVICE_PAUSE_PENDING (6) : The service pause is pending.
    SERVICE_PAUSED (7) : The service is paused.
*/

MsgBox % Service_State("Alerter")


Service_List(State="", Type="", delimiter="`n"){
    if !State
        ServiceState := 0x3 ;SERVICE_STATE_ALL (0x00000003)
    else if (State="Active")
        ServiceState := 0x1 ;SERVICE_ACTIVE (0x00000001)
    else if (State="Inactive")
        ServiceState := 0x2 ;SERVICE_INACTIVE (0x00000002)
    else
        ServiceState := 0x3
   
    if !Type
        ServiceType := 0x30 ;SERVICE_WIN32 (0x00000030)
    else if (Type="Driver")
        ServiceType := 0xB ;SERVICE_DRIVER (0x0000000B)
    else if (Type="All")
        ServiceType := 0x3B ;sum of both
    else
        ServiceType := 0x30
       
    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0
                        , "Int", 0
                        , "UInt", 0x4) ;SC_MANAGER_ENUMERATE_SERVICE (0x0004)   

    DllCall("advapi32\EnumServicesStatusA"
        , "UInt", SCM_HANDLE
        , "UInt", ServiceType
        , "UInt", ServiceState
        , "UInt", 0
        , "UInt", 0
        , "UIntP", bSize ;get required buffer size first
        , "UIntP", 0
        , "UIntP", 0)
   
    VarSetCapacity(ENUM_SERVICE_STATUS, bSize, 0) ;prepare struct
   
    DllCall("advapi32\EnumServicesStatusA" ;actual enumeration
        , "UInt", SCM_HANDLE
        , "UInt", ServiceType
        , "UInt", ServiceState
        , "UInt", &ENUM_SERVICE_STATUS
        , "UInt", bSize
        , "UIntP", 0
        , "UIntP", ServiceCount
        , "UIntP", 0)
   
    Loop, %ServiceCount%
        result .= DllCall("MulDiv"
                        , "Int", NumGet(ENUM_SERVICE_STATUS, (A_Index-1)*36+4)
                        , "Int", 1
                        , "Int", 1, "str") . delimiter

    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    Return result
   
}

Service_Start(ServiceName)
{
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x10)) ;SERVICE_START (0x0010)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\StartServiceA"
                        , "UInt", SC_HANDLE
                        , "Int", 0
                        , "Int", 0)

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_Stop(ServiceName)
{
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x20)) ;SERVICE_STOP (0x0020)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\ControlService"
                        , "UInt", SC_HANDLE
                        , "Int", 1
                        , "Str", "")

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_State(ServiceName)
{ ; Return Values
; SERVICE_STOPPED (1) : The service is not running.
; SERVICE_START_PENDING (2) : The service is starting.
; SERVICE_STOP_PENDING (3) : The service is stopping.
; SERVICE_RUNNING (4) : The service is running.
; SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
; SERVICE_PAUSE_PENDING (6) : The service pause is pending.
; SERVICE_PAUSED (7) : The service is paused.
    ServiceName := _GetName_(ServiceName)

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
                           
    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0x4)) ;SERVICE_QUERY_STATUS (0x0004)
        result := -4 ;Service Not Found

    VarSetCapacity(SC_STATUS, 28, 0) ;SERVICE_STATUS Struct

    if !result
        result := !DllCall("advapi32\QueryServiceStatus"
                         , "UInt", SC_HANDLE
                         , "UInt", &SC_STATUS)
                         ? False : NumGet(SC_STATUS, 4) ;-1 or dwCurrentState

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    return result
}

Service_Add(ServiceName, BinaryPath, StartType=""){
    if !A_IsAdmin
        Return False

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0
                        , "Int", 0
                        , "UInt", 0x2) ;SC_MANAGER_CREATE_SERVICE (0x0002)
   
    StartType := !StartType ? 0x3 : 0x2
    ;SERVICE_DEMAND_START(0x00000003) vs SERVICE_AUTO_START(0x00000002)
   
    SC_HANDLE := DllCall("advapi32\CreateServiceA"
                   , "UInt", SCM_HANDLE
                   , "Str", ServiceName
                   , "Str", ServiceName
                   , "UInt", 0xF01FF ;SERVICE_ALL_ACCESS (0xF01FF)
                   , "UInt", 0x110 ;SERVICE_WIN32_OWN_PROCESS(0x00000010) | SERVICE_INTERACTIVE_PROCESS(0x00000100)
    ;interactable service with desktop (requires local account)
    ;http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
                   , "UInt", StartType
                   , "UInt", 0x1 ;SERVICE_ERROR_NORMAL(0x00000001)
                   , "Str", BinaryPath
                   , "Str", "" ;No Group
                   , "UInt", 0 ;No TagId
                   , "Str", "" ;No Dependencies
                   , "Int", 0 ;Use LocalSystem Account
                   , "Str", "")
    result := A_LastError ? SC_HANDLE "," A_LastError : 1
    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
    Return result
}

Service_Delete(ServiceName)
{
    if !A_IsAdmin ;Requires Administrator rights
        Return False
    ServiceName := _GetName_(ServiceName)   

    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
                        , "Int", 0 ;NULL for local
                        , "Int", 0
                        , "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)

    if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
                            , "UInt", SCM_HANDLE
                            , "Str", ServiceName
                            , "UInt", 0xF01FF)) ;SERVICE_ALL_ACCESS (0xF01FF)
        result := -4 ;Service Not Found

    if !result
        result := DllCall("advapi32\DeleteService", "Uint", SC_HANDLE)

    DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
    Return result   
}

_GetName_(DisplayName)
{ ;Internal, Gets Service Name from Display Name,
    SCM_HANDLE := DllCall("advapi32\OpenSCManagerA", "Int", 0, "Int", 0, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)   

    DllCall("advapi32\GetServiceKeyNameA" ;Get Buffer Size
            , "Uint", SCM_HANDLE
            , "Str", DisplayName
            , "Int", 0
            , "UintP", Len)

    VarSetCapacity(Buffer, Len) ;Prepare Buffer

    DllCall("advapi32\GetServiceKeyNameA" ;Get Actual Service Name
        , "Uint", SCM_HANDLE
        , "Str", DisplayName
        , "Uint", &Buffer
        , "UintP", Len)

    Loop, % Len//2   
        Output .= Chr(NumGet(Buffer, A_Index-1, "Char"))

    return !Output ? DisplayName : Output
}


~^m::
Exitapp
return
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group