Adding DLL references to VB.Net Project

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
* =?Utf-8?B?RGVubmlz?= said:
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.

That's because "ZLIB.DLL" is a "standard DLL", which exports functions,
and it's no .NET assembly or COM DLL. Instead of referencing it, you
can use the functions by declaring them ('Declare', 'DllImport', see
chapters about platform invocation in help).
 
Thanks to help from Cor and Herfried, I got it to work as follows:

<DllImport("zlib.DLL", EntryPoint:="compress", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.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.
 
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(Source 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("zlib.DLL", EntryPoint:="compress")> _
Private Shared Function CompressByteArray(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 CompressByteArray to compress in zlib.dLL
End Function
<DllImport("zlib.DLL", EntryPoint:="uncompress")> _
Private Shared Function UncompressByteArray(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 UnCompressByteArray to Uncompress in zlib.dLL
End Function

Public Sub New()
MyBase.New()
End Sub

Public Function CompressBytes(ByRef 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(BufferSize)
'Compress data byte array
result = CompressByteArray(TempBuffer, BufferSize, Data, UBound(Data) + 1)
'Store results
If result = 0 Then
If usenewstorage Then
'Return results in TempBuffer
ReDim Preserve TempBuffer(BufferSize - 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(TempBuffer, 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(Buffersize)

'Decompress data
result = UncompressByteArray(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(Origsize - 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(TempBuffer, 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
 
Hi Dennis,

Thanks for sharing this with the newgroups
I added Zip file in the header than it will be easy to find on Google.

Cor
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(Source 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("zlib.DLL", EntryPoint:="compress")> _
Private Shared Function CompressByteArray(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
CompressByteArray to compress in zlib.dLL
End Function
<DllImport("zlib.DLL", EntryPoint:="uncompress")> _
Private Shared Function UncompressByteArray(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
UnCompressByteArray to Uncompress in zlib.dLL
End Function

Public Sub New()
MyBase.New()
End Sub

Public Function CompressBytes(ByRef 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(BufferSize)
'Compress data byte array
result = CompressByteArray(TempBuffer, BufferSize, Data, UBound(Data) + 1)
'Store results
If result = 0 Then
If usenewstorage Then
'Return results in TempBuffer
ReDim Preserve TempBuffer(BufferSize - 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(TempBuffer, 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(Buffersize)

'Decompress data
result = UncompressByteArray(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(Origsize - 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(TempBuffer, 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
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.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.
 
Thanks for sharing this knowledge.

Dennis said:
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(Source 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("zlib.DLL", EntryPoint:="compress")> _
Private Shared Function CompressByteArray(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
CompressByteArray to compress in zlib.dLL
End Function
<DllImport("zlib.DLL", EntryPoint:="uncompress")> _
Private Shared Function UncompressByteArray(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
UnCompressByteArray to Uncompress in zlib.dLL
End Function

Public Sub New()
MyBase.New()
End Sub

Public Function CompressBytes(ByRef 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(BufferSize)
'Compress data byte array
result = CompressByteArray(TempBuffer, BufferSize, Data,
UBound(Data) + 1)
'Store results
If result = 0 Then
If usenewstorage Then
'Return results in TempBuffer
ReDim Preserve TempBuffer(BufferSize - 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(TempBuffer, 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(Buffersize)

'Decompress data
result = UncompressByteArray(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(Origsize - 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(TempBuffer, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top