mardi 4 août 2015

How can I add a SWIG-generated C++ DLL reference to a C# project?

I am using SWIG to generate a DLL that will expose C++ functionality to a C# project. At the moment I:

  1. Define a SWIG interface file

    %module example
    %{
    /* Includes the header in the wrapper code */
    #include "../pointmatcher/PointMatcher.h"
    %}
    
    ...
    
    %include "../pointmatcher/PointMatcher.h"
    
    
  2. Use SWIG to generate a .cxx wrapper

    swig.exe -c++ -csharp -outdir csharp example.i
    
    
  3. Compile the .cxx wrapper with MSBUILD via CMake

    # create wrapper DLL
    add_library(example SHARED ${WRAP_CSHARP_FILE})
    target_link_libraries(example pointmatcher)
    install(TARGETS example
            ARCHIVE DESTINATION ${INSTALL_LIB_DIR}
            LIBRARY DESTINATION ${INSTALL_LIB_DIR}
            RUNTIME DESTINATION ${INSTALL_BIN_DIR})
    
    

I then have a DLL file (example.dll) which I can inspect via Dependency Walker, and confirm that methods are being exposed as follows:

Dependency Walker inspection of DLL

However, when I try to add this MSVC DLL as a reference to a C# project I get the error "It is not a valid assembly or COM component".

Based on answers at How can I add a VC++ DLL as a reference in my C# Visual Studio project? I have confirmed that SWIG itself generates P/Invoke calls, and that tlbimp doesn't recognise the DLL either.

Aucun commentaire:

Enregistrer un commentaire