Results 1 to 3 of 3

Thread: Combine 3 byte buffers...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Combine 3 byte buffers...

    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

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Combine 3 byte buffers...

    In general byte() can be combined by adding them to a list.

    Code:
            'for testing create some byte arrays
            Dim someBytes1() As Byte = New Byte() {1, 2, 3}
            Dim someBytes2() As Byte = New Byte() {11, 12, 13, 14}
            Dim someBytes3() As Byte = New Byte() {251, 252, 253, 254, 255}
            Dim combine As New List(Of Byte)
    
            combine.AddRange(someBytes1)
            combine.AddRange(someBytes2)
            combine.AddRange(someBytes3)
            'combine has all the bytes
            'if they need to be in an array
            Dim combined() As Byte = combine.ToArray
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Combine 3 byte buffers...

    try this:

    Code:
    Shared Function CombineBuffersWithLength(ByVal buff1 As Byte(), ByVal buff2 As Byte()) As Byte()
        Dim ret As Byte() = buff1.Concat(buff2).ToArray
        Return ret
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width