2014年11月25日火曜日

Wake ON LAN

マジックパケットをVB.netで送るソフトを
http://vbdotnetmsde.sblo.jp/article/4109822.html
を参考にして作ってみた。


Button1を設定データ保存するときクリック
Timer1 StatusStrip1 を使う
項目名の最後に*をつけたものについては、ソフト起動時にmagicパケットを送るようにしています。
imaxを10として、10台のPCまで登録できるようにした。
imaxを増やせば、何台までも設定できる。


*****************
Public Class Form1
    Private testButtons As System.Windows.Forms.Button()
    Private macad As System.Windows.Forms.TextBox()
    Private komoku As System.Windows.Forms.TextBox()
    Dim scount As Integer
    Dim imax As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        imax = 10
        Dim i As Integer
        Me.testButtons = New System.Windows.Forms.Button(imax) {}
        Me.macad = New System.Windows.Forms.TextBox(imax) {}
        Me.komoku = New System.Windows.Forms.TextBox(imax) {}
        Dim txt, stxt() As String
        Try
            txt = My.Computer.FileSystem.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "config.dat")
        Catch ex As Exception
            txt = ""
        End Try
        For i = 0 To imax - 1
            Me.testButtons(i) = New System.Windows.Forms.Button
            'プロパティ設定
            Me.testButtons(i).Name = (i).ToString()
            Me.testButtons(i).Text = "起動" + (i).ToString()
            Me.testButtons(i).Size = New System.Drawing.Size(50, 25)
            Me.testButtons(i).Location = New Point(20, 40 + i * 30)
            'イベントハンドラに関連付け
            Me.Controls.Add(Me.testButtons(i))
            AddHandler Me.testButtons(i).Click, AddressOf Me.testButtons_Click
            '項目名
            Me.komoku(i) = New System.Windows.Forms.TextBox()
            Me.komoku(i).Name = "00" + (i).ToString()
            Me.komoku(i).Size = New System.Drawing.Size(200, 15)
            Me.komoku(i).Location = New Point(100, 40 + i * 30)
            'Me.komoku(i).Visible = True
            Me.Controls.Add(Me.komoku(i))
            'mac address()
            Me.macad(i) = New System.Windows.Forms.TextBox()
            Me.macad(i).Name = "0" + (i).ToString()
            Me.macad(i).Size = New System.Drawing.Size(110, 15)
            Me.macad(i).Location = New Point(320, 40 + i * 30)
            Me.macad(i).Visible = True
            Me.Controls.Add(Me.macad(i))
            If txt <> "" Then
                stxt = txt.Split(",")
                Me.komoku(i).Text = stxt(2 * i)
                Me.macad(i).Text = stxt(2 * i + 1)
            Else
                Me.komoku(i).Text = "None"
                Me.macad(i).Text = "None"
            End If
            Try
                macad(i).Text = My.Computer.FileSystem.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + Str(i).Trim + ".txt")
                komoku(i).Text = My.Computer.FileSystem.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + Str(i).Trim + ".txk")
            Catch ex As Exception
            End Try
        Next i
        For i = 0 To imax - 1
            If Microsoft.VisualBasic.Right(komoku(i).Text, 1) = "*" Then
                magic(i)
            End If
        Next
        Timer1.Enabled = True
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txt As String
        txt = ""
        For i = 0 To imax - 2
            txt = txt + komoku(i).Text.Trim + "," + macad(i).Text.Trim + ","
        Next
        txt = txt + komoku(imax - 1).Text.Trim + "," + macad(imax - 1).Text.Trim
        My.Computer.FileSystem.WriteAllText(System.AppDomain.CurrentDomain.BaseDirectory + "config.dat", txt, False)
    End Sub
    Private Sub testButtons_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim num As Integer
        num = Val(CType(sender, System.Windows.Forms.Button).Name)
        magic(num)
    End Sub
    Private Sub magic(ByVal num As Integer)
        Dim sMac As String
        Dim btPacket() As Byte = New Byte(17 * 6 - 1) {}
        Dim sMacArray(5) As String
        Dim btMac(5) As Byte
        Dim iCounter As Integer
        Dim iMacCounter As Integer
        Dim objeClient As New System.Net.Sockets.UdpClient()
        sMac = macad(num).Text
        sMacArray(0) = sMac.Substring(0, 2)
        sMacArray(1) = sMac.Substring(2, 2)
        sMacArray(2) = sMac.Substring(4, 2)
        sMacArray(3) = sMac.Substring(6, 2)
        sMacArray(4) = sMac.Substring(8, 2)
        sMacArray(5) = sMac.Substring(10, 2)
        For iCounter = 0 To 5
            btMac(iCounter) = Long.Parse(sMacArray(iCounter), Globalization.NumberStyles.HexNumber)
        Next
        For iCounter = 0 To 5
            btPacket(iCounter) = &HFF
        Next
        For iCounter = 1 To 16
            For iMacCounter = 0 To 5
                btPacket(iCounter * 6 + iMacCounter) = btMac(iMacCounter)
            Next
        Next
        objeClient.Connect(Net.IPAddress.Broadcast, 40000)
        objeClient.Send(btPacket, btPacket.Length)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        scount = scount + 1
        ToolStripStatusLabel1.Text = "終了までの秒数:" + (30 - scount).ToString + "sec"
        If scount >= 30 Then
            End
        End If
    End Sub
End Class

0 件のコメント:

コメントを投稿