利用Magic Packet实现的远程开机代码
调用示例:
MagicPacket “192.168.1.100:54321” "A1-B2-C3-D4-E5-F6"
Private Const WSA_DescriptionLen = 256 Private Const WSA_DescriptionSize = WSA_DescriptionLen + 1 Private Const WSA_SYS_STATUS_LEN = 128 Private Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN + 1 Private Const INVALID_SOCKET = -1 Private Const SOCKET_ERROR = -1 Private Const AF_INET = 2 Private Const SOCK_DGRAM = 2 Private Const IPPROTO_UDP = 17 Private Type HostEnt hName As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type Private Type SockAddr Sin_Family As Integer Sin_Port As Integer Sin_Addr As Long Sin_Zero(7) As Byte End Type Private Type WSADataType wVersion As Integer wHighVersion As Integer szDescription As String * WSA_DescriptionSize szSystemStatus As String * WSA_SysStatusSize iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As Long End Type Private Declare Function CloseSocket Lib "ws2_32.dll" Alias "closesocket" (ByVal hSocket As Long) As Long Private Declare Function Conn Lib "ws2_32.dll" Alias "connect" (ByVal hSocket As Long,Addr As SockAddr,ByVal NameLen As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any,Source As Any,ByVal Length As Long) Private Declare Function GetHostByName Lib "ws2_32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long Private Declare Function Htons Lib "ws2_32.dll" Alias "htons" (ByVal HostShort As Integer) As Integer Private Declare Function iNet_Addr Lib "wsock32.dll" Alias "inet_addr" (ByVal S As String) As Long Private Declare Function Send Lib "ws2_32.dll" Alias "send" (ByVal hSocket As Long,Buf As Any,ByVal BufLen As Long,ByVal Flags As Long) As Long Private Declare Function Socket Lib "ws2_32.dll" Alias "socket" (ByVal af As Long,ByVal sType As Long,ByVal Protocol As Long) As Long Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVR As Long,lpWSAD As WSADataType) As Long Public Sub MagicPacket(ByVal Host As String,ByVal MAC As String) Dim WSAData As WSADataType,SA As SockAddr,hHost As HostEnt Dim hSocket As Long,Port As Long,R As Long,I As Integer,Data() As Byte If WSAStartup(&H202,WSAData) <> 0 Then WSACleanup Exit Sub End If Port = 9 '当不输入端口号时,默认端口号为9 If (InStr(Host,":") > 0) Then If IsNumeric(Right(Host,Len(Host) - InStr(Host,":"))) = True Then Port = CLng(Right(Host,":"))) Host = Left(Host,InStr(Host,":") - 1) End If hSocket = Socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) SA.Sin_Family = AF_INET SA.Sin_Port = Htons(CInt("&H" & Hex(Port))) SA.Sin_Addr = iNet_Addr(Host) If SA.Sin_Addr = INVALID_SOCKET Then R = GetHostByName(Host) If R <> 0 Then CopyMemory hHost,ByVal R,LenB(hHost) CopyMemory R,ByVal hHost.hAddrList,LenB(R) CopyMemory SA.Sin_Addr,hHost.hLength End If End If If Conn(hSocket,SA,LenB(SA)) = SOCKET_ERROR Then WSACleanup Exit Sub End If MAC = Replace(MAC,"-","") MAC = Replace(MAC,":","") ReDim Data(101) For I = 0 To 5 Data(I) = &HFF Next For I = 0 To 15 Data((I + 1) * 6) = CByte("&H" & Mid(MAC,1,2)) Data((I + 1) * 6 + 1) = CByte("&H" & Mid(MAC,3,2)) Data((I + 1) * 6 + 2) = CByte("&H" & Mid(MAC,5,2)) Data((I + 1) * 6 + 3) = CByte("&H" & Mid(MAC,7,2)) Data((I + 1) * 6 + 4) = CByte("&H" & Mid(MAC,9,2)) Data((I + 1) * 6 + 5) = CByte("&H" & Mid(MAC,11,2)) Next Send hSocket,Data(0),UBound(Data) + 1,0 CloseSocket hSocket End Sub