Public Function ReadINI(ByVal Section As String, ByVal Key As String, ByVal inipath As String) As String
Dim RetStr As String
If Dir(inipath) = "" Then Exit Function
RetStr = String(255, Chr(0))
ReadINI = Left(RetStr, GetPrivateProfileString(Section, ByVal Key, "", RetStr, Len(RetStr), inipath))
End Function
'' ==========================================================
' 开发人员:dengxue.com
' 编写时间:2004-3-28
' 函数名称:WriteINI(ByVal Section As String, ByVal Key As String, ByVal KeyValue As String, ByVal inipath As String) As Integer
' 参数说明:Section 段名
' Key 键名
' KeyValue 键值
' inipath INI文件路径
' 功能说明:写INI文件
' Function returns 1 if successful and 0 if unsuccessful
'
'' ==========================================================
Public Function WriteINI(ByVal Section As String, ByVal Key As String, ByVal KeyValue As String, ByVal inipath As String) As Integer
If Dir(inipath) = "" Then Exit Function
WritePrivateProfileString Section, Key, KeyValue, inipath
WriteINI = 1
End Function
Private Type SERVICE_TABLE_ENTRY
lpServiceName As String
lpServiceProc As Long
lpServiceNameNull As Long
lpServiceProcNull As Long
End Type
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 Type QUERY_SERVICE_CONFIG
dwServiceType As Long
dwStartType As Long
dwErrorControl As Long
lpBinaryPathName As Long 'String
lpLoadOrderGroup As Long ' String
dwTagId As Long
lpDependencies As Long 'String
lpServiceStartName As Long 'String
lpDisplayName As Long 'String
End Type
Private Declare Function QueryServiceConfig Lib "advapi32.dll" Alias "QueryServiceConfigA" (ByVal hService As Long, lpServiceConfig As Byte, ByVal cbBufSize As Long, pcbBytesNeeded As Long) As Long
Private Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Public Function CheckServiceRunning(ByVal serviceName As String, Optional ByRef serviceRunning As e_ServiceState, Optional ByRef serviceStartType As e_ServiceType, Optional servicePath As String) As Boolean
Dim hSCM As Long
Dim hSVC As Long
Dim pSTATUS As SERVICE_STATUS
Dim udtConfig As QUERY_SERVICE_CONFIG
Dim lRet As Long
Dim lBytesNeeded As Long
Dim sTemp As String
Dim pFileName As Long
CheckServiceRunning = True
' Open The Service Control Manager
'
hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_CONNECT)
If hSCM = 0 Then
CheckServiceRunning = False
End If
' Open the specific Service to obtain a handle
'
hSVC = OpenService(hSCM, Trim(serviceName), GENERIC_READ)
If hSVC = 0 Then
CheckServiceRunning = False
'MsgBox "Error - " & Err.LastDllError
GoTo CloseHandles
End If
' Fill the Service Status Structure
'
lRet = QueryServiceStatus(hSVC, pSTATUS)
If lRet = 0 Then
CheckServiceRunning = False
GoTo CloseHandles
End If
' Report the Current State
'
Select Case pSTATUS.dwCurrentState
Case SERVICE_STOP
serviceRunning = e_ServiceState_Stopped
Case SERVICE_START
serviceRunning = e_ServiceState_StartPending
Case SERVICE_STOP_PENDING
serviceRunning = e_ServiceState_StopPending
Case SERVICE_RUNNING
serviceRunning = e_ServiceState_Running
Case SERVICE_CONTINUE_PENDING
serviceRunning = e_ServiceState_ContinuePending
Case SERVICE_PAUSE_PENDING
serviceRunning = e_ServiceState_PausePending
Case SERVICE_PAUSED
serviceRunning = e_ServiceState_Paused
Case SERVICE_ACCEPT_STOP
serviceRunning = e_ServiceState_Stopped
Case SERVICE_ACCEPT_PAUSE_CONTINUE
serviceRunning = e_ServiceState_Paused
Case SERVICE_ACCEPT_SHUTDOWN
serviceRunning = e_ServiceState_StopPending
End Select
' Call QueryServiceConfig with 1 byte buffer to generate an error
' that returns the size of a buffer we need.
'
ReDim abConfig(0) As Byte
lRet = QueryServiceConfig(hSVC, abConfig(0), 0&, lBytesNeeded)
If lRet = 0 And Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER Then
CheckServiceRunning = False
End If
' Redim our byte array to the size necessary and call
' QueryServiceConfig again
'
ReDim abConfig(lBytesNeeded) As Byte
lRet = QueryServiceConfig(hSVC, abConfig(0), lBytesNeeded, _
lBytesNeeded)
If lRet = 0 Then
CheckServiceRunning = False
GoTo CloseHandles
End If
' Fill our Service Config User Defined Type.
'
CopyMemory udtConfig, abConfig(0), Len(udtConfig)
serviceStartType = udtConfig.dwStartType
sTemp = Space(255)
' Now use the pointer obtained to copy the path into the temporary
' String Variable
'
lRet = lstrcpy(sTemp, udtConfig.lpBinaryPathName)
servicePath = Trim(sTemp)
CloseHandles:
' Close the Handle to the Service
'
CloseServiceHandle (hSVC)
' Close the Handle to the Service Control Manager
'
CloseServiceHandle (hSCM)
End Function
'重启Serv-U服务,使改动生效
Public Sub ReStartServer()
Dim serviceRunning As e_ServiceState
Shell "net stop serv-u", vbHide
Do
If CheckServiceRunning("serv-u", serviceRunning) = True Then
If serviceRunning = e_ServiceState_Stopped Then
Shell "net start serv-u", vbHide
Exit Do
End If
End If
DoEvents
Loop
'Ru = RunService("serv-u")
End Sub
Private Const SERVICE_DISABLED As Long = &H4
Private Const SERVICE_DEMAND_START As Long = &H3
Private Const SERVICE_AUTO_START As Long = &H2
Private Const SERVICE_SYSTEM_START As Long = &H1
Private Const SERVICE_BOOT_START As Long = &H0