Using zlib within a managed MS Visual C++ 2005 Express project

[FYI, I'm somewhat new to the latest Visual Studio incarnations and am coming up to speed as quickly as possible. ]

Has anyone managed to get zlib 1.2.3 to work within a managed MS Visual C++ 2005 Express project (using /clr:safe) I have gone so far as to successfully rebuild the zlib DLL and lib within the same IDE (different project) successfully using the original sources (though the project had to be converted automatically from the older version first). However, zlib is not in C++ .Net (it's pretty much standard C++) and so I cannot simply reference its DLL as part of the project (it's not clear to me how you'd make that call). I'm in need of some direction, i.e., how to load the DLL and how to reference some part of it. I'm getting nowhere fast so any help is appreciated. I figure it has to do with DllImport but I'm not sure how to use it properly yet.

[Time passed... coding happened...]

I did get this much to work finally:
[DllImport("zlib1.dll", EntryPoint="zlibVersion", SetLastError=true)]
String ^ zlibVersion(); // returns "1.2.3" when invoked from "main" which is as expected


Now that I think I can deal with functions, I wonder how to get all the external variables to be visible (i.e., #defined constants)... if nothing else I can just use the original header file (with adjustments) but perhaps there is a smoother way

Thanks in advance,
Marty


Answer this question

Using zlib within a managed MS Visual C++ 2005 Express project

  • shivali.sadavarte

    Unmanaged library can be used by C++/CLI client by the same way as by unmanaged C++ client. All .lib name to the list of libraries for linker, include required zlib h-files to cpp files, and call unmanaged functions directly from managed code. This technology is sometimes called IJW (It Just Works). C++/CLI developer doesn't need PInvoke - it is for C# and VB.
  • Jamie Thomson

    Remove DllImport line, you don't need PInvoke. Instead of this add zlib.lib to linker settings.
  • KrisFB_APPS

    The further I go the more complicated it gets... the little snippet worked but when you add zlib.h and zconf.h to the mix it all goes kinda haywire. You can avoid the "unverifiable" errors easily enough (I'm more curious how to wedge this in than in making it verifiable... but I do like using managed code where I can too). However, all the typedefs and function defs seem to throw up another roadblock:
    error C3115: 'System::Runtime::InteropServices::DllImportAttribute': this attribute is not allowed on '<any given argument typedefed in zlib.h>'

    Below is a snippet from the start of my code. Rearranging the includes doesn't seem to help matters a great deal, though if zlib.h is above DllImport I get unresolved external token/symbol errors during compilation.

    ......
    #include "stdafx.h"
    using namespace ThisApp;
    using namespace System::Runtime::InteropServices;

    #pragma warning(disable : 4956 4959)
    #include "zconf.h"
    [DllImport("zlib1.dll", SetLastError=true)]
    #include "zlib.h"
    #pragma warning(default : )

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    ......

    I am getting close to giving up on this and using something else (or even worse, rewriting zlib) but it seems to me this shouldn't be that difficult.

    Regards,
    Marty




  • Pockey

    Of course, the vast majority of things with IJW will never work with /clr:safe. Even though you can disable most compiler errors re verifiability you won't get a verifiable image. That said, that's hardly different from the PInvoke case.

    -hg


  • Using zlib within a managed MS Visual C++ 2005 Express project