Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bCheck As Boolean
Try
bCheck = My.Computer.Network.Ping(TextBox1.Text)
If bCheck Then
MessageBox.Show("Ping Check Success...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
MessageBox.Show("Ping Check Failed...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
bCheck = False
End Try
End Sub
End Class
* 구조체를 Marshal 이용 바이트배열로 변환 하거나 바이트 배열화 된 구조체를 다시 원래 모습으로
복귀 시키는 예제...
메인화면
전체 소스 코드
Form1.vb
Imports System.Runtime.InteropServices
Public Class Form1
Structure t_TEST
Public iTmp As Integer
Public strTmp As String
Public dbTmp As Double
Public ftTmp As Single
End Structure
Dim tt As t_TEST = New t_TEST
Dim bTmp() As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tt.dbTmp = 0.12321312313
tt.iTmp = 123123
tt.strTmp = "테스트1"
tt.ftTmp = 0.123213F
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'구조체를 바이트 배열로...
bTmp = StructToBytes(tt)
label1.Text = bTmp.Length.ToString() + " bytes"
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'바이트 배열을 다시 구조체로 변환...
Dim tTmp As t_TEST = ByteToStruct(Of t_TEST)(bTmp)
label2.Text = "int 값: " & tTmp.iTmp.ToString()
label3.Text = "double 값: " & tTmp.dbTmp.ToString()
label4.Text = "float 값: " & tTmp.ftTmp.ToString()
label5.Text = "string 값: " & tTmp.strTmp
End Sub
Private Function StructToBytes(ByVal obj As Object) As Byte()
'구조체 사이즈
Dim iSize As Integer = Marshal.SizeOf(obj)
'사이즈 만큼 메모리 할당
Dim arr(iSize) As Byte
Dim ptr As IntPtr = Marshal.AllocHGlobal(iSize)
'구조체 주소값 가져오기
Marshal.StructureToPtr(obj, ptr, False)
'메모리 복사
Marshal.Copy(ptr, arr, 0, iSize)
'메모리 할당 받은거 해제
Marshal.FreeHGlobal(ptr)
Return arr
End Function
Private Function ByteToStruct(Of T As Structure)(ByVal buffer As Byte()) As T
Dim size As Integer = Marshal.SizeOf(GetType(T))
If size > buffer.Length Then
Throw New Exception()
End If
Dim ptr As IntPtr = Marshal.AllocHGlobal(size)
Marshal.Copy(buffer, 0, ptr, size)
Dim obj As T = CType(Marshal.PtrToStructure(ptr, GetType(T)), T)
Marshal.FreeHGlobal(ptr)
Return obj
End Function
End Class
* VBNET API PC (종료, 재시작) 또는 Diagnostics.Process 이용 PC 종료 예제...
메인화면
전체 소스 코드
Form1.vb
Imports System.Runtime.InteropServices
Public Class Form1
Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Integer, _
ByVal bForceAppsClosed As Integer, _
ByVal bRebootAfterShutdown As Integer) As Integer
'1: 종료 전 사용자에게 알릴 메시지 , 2:종료 전 사용자에게 알릴 메시지, 3:종료까지 대기 시간, 4:프로그램 강제 종료 여부(False -> 강제 종료), 5:시스템 종료 후 다시 시작 여부(true -> 다시 시작)
Dim bClick As Boolean = False
Dim dtClick As DateTime
Dim bThread As Boolean = True
Dim thMain As System.Threading.Thread
Dim bCheck As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'크로스 스레드 오류 방지
CheckForIllegalCrossThreadCalls = False
thMain = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Thread_Tick))
'1. 첫번째 방법 : 스레드
thMain.IsBackground = True
thMain.Start()
'2 두번째 방법
'10초 뒤 종료
'System.Diagnostics.Process.Start("shutdown", "/s /f /t 10")
'label2.Text = "10"
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
dtClick = DateTime.Now
bClick = True
End Sub
Private Function After_Time(ByVal dtNow As DateTime, ByVal dtBefore As DateTime) As Double
Dim ts As TimeSpan = dtNow - dtBefore
Return ts.TotalSeconds
End Function
Private Sub Thread_Tick()
While (bThread)
If bClick Then
label2.Text = String.Format("{0:##0}", After_Time(DateTime.Now, dtClick))
'5초 뒤에 PC 재시작
'마지막인자 True 이면 재시작 False 이면 종료
If After_Time(Date.Now, dtClick) >= 5 And Not bCheck Then
InitiateSystemShutdown("\\127.0.0.1", Nothing, 0, False, True)
bCheck = True
End If
End If
System.Threading.Thread.Sleep(100)
End While
End Sub
Private Sub Form1_FontChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FontChanged
'스레드 변수가 nothing 이 아니면
If Not thMain Is Nothing Then
'스레드가 돌아가고 있으면.
If thMain.IsAlive Then
'스레드 강제 종료
thMain.Abort()
End If
End If
End Sub
End Class
* 두번째 방법으로는 아래와 같이 Diagnostics.Process 를 이용 PC 종료 예제 입니다.