Newsgroup: it.comp.lang.visual-basic
Titolo: stop servizio msssqlserver

Cari colleghi,
devo stoppare il servizio di MS Sql Server su Nt/2x/Xp prima di avviare
l'installazione della mia applicazione Visual Basic che fa uso della
libreria SQLDmo.
l'unica cosa che sono riuscito a fare da prompt di dos è quello di eseguire
la seguente riga di comando :
"net stop mssqlserver"
il mio problema è che la finestra dos mi rimane aperta, mentre vorrei che
dopo l'esecuzione automaticamente si chiudesse per poi proseguire con tutta
tranquillità con l'installazione.
sapete se esiste una maniera silent di eseguire la precedente riga di
comando ?
oppure sapete come potrei chiudere il servizio mssqlserver in nt e/o 98 non
manualmente?
grazie

 tranquillità con l'installazione.
salve,
se dici  che SqlServer e' gia' installato, puoi comunque gia' usare DMO...
ti connetti al server richiedendo User e Pwd, oppure con una connessione
trusted  e successivamente esegui
objSqlServer.ShutDown (true)
in modo da terminare con grazia il servizio....
oppure, come tu gia' fai, chiami il comando NET passando per shellexecute
attendendo il termine del processo, qualche cosa tipo
    Private Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessID As Long
        dwThreadID As Long
    End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
      hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
      lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
      ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
      ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
      lpStartupInfo As STARTUPINFO, lpProcessInformation As _
      PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const SW_HIDE = 0
Private Const STARTF_USESHOWWINDOW = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Function ExecCmd(ByVal cmdline As String) As Long
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO      ' Initialize the STARTUPINFO structure:
    Dim lRet As Long
    start.cb = Len(start)      ' Start the shelled application:
    start.dwFlags = STARTF_USESHOWWINDOW
    start.wShowWindow = SW_HIDE
    lRet = CreateProcessA(0&, cmdline, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    lRet = WaitForSingleObject(proc.hProcess, INFINITE)
    Call GetExitCodeProcess(proc.hProcess, lRet)
    Call CloseHandle(proc.hThread)
    Call CloseHandle(proc.hProcess)
    ExecCmd = lRet
End Function
cosi' dovresti anche avere l'exit code del processo da poter testare....
avevo provato questo approccio per richiamare oSql.exe per eseguire la
creazione di database, ma l'ho abbandonato in quanto oSql..exe non ritorna
codice di errore nell'exit code su piattaforme Win9x
ti ricordo che i comandi NET non sono disponibili su Win9x....
saluti
Andrea Montanari
montanar...@virgilio.it
http://www.asql.biz/DbaMgr.shtm
DbaMgr2k ver 0.2.0  -  DbaMgr ver 0.48.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
--------- remove DMO to reply

 
Usa l'API ControlService
'apri il database dei servizi
hSCManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT)
'accedi al servizio con diritto di stoppare
hService = OpenService(hSCManager, "nome servizio", SERVICE_STOP)
'mandi al servizio il messaggio FERMATI
ControlService(hService, SERVICE_CONTROL_STOP, ServStatus)
'chiudi ciò che hai aperto
CloseServiceHandle(hService)
CloseServiceHandle(hSCManager)
-- 
Samuele Battarra
prova il mio newsreader:
http://digilander.iol.it/battarsa/programmi/news/


ti ringrazio ma non succede nulla ti allego il codice vb ....
Public Type SERVICE_STATUS
        dwServiceType As Long
        dwCurrentState As Long
        dwControlsAccepted As Long
        dwWin32ExitCode As Long
        dwServiceSpecificExitCode As Long
        dwCheckPoint As Long
        dwWaitHint As Long
End Type
Public Const SERVICE_STOP = &H20
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SC_MANAGER_CONNECT = &H1
Public Declare Function OpenSCManager Lib "advapi32.dll" Alias _
                        "OpenSCManagerA" (ByVal lpMachineName As String, _
                                            ByVal lpDatabaseName As String,
_
                                            ByVal dwDesiredAccess As Long)
As Long
Public Declare Function OpenService Lib "advapi32.dll" Alias _
                        "OpenServiceA" (ByVal hSCManager As Long, _
                        ByVal lpServiceName As String, _
                        ByVal dwDesiredAccess As Long) As Long
Public Declare Function ControlService Lib "advapi32.dll" (ByVal hService As
Long, _
                                                            ByVal dwControl
As Long, _
                                                            lpServiceStatus
As SERVICE_STATUS) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal
hSCObject As Long) As Long
Dim hSCManager As Long
Dim hService As Long
Dim hControl As Long
Dim hclose As Long
Dim ServStatus As SERVICE_STATUS
'apri il database dei servizi
hSCManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT)
'accedi al servizio con diritto di stoppare
hService = OpenService(hSCManager, "mssqlserver", SERVICE_STOP)
'mandi al servizio il messaggio FERMATI
hControl = ControlService(hService, SERVICE_CONTROL_STOP, ServStatus)
'chiudi ciò che hai aperto
hclose = CloseServiceHandle(hService)
hclose = CloseServiceHandle(hSCManager)

 
[cut]
prova con questo codice, a me funziona
naturalmente il prog lo devi eseguire come amministratore
Option Explicit
Private Type SERVICE_STATUS
   dwServiceType As Long
   dwCurrentState As Long
   dwControlsAccepted As Long
   dwWin32ExitCode As Long
   dwServiceSpecificExitCode As Long
   dwCheckPoint As Long
   dwWaitHint As Long
End Type
Private Const SERVICE_STOP = &H20
Private Const SERVICE_CONTROL_STOP = &H1
Private Const SC_MANAGER_CONNECT = &H1
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _
   (ByVal lpMachineName As String, _
    ByVal lpDatabaseName As String, _
    ByVal dwDesiredAccess As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
   (ByVal hSCManager As Long, _
    ByVal lpServiceName As String, _
    ByVal dwDesiredAccess As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" _
   (ByVal hService As Long, _
    ByVal dwControl As Long, _
    lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" _
   (ByVal hSCObject As Long) As Long
 
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
   (ByVal dwFlags As Long, _
    lpSource As Any, _
    ByVal dwMessageId As Long, _
    ByVal dwLanguageId As Long, _
    ByVal lpBuffer As String, _
    ByVal nSize As Long, _
    Arguments As Long) As Long
Private Sub ErrorMessage()
   Dim ErrCode As Long
   Dim NumCar As Long
   Dim Text As String
 
   ErrCode = GetLastError
   Text = Space$(1000)
   NumCar = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, ErrCode, 0, Text, Len(Text), 0)
   MsgBox Text, vbCritical
End Sub
Public Sub StopService(ByVal ServiceName As String)
   Dim hSCManager As Long
   Dim hService As Long
   Dim hControl As Long
   Dim hClose As Long
   Dim ServStatus As SERVICE_STATUS
   'apri il database dei servizi
   hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_CONNECT)
   If hSCManager <> 0 Then
      'accedi al servizio con diritto di stoppare
      hService = OpenService(hSCManager, ServiceName, SERVICE_STOP)
      If hService <> 0 Then
         'mandi al servizio il messaggio FERMATI
         hControl = ControlService(hService, SERVICE_CONTROL_STOP, ServStatus)
         If hControl <> 0 Then
            'chiudi ciň che hai aperto
            hClose = CloseServiceHandle(hService)
         Else
            ErrorMessage
         End If
      Else
         ErrorMessage
      End If
      hClose = CloseServiceHandle(hSCManager)
   Else
      ErrorMessage
   End If
End Sub
Sub Main()
   StopService "MSSQLServer"
End Sub
