Using zlib with a Visual C++ CLR Windows Forms Application
I've been trying to do this for a long time, and that is to get the zlib library to work with my program. I'm using Microsoft Visual C++ 2008 Express Edition with a CLR Windows Forms Application. The thing is I'm worried that zlib is to old to work with what I'm using. All the examples (the very few examples) of zlib I find are with a Win32 application.
And that's the other thing; I can't find any zlib examples that aren't outdated. And I do under开发者_运维知识库stand that zlib is old, but I can't find any alternatives :(
So if anyone could first of all tell me if its possible to use zlib with my application, and how, thanks a bunch. If someone wants to take it a step further and maybe recommend a better zlib-like program that I can use, thanks even more.
(And by the way, I'm creating an application to read the level data of the game Minecraft. A little can be found on that here.)
It is not a problem, C++/CLI was made to handle tasks like this. You have to tell the compiler that the zlib header isn't managed code with #pragma managed and tell the linker to link the import library. Like this, in a sample console mode app:
#include "stdafx.h"
#pragma managed(push, off)
#include "c:/temp/deleteme/include/zlib.h"
#pragma managed(pop)
#pragma comment(lib, "c:/temp/deleteme/lib/zdll.lib")
using namespace System;
int main(array<System::String ^> ^args)
{
const char* vers = zlibVersion();
String^ mvers = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)(void*)vers);
Console::WriteLine(mvers);
Console::ReadKey();
return 0;
}
Only other thing you have to do is to get the DLL copied into the build directory so you can debug the code. Project + Properties, Build Events, Post-Build Event, Command line:
xcopy /d "c:\temp\deleteme\zlib1.dll" "$(TargetDir)"
Maybe you want to pick a different install directory :)
I've used the DotNetZip library in several C++/CLI applications. It's very easy to get going and I've yet to find anything it can't do (zipping, unzipping, password protected zip files all works well). From the description, it's an improvement over ZLib:
If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, DotNetZip has that, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance than the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952).
If you're using CLR, can't you use the built in .NET gzip library? http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
精彩评论