For all the newcomers to VB.Net (I know this is simple stuff to experienced users), below is a class in VB that compresses and decompresses byte arrrays. Thanks to all the help from other users.
'DATACOMPRESS CLASS - VB.Net
'************** *************** *************** *************** *************** *************** *************** **
' METHODS:
' CompressBytes(S ource ByteArray, Optional Temporay Byte Array
' Compresses ByteArray into Temporary Byte Array or into ByteArray if Temporary Byte Array not input
'
' DeCompressBytes (Source ByteArray, Original Array Size before compression as integer, Optional Temporary Byte Array)
' Decompresses ByteArray into Temporary Byte Array or into ByteArray if Temporary Byte Array not input
'
'************** *************** *************** *************** *************** *************** *************** ***
Imports System.Runtime. InteropServices
Public Class DataCompress
'Declare zlib functions "Compress" and "Uncompress " for compressing Byte Arrays
<DllImport("zli b.DLL", EntryPoint:="co mpress")> _
Private Shared Function CompressByteArr ay(ByVal dest As Byte(), ByRef destLen As Integer, ByVal src As Byte(), ByVal srcLen As Integer) As Integer
' Leave function empty - DLLImport attribute forwards calls to CompressByteArr ay to compress in zlib.dLL
End Function
<DllImport("zli b.DLL", EntryPoint:="un compress")> _
Private Shared Function UncompressByteA rray(ByVal dest As Byte(), ByRef destLen As Integer, ByVal src As Byte(), ByVal srcLen As Integer) As Integer
' Leave function empty - DLLImport attribute forwards calls to UnCompressByteA rray to Uncompress in zlib.dLL
End Function
Public Sub New()
MyBase.New()
End Sub
Public Function CompressBytes(B yRef Data() As Byte, Optional ByRef TempBuffer() As Byte = Nothing) As Integer
'Compresses Data into a temp buffer
'Returns compressed Data in Data if TempBuff not specified
'Returns Result = Size of compressed data if ok, -1 if not
Dim OriginalSize As Long = UBound(Data) + 1
'Allocate temporary Byte Array for storage
Dim result As Integer
Dim usenewstorage As Boolean
If TempBuffer Is Nothing Then usenewstorage = False Else usenewstorage = True
Dim BufferSize As Integer = UBound(Data) + 1
BufferSize = CInt(BufferSize + (BufferSize * 0.01) + 12)
ReDim TempBuffer(Buff erSize)
'Compress data byte array
result = CompressByteArr ay(TempBuffer, BufferSize, Data, UBound(Data) + 1)
'Store results
If result = 0 Then
If usenewstorage Then
'Return results in TempBuffer
ReDim Preserve TempBuffer(Buff erSize - 1)
Else
'Return compressed Data in original Data Array
' Resize original data array to compressed size
ReDim Data(BufferSize - 1)
' Copy Array to original data array
Array.Copy(Temp Buffer, Data, BufferSize)
'Release TempBuffer STorage
TempBuffer = Nothing
End If
Return BufferSize
Else
Return -1
End If
End Function
Public Function DeCompressBytes (ByRef Data() As Byte, ByVal Origsize As Integer, Optional ByRef TempBuffer() As Byte = Nothing) As Integer
'DeCompresses Data into a temp buffer..note that Origsize must be the size of the original data before compression
'Returns compressed Data in Data if TempBuff not specified
'Returns Result = Size of decompressed data if ok, -1 if not
'Allocate memory for buffers
Dim result As Integer
Dim usenewstorage As Boolean
Dim Buffersize As Integer = CInt(Origsize + (Origsize * 0.01) + 12)
If TempBuffer Is Nothing Then usenewstorage = False Else usenewstorage = True
ReDim TempBuffer(Buff ersize)
'Decompress data
result = UncompressByteA rray(TempBuffer , Origsize, Data, UBound(Data) + 1)
'Truncate buffer to compressed size
If result = 0 Then
If usenewstorage Then
'Return decoompressed data in TempBuffer
ReDim Preserve TempBuffer(Orig size - 1)
Else
'Return decompressed data in original source data file
' Truncate to compressed size
ReDim Data(Origsize - 1)
' Copy Array to original data array
Array.Copy(Temp Buffer, Data, Origsize)
'Release TempBuffer STorage
TempBuffer = Nothing
End If
Return Origsize
Else
Return -1
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub
End Class
--
Dennis in Houston
"Dennis" wrote:
Thanks to help from Cor and Herfried, I got it to work as follows:
<DllImport("zli b.DLL", EntryPoint:="co mpress", SetLastError:=T rue, CharSet:=CharSe t.Unicode, ExactSpelling:= True, _
CallingConventi on:=CallingConv ention.StdCall) > _
Public Shared Function compressv(ByVal dest As Byte(), ByRef destLen As Integer, ByVal src As Byte(), ByVal srcLen As Integer) As Integer
' Leave function empty - DLLImport attribute forwards calls to compressv to
' compress in zlib.dLL
End Function
After this declaration, you can use normal calling conventions to compressV by specifying byte arrays, integers, etc. Since in VB.Net, a delcare statement for an unmanaged DLL invokes dllimport, I don't understand why M'soft didn't allow standard calling conventions in the Declare statements. But anyway, it works now. Thanks to help I got.
--
Dennis in Houston
"Dennis" wrote:
I am trying to use ZLIB.Dll in a VB.Net project but keep getting an error message that says it can't load the DLL. I tried to add a reference using "Project-Add Reference" but get the error message stating it's not a valid Com component. I'm new to vb.net so any help would be appreciated. Thanks.
--
Dennis in Houston