Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27376

Combine 3 byte buffers...

$
0
0
I was wondering if anyone could help me with combining 3 byte buffers. I am using a licening piece of software has supplied the below code example on there website, which is to create a unique machinecode. The problem is that the example shows how to use 2 computer parameter e.g. GetCPUId and GetMachineName() however I was to use GetCPUId(), GetMachineName() and GetMBSerial().

The problem is that I do not understand how to do this. I know this is probably simple, but I am still learning VB.Net.

Any help in showing what to modify would be really appreshiated.

Code:

        ' Override to provide custom machine code
        ' This example uses the CPU-ID as the machine code
        Public Overrides Function GetLocalMachineCode() As Byte()
            Dim ret As Byte() = Nothing

            Try
                ret = CombineBuffersWithLength(GetCPUId(), GetMachineName())
            Catch ex As Exception
            End Try

            ' Fall back to base implementation if failed
            If ret Is Nothing = True Or ret.Length = 2 Then
                ret = MyBase.GetLocalMachineCode()
            End If

            Return ret
        End Function

        ' Gets the CPU-ID of the local machine
        Protected Shared Function GetCPUId() As Byte()
            Dim mgmt As ManagementClass = New ManagementClass("Win32_Processor")
            Dim objCol As ManagementObjectCollection = mgmt.GetInstances()
            Dim obj As ManagementObject

            For Each obj In objCol
                ' Only use CPU-ID from the first CPU
                Dim cpuInfo As String = obj.Properties("ProcessorId").Value.ToString()
                If (cpuInfo Is Nothing = False And cpuInfo.Length > 0) Then
                    Dim ret As Byte() = Encoding.UTF8.GetBytes(cpuInfo)
                    Return ret
                End If
            Next

            Return New Byte(0) {}
        End Function

        Protected Shared Function GetMBSerial() As Byte()
            Try
                Dim mgmt As ManagementClass = New ManagementClass("Win32_BaseBoard")
                Dim objCol As ManagementObjectCollection = mgmt.GetInstances()
                Dim obj As ManagementObject

                For Each obj In objCol
                    ' Only use CPU-ID from the first CPU
                    Dim mbserial As String = obj.Properties("SerialNumber").Value.ToString()
                    If (mbserial Is Nothing = False And mbserial.Length > 0) Then
                        Dim ret As Byte() = Encoding.UTF8.GetBytes(mbserial)
                        Return ret
                    End If
                Next
            Catch ex As Exception

            End Try
            Return New Byte(0) {}
        End Function

        Public Shared Function GetMachineName() As Byte()
            Dim computerName As String = Nothing

            computerName = MachineName.ToString
            If computerName <> "" Then
                Dim ret As Byte() = Encoding.UTF8.GetBytes(computerName)
                Return ret
            End If

            Return New Byte(0) {}
        End Function

        Public Shared Function GetUserName() As Byte()
            Dim userName As String = Nothing

            userName = UCase(Environment.UserName)
            If userName <> "" Then
                Dim ret As Byte() = Encoding.UTF8.GetBytes(userName)
                Return ret
            End If

            Return New Byte(0) {}
        End Function

        Shared Function CombineBuffersWithLength(ByVal buff1 As Byte(), ByVal buff2 As Byte()) As Byte()
            ' Returned format is:
            ' buff1length-....buff1....-buff2length-...buff2....
            Dim ret As Byte() = New Byte(buff1.Length + buff2.Length + 2) {}
            ret(0) = CType(buff1.Length, Byte)
            buff1.CopyTo(ret, 1)
            ret(buff1.Length + 1) = CType(buff2.Length, Byte)
            buff2.CopyTo(ret, buff1.Length + 2)

            Return ret
        End Function

        Shared Function AreBuffersEqual(ByVal buff1 As Byte(), ByVal buff2 As Byte()) As Boolean
            Try
                If (buff1.Length <> buff2.Length) Then
                    Return False
                End If
                Dim i As Integer
                For i = 0 To buff1.Length - 1
                    If (buff1(i) <> buff2(i)) Then
                        Return False
                    End If
                Next i

                Return True
            Catch ex As Exception
            End Try

            Return False
        End Function

        ' Gets machine code components
        Shared Function SplitBuffer(ByVal buff As Byte(), ByRef buff1 As Byte(), ByRef buff2 As Byte()) As Boolean
            buff1 = Nothing
            buff2 = Nothing

            Try
                Dim buff1Length As Byte = buff(0)
                buff1 = New Byte(buff1Length) {}
                Buffer.BlockCopy(buff, 1, buff1, 0, buff1Length)

                Dim buff2Length As Byte = buff(buff1Length + 1)
                buff2 = New Byte(buff2Length) {}
                Buffer.BlockCopy(buff, buff1Length + 2, buff2, 0, buff2Length)

                Return True
            Catch
                buff1 = Nothing
                buff2 = Nothing
            End Try
            Return False
        End Function


Viewing all articles
Browse latest Browse all 27376

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>