Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine 3 byte buffers...

Status
Not open for further replies.

lidds

Programmer
Jun 9, 2005
72
0
0
GB
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
 

I think you just need to create a buffer to hold the first 2 combinations, then combine that with the third:

Public Overrides Function GetLocalMachineCode() As Byte()
Dim temp As Byte() = Nothing
Dim ret As Byte() = Nothing

Try
temp = CombineBuffersWithLength(GetCPUId(), GetMachineName())

ret = CombineBuffersWithLength(temp, GetMBSerial())
Catch ex As Exception
End Try

' Fall back to base implementation if failed
If ret Is Nothing = True Or ret.Length = 2 Then 'you may need to change this since there are 3 buffers being combined now
ret = MyBase.GetLocalMachineCode()
End If

Return ret
End Function

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top