# C# and zlib1.dll

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to access the functions inside the unmanaged zlib1.dll (zlib1.dll is the official release of zlib as a dll) with managed C#. I did all the marshaling, data type conversions, unsafe code, etc. already so it should all be working now. But I'm stuck because the Runtime keeps throwing these EntryPointNotFoundException exceptions. The documentation says this about the exception:
Represents the error that occurs when an attempt to load an Assembly fails due to the absence of an entry point.
This exception only occurs when I try to call deflateInit() and inflateInit(). Here's some of the code that I'm using: These are the declarations of the first four functions in the DLL. The basis for the declarations were taken from the declarations the zlib.h C header file. All of the functions listed here work except for deflateInit() (as calling it from managed code throws the exception):
// C#

namespace Zlib
{
public class Zlib
{
public class z_stream
{
// contents omitted to maintain clarity
// this is basically a C# version of z_stream struct in zlib.h with all the types marshaled
// this structure is used to interface with zlib
// not sure if they are marshaled correctly or not...
...
}

[System.Runtime.InteropServices.DllImport("zlib1.dll")]
public static extern System.String zlibVersion();

[System.Runtime.InteropServices.DllImport("zlib1.dll")]
public static extern System.Int32 deflateInit(
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStruct)]
ref z_stream strm,
System.Int32 level
);

[System.Runtime.InteropServices.DllImport("zlib1.dll")]
public static extern System.Int32 deflate(
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStruct)]
ref z_stream strm,
System.Int32 flush
);

[System.Runtime.InteropServices.DllImport("zlib1.dll")]
public static extern System.Int32 deflateEnd(
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStruct)]
ref z_stream strm
);
}
}


For contrast, here are the C declarations in zlib.h:
// C
// you can basically ignore all the ZEXPORT and OF stuff as they don't do anything
// then again, maybe ignoring it could be the cause of my problem

typedef z_stream FAR *z_streamp;
ZEXTERN const char * ZEXPORT zlibVersion OF((void));
ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));


All of the other functions in the header/DLL haven't been tested yet as I haven't written the code to implement them. The only thing that I can think of about the Init functions that might bear significance to this problem is that they do initialize the state member in z_stream and that is declared as a System.IntPtr in the managed version of the struct. Another possibility might be the memory management functions that zlib is using, although I don't have enough knowledge of that to be sure. So I guess my question is, is there something that deflateInit() and inflateInit() do that's preventing me from calling them? Do these functions depend on some sort of entry point somewhere (although I can't imagine how). Am I missing something? Any help would be greatly appreciated. Edit: threw a 'ref' in front of every z_stream parameter, this allows the unmanaged code to modify the data in the managed structure instead of just modifying a copy. Unfortunately, this still doesn't solve the problem. The runtime still cannot find a function by the name of 'deflateInit' in 'zlib1.dll'. [Edited by - Ridiculous on February 2, 2007 2:11:40 AM]

##### Share on other sites
Unless you have specific reasons to use the original zlib, why don't you just use #ziplib?

It has support for many more compression algorithms (such as bzip2) and is written in pure C# code, so it won't cause trouble when your app is run in an x64 windows or a linux mono environment.

I can't see any obvious mistakes in your code, thought. Maybe the return value of version should have a [MarshalAs(UnmanagedType.LPTStr)] attribute, but that would cause a different exception if the program would be getting that far.

-Markus-

##### Share on other sites
Just tested this code snippet with Visual C# Express:

using System;using System.Runtime.InteropServices;namespace DllCall {  class Program {    [System.Runtime.InteropServices.DllImport("zlib1.dll")]    public static extern System.String zlibVersion();    static void Main(string[] args) {      string s = zlibVersion();          }  }} // namespace DllCall

And it worked without problems.

-Markus-

##### Share on other sites
Ah, yeah, I have looked at #ziplib but I want to get this to work because I haven't seen any .NET interface that provides the same interface as zlib without wrapping it in something simpler.

Anyway, yeah, zlibVersion() works for me too even without marshaling the return value. All of the functions that I listed work except for deflateInit() and inflateInit(), so I'm still stuck. Of course, I can't really know if deflate(), deflateEnd(), inflate(), and inflateEnd() are working because they depend on deflateInit() and inflateInit() to initialize the z_stream structure. But calling them does not result in an exception. They return -2, which is essentially a stream error.

Thanks for the info though. [cool]

I know it's messy dealing with low level things like bytes in managed code, so if someone else or I can't figure this out, I'll probably move on to #ziplib.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×