Topic Options
#1422 - 09/22/07 08:56 AM Linking to dlls
SDallas Offline
Junior Member

Registered: 09/04/07
Posts: 7
Loc: Nashville, TN
This may be a stupid question, but how can I link to a dll? Can it be done?

Top
#1423 - 09/22/07 08:41 PM Re: Linking to dlls [Re: SDallas]
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Yes, it’s really quite easy.
There are tons of articles on it, just google “calling a DLL from C#” and you should get lots of hits.
here is one such article:
http://www.csharphelp.com/archives/archive52.html

I do it all the time .. one of my WinCron scripts calls zlib.dll to do compression, I set it up like this:

 Code:
using System;
using System.Runtime.InteropServices;

namespace Server
{
	public enum ZLibError : int
	{
		Z_OK = 0,
		Z_STREAM_END = 1,
		Z_NEED_DICT = 2,
		Z_ERRNO = (-1),
		Z_STREAM_ERROR = (-2),
		Z_DATA_ERROR = (-3),
		Z_MEM_ERROR = (-4),
		Z_BUF_ERROR = (-5),
		Z_VERSION_ERROR = (-6),
	}

	public enum ZLibCompressionLevel : int
	{
		Z_NO_COMPRESSION = 0,
		Z_BEST_SPEED = 1,
		Z_BEST_COMPRESSION = 9,
		Z_DEFAULT_COMPRESSION = (-1)
	}

	public class ZLib
	{
		[DllImport("./server/bin/zlib.dll")]
		public static extern string zlibVersion();
		[DllImport("./server/bin/zlib.dll")]
		public static extern ZLibError compress( byte[] dest, ref int destLength, byte[] source, int sourceLength );
		[DllImport("./server/bin/zlib.dll")]
		public static extern ZLibError compress2( byte[] dest, ref int destLength, byte[] source, int sourceLength, ZLibCompressionLevel level );
		[DllImport("./server/bin/zlib.dll")]
		public static extern ZLibError uncompress( byte[] dest, ref int destLen, byte[] source, int sourceLen );
	}
}


Then call it like this:

int deflatedLength = m_DeflatedBuffer.Length;
ZLibError ce = ZLib.compress2(m_DeflatedBuffer, ref deflatedLength, inflatedBuffer, size, ZLibCompressionLevel.Z_DEFAULT_COMPRESSION);
_________________________
Regards,
Luke Tomasello

Top


Moderator:  Luke Tomasello