你可以在C ++ / CLI的WinForms应用程序中使用C ++库本身? [英] Can you use C++ libraries natively in a C++/CLI WinForms application?

查看:129
本文介绍了你可以在C ++ / CLI的WinForms应用程序中使用C ++库本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以创建一个VC ++ Windows窗体上的CLR,这基本上是一个.NET应用程序用C ++编写运行项目。你能直接从这样的项目中使用非托管C ++库?这是否意味着该库必须编译,并在.NET运行?或者要你写<一个href="http://www.multicoreconsulting.co.uk/blog/c-snippets/how-to-call-unmanaged-cplusplus-from-csharp/"相对=nofollow> CLR包装类的这种图书馆,只有那些会从CLR应用程序?

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

推荐答案

是的。这里有一些指南。混合CLI / C ++和本土code。你并不需要一个包装使用他们在CLI / C ++。事实上,你使用CLI / C ++和本土code创建一个包装。

Yes. Here are some guides. Mixing CLI/C++ and native code. You don't need a wrapper to use them in CLI/C++. In fact, you use CLI/C++ and native code to create a wrapper.

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

HTTP:// WWW。 codeproject.com /用品/ 35041 /混合-NET-和-native- code

如果你实际上是试图使包装在C#中使用,它应该是这个样子:

If you are actually trying to make a wrapper to 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; }
};

请参照<一个href="http://stackoverflow.com/questions/10223186/c-cli-wrapper-for-native-c-to-use-as-reference-in-c-sharp">C++/CLI包装本地C ++与C#参考使用

这篇关于你可以在C ++ / CLI的WinForms应用程序中使用C ++库本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆