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
Private Const scUserAgent = "vb wininet"
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Const INTERNET_INVALID_PORT_NUMBER = 0
Private Const FTP_TRANSFER_TYPE_BINARY = &H2
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const INTERNET_FLAG_PASSIVE = &H8000000
Private Const INTERNET_SERVICE_FTP = 1
Private Const INTERNET_SERVICE_GOPHER = 2
Private Const INTERNET_SERVICE_HTTP = 3
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpCommand Lib "wininet.dll" Alias "FtpCommandA" _
(ByVal hConnect As Long, ByVal fExpectResponse As Boolean, ByVal dwFlags As Long, _
ByVal lpszCommand As String, ByVal lContext As Long, phFtpCommand As Long) As Boolean
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private hOpen As Long
Private hConnection As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Private newPASSWORD As String
Private oldPASSWORD As String
Private userName As String
Private URLSTR As String
‘修改FTP密码主程序
Public Function getStr() As String
hOpen = 0
hConnection = 0
Dim nFlag As Long
Dim strCmd As String
nFlag = INTERNET_FLAG_PASSIVE
‘建立FTP修改密码命令字符串
strCmd = "SITE PSWD " & oldPASSWORD & Space(1) & newPASSWORD
‘建立internet连接
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
If hOpen = 0 Then getStr = "不能打开连接。..."
If hOpen <> 0 Then
‘与FTP服务器建立连接
hConnection = InternetConnect(hOpen, URLSTR, NTERNET_INVALID_PORT_NUMBER, _
userName, oldPASSWORD, INTERNET_SERVICE_FTP, nFlag, 0)
If hConnection <> 0 Then
bret = FtpCommand(hConnection, False, FTP_TRANSFER_TYPE_ASCII, strCmd, 0, hFile)
If bret Then
getStr = "密码修改成功!..."
Else
getStr = "密码修改失败!..."
End If
Else
getStr = "无法登录至FTP服务器,请检查帐户名或密码是否正确。"
End If
End If
‘断开internet连接
If hConnection <> 0 Then InternetCloseHandle (hConnection)
‘断开FTP服务器连接
If hOpen <> 0 Then InternetCloseHandle (hOpen)
End Function
‘传入FTP服务器的URL
Public Property Let URL(URL1 As String)
URLSTR = URL1
End Property
‘传入FTP原有密码
Public Property Let oldPSD(psd As String)
oldPASSWORD = psd
End Property
‘传入FTP新密码
Public Property Let newPSD(psd1 As String)
newPASSWORD = psd1
End Property
‘传入FTP帐户名
Public Property Let user(psd2 As String)
userName = psd2
End Property