Imports System.Runtime.InteropServices

Public Class VB_API_Demo
    'Source: http://www.caliper2pc.de

    Private p1 As IntPtr
    Private p2 As IntPtr
    Private p3 As IntPtr
    Private p4 As IntPtr
    Private p5 As IntPtr
    Private p6 As IntPtr
    Private p7 As IntPtr
    Private p8 As IntPtr
    Private p9 As IntPtr
    Private p10 As IntPtr
    Private p11 As IntPtr
    Private p12 As IntPtr
    Private p13 As IntPtr
    Private p14 As IntPtr

    'Funktionsdeklarationen
    'Die "Exchange.dll" (bzw. "Exchange64.dll") muss sich im gleichen Verzeinis befinden wie die *.exe Datei!!!

    'Function declaration
    'The "Exchange.dll" (or "Exchange64.dll") and the *.exe file must be in the same directory!!!

    '******************** 32 BIT ********************
    '"Exchange.dll" für 32 bit Systeme benutzen. 
    'Use "Exchange.dll" on 32 bit Systems.   

    Public Declare Function GetChannel1 Lib "Exchange.dll" Alias "GetChannel1" () As IntPtr
    Public Declare Function GetChannel2 Lib "Exchange.dll" Alias "GetChannel2" () As IntPtr
    Public Declare Function GetChannel3 Lib "Exchange.dll" Alias "GetChannel3" () As IntPtr
    Public Declare Function GetChannel4 Lib "Exchange.dll" Alias "GetChannel4" () As IntPtr
    Public Declare Function GetChannel5 Lib "Exchange.dll" Alias "GetChannel5" () As IntPtr
    Public Declare Function GetChannel6 Lib "Exchange.dll" Alias "GetChannel6" () As IntPtr
    Public Declare Function GetSensor1 Lib "Exchange.dll" Alias "GetSensor1" () As IntPtr
    Public Declare Function GetSensor2 Lib "Exchange.dll" Alias "GetSensor2" () As IntPtr
    Public Declare Function GetPort1 Lib "Exchange.dll" Alias "GetPort1" () As Byte
    Public Declare Function GetPort2 Lib "Exchange.dll" Alias "GetPort2" () As Byte
    Public Declare Function GetPort3 Lib "Exchange.dll" Alias "GetPort3" () As Byte
    Public Declare Function GetPort4 Lib "Exchange.dll" Alias "GetPort4" () As Byte
    Public Declare Function GetPort5 Lib "Exchange.dll" Alias "GetPort5" () As Byte
    Public Declare Function GetPort6 Lib "Exchange.dll" Alias "GetPort6" () As Byte
    Public Declare Function SetPort1 Lib "Exchange.dll" Alias "SetPort1" (ByVal status As Byte) As Byte
    Public Declare Function SetPort2 Lib "Exchange.dll" Alias "SetPort2" (ByVal status As Byte) As Byte
    Public Declare Function SetPort3 Lib "Exchange.dll" Alias "SetPort3" (ByVal status As Byte) As Byte
    Public Declare Function SetPort4 Lib "Exchange.dll" Alias "SetPort4" (ByVal status As Byte) As Byte
    Public Declare Function SetPort5 Lib "Exchange.dll" Alias "SetPort5" (ByVal status As Byte) As Byte
    Public Declare Function SetPort6 Lib "Exchange.dll" Alias "SetPort6" (ByVal status As Byte) As Byte

    '******************** 64 BIT ********************
    '"Exchange64.dll" für 64 bit Systeme benutzen.
    'Use "Exchange64.dll" on 64 bit Systems.

    'Public Declare Function GetChannel1 Lib "Exchange64.dll" Alias "GetChannel1" () As IntPtr
    'Public Declare Function GetChannel2 Lib "Exchange64.dll" Alias "GetChannel2" () As IntPtr
    'Public Declare Function GetChannel3 Lib "Exchange64.dll" Alias "GetChannel3" () As IntPtr
    'Public Declare Function GetChannel4 Lib "Exchange64.dll" Alias "GetChannel4" () As IntPtr
    'Public Declare Function GetChannel5 Lib "Exchange64.dll" Alias "GetChannel5" () As IntPtr
    'Public Declare Function GetChannel6 Lib "Exchange64.dll" Alias "GetChannel6" () As IntPtr
    'Public Declare Function GetSensor1 Lib "Exchange64.dll" Alias "GetSensor1" () As IntPtr
    'Public Declare Function GetSensor2 Lib "Exchange64.dll" Alias "GetSensor2" () As IntPtr
    'Public Declare Function GetPort1 Lib "Exchange64.dll" Alias "GetPort1" () As Byte
    'Public Declare Function GetPort2 Lib "Exchange64.dll" Alias "GetPort2" () As Byte
    'Public Declare Function GetPort3 Lib "Exchange64.dll" Alias "GetPort3" () As Byte
    'Public Declare Function GetPort4 Lib "Exchange64.dll" Alias "GetPort4" () As Byte
    'Public Declare Function GetPort5 Lib "Exchange64.dll" Alias "GetPort5" () As Byte
    'Public Declare Function GetPort6 Lib "Exchange64.dll" Alias "GetPort6" () As Byte
    'Public Declare Function SetPort1 Lib "Exchange64.dll" Alias "SetPort1" (ByVal status As Byte) As Byte
    'Public Declare Function SetPort2 Lib "Exchange64.dll" Alias "SetPort2" (ByVal status As Byte) As Byte
    'Public Declare Function SetPort3 Lib "Exchange64.dll" Alias "SetPort3" (ByVal status As Byte) As Byte
    'Public Declare Function SetPort4 Lib "Exchange64.dll" Alias "SetPort4" (ByVal status As Byte) As Byte
    'Public Declare Function SetPort5 Lib "Exchange64.dll" Alias "SetPort5" (ByVal status As Byte) As Byte
    'Public Declare Function SetPort6 Lib "Exchange64.dll" Alias "SetPort6" (ByVal status As Byte) As Byte

    Private Sub tuwas()
        p1 = GetChannel1()
        p2 = GetChannel2()
        p3 = GetChannel3()
        p4 = GetChannel4()
        p5 = GetChannel5()
        p6 = GetChannel6()

        p7 = GetPort1() 'Switch status (1=ON 0=OFF)
        p8 = GetPort2() 'Switch status (1=ON 0=OFF)
        p9 = GetPort3() 'Switch status (1=ON 0=OFF)
        p10 = GetPort4() 'Switch status (1=ON 0=OFF)
        p11 = GetPort5() 'Switch status (1=ON 0=OFF)
        p12 = GetPort6() 'Switch status (1=ON 0=OFF)

        p13 = GetSensor1()
        p14 = GetSensor2()
    End Sub


    Private Sub tmrRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRefresh.Tick
        tuwas()

        tbxP1.Text = Marshal.PtrToStringAnsi(p1)
        If (Marshal.PtrToStringAnsi(p1) <> "No Server!") Then
            Marshal.FreeHGlobal(p1)
        End If

        tbxP2.Text = Marshal.PtrToStringAnsi(p2)
        If (Marshal.PtrToStringAnsi(p2) <> "No Server!") Then
            Marshal.FreeHGlobal(p2)
        End If

        tbxP3.Text = Marshal.PtrToStringAnsi(p3)
        If (Marshal.PtrToStringAnsi(p3) <> "No Server!") Then
            Marshal.FreeHGlobal(p3)
        End If

        tbxP4.Text = Marshal.PtrToStringAnsi(p4)
        If (Marshal.PtrToStringAnsi(p4) <> "No Server!") Then
            Marshal.FreeHGlobal(p4)
        End If

        tbxP5.Text = Marshal.PtrToStringAnsi(p5)
        If (Marshal.PtrToStringAnsi(p5) <> "No Server!") Then
            Marshal.FreeHGlobal(p5)
        End If

        tbxP6.Text = Marshal.PtrToStringAnsi(p6)
        If (Marshal.PtrToStringAnsi(p6) <> "No Server!") Then
            Marshal.FreeHGlobal(p6)
        End If

        tbxTasterP1.Text = GetPort1.ToString 'Switch status (1=ON 0=OFF)
        tbxTasterP2.Text = GetPort2.ToString 'Switch status (1=ON 0=OFF)
        tbxTasterP3.Text = GetPort3.ToString 'Switch status (1=ON 0=OFF)
        tbxTasterP4.Text = GetPort4.ToString 'Switch status (1=ON 0=OFF)
        tbxTasterP5.Text = GetPort5.ToString 'Switch status (1=ON 0=OFF)
        tbxTasterP6.Text = GetPort6.ToString 'Switch status (1=ON 0=OFF)

        tbxSensor1.Text = Marshal.PtrToStringAnsi(p13)
        If (Marshal.PtrToStringAnsi(p13) <> "No Server!") Then
            Marshal.FreeHGlobal(p13)
        End If

        tbxSensor2.Text = Marshal.PtrToStringAnsi(p14)
        If (Marshal.PtrToStringAnsi(p14) <> "No Server!") Then
            Marshal.FreeHGlobal(p14)
        End If
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Me.Close()
    End Sub

    Private Sub cmdOn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn1.Click
        SetPort1(1) 'ON
    End Sub

    Private Sub cmdOff1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff1.Click
        SetPort1(0) 'OFF
    End Sub

    Private Sub cmdOn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn2.Click
        SetPort2(1) 'ON
    End Sub

    Private Sub cmdOff2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff2.Click
        SetPort2(0) 'OFF
    End Sub

    Private Sub cmdOn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn3.Click
        SetPort3(1) 'ON
    End Sub

    Private Sub cmdOff3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff3.Click
        SetPort3(0) 'OFF
    End Sub

    Private Sub cmdOn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn4.Click
        SetPort4(1) 'ON
    End Sub

    Private Sub cmdOff4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff4.Click
        SetPort4(0) 'OFF
    End Sub

    Private Sub cmdOn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn5.Click
        SetPort5(1) 'ON
    End Sub

    Private Sub cmdOff5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff5.Click
        SetPort5(0) 'OFF
    End Sub

    Private Sub cmdOn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOn6.Click
        SetPort6(1) 'ON
    End Sub

    Private Sub cmdOff6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOff6.Click
        SetPort6(0) 'OFF
    End Sub

    Private Sub VB_API_Demo_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    End Sub
End Class