Can you use C ++ libraries natively in a WinForms C ++ / CLI application?

You can create a VC ++ Windows Forms project that runs on the CLR, which is essentially a .NET application written in C ++. Can you use unmanaged C ++ libraries directly from such a project? Does this mean that the library should compile and run under .NET? Or should you write CLR shell classes for such libraries, and only those that will be used from the CLR application?

0
source share
2 answers

Yes. Here are some guides. CLI / C ++ mixing and native code. You do not need a shell for use in CLI / C ++. In fact, you use CLI / C ++ and native code to create the wrapper.

http://www.technical-recipes.com/2012/mixing-managed-and-native-types-in-c-cli/

http://www.codeproject.com/Articles/35041/Mixing-NET-and-native-code

If you are actually trying to make a shell for use in C #, it should look something like this:

#include "NativeClass.h"

public ref class NativeClassWrapper {
    NativeClass* m_nativeClass;

public:
    NativeClassWrapper() { m_nativeClass = new NativeClass(); }
    ~NativeClassWrapper() { delete m_nativeClass; }
    void Method() {
        m_nativeClass->Method();
    }

protected:
    // an explicit Finalize() method—as a failsafe
    !NativeClassWrapper() { delete m_nativeClass; }
};

Refer to the C ++ / CLI shell for C ++ for use as a reference in C #

+1
source

( ) , , Dispose(), Dispose() ..

Dr_Asik, , :

#include "clr_scoped_ptr.h"
#include "NativeClass.h"

public ref class NativeClassWrapper {
    clr_scoped_ptr<NativeClass> m_nativeClass;

public:
    NativeClassWrapper() m_nativeClass(new NativeClass()) {}
    // auto-generated destructor is correct
    // auto-generated finalizer is correct

    void Method() {
        m_nativeClass->Method();
    }
};

marshal_as, Microsoft . ildjarn : ++/CLI

+1

All Articles