zoukankan      html  css  js  c++  java
  • Mixed mode programming is the absolute power of C++/CLI

    From this article in codeproject.

    Mixed mode programming is the absolute power of C++/CLI, and so is C++/CLI the superior and mightiest of all programming languages. C++/CLI is to C++, as it is to C. You can do C programming in C++. In the same sense, you can do unmanaged C++ programming in C++/CLI without using any of the managed features, not even a managed class. (I would imagine a reason to do that sort of a thing for the rest of my life.) Also, you can do pure managed programming without using any of the unmanaged practices. You can also do mixed mode programming, which means you can write an application that has both managed and unmanaged classes interacting with each other. So, a managed object can contain or interact with an unmanaged object, and vice versa. Could you imagine the power of programming that you can unleash with C++/CLI? The simplest application of the above power is when you want to port your existing hi-fi image processing or math library written in C++ to work on the .NET platform. And, when you have not enough budget/time to rewrite it in C# (or VB.NET, would you try that), you can recompile your existing code with C++/CLI and write a (managed) wrapper so that they can be used by any .NET programming language. It does not take much effort to write a wrapper when you compare the effort of rewriting and testing it. Following is a managed class that interacts with an unmanaged object:-

    ref class ManagedClass
    {
    private: UnmanagedClass *unmgdPtr;

    public: ManagedClass(UnmanagedClass *unmgdClassPtr)
    {
    // similar to _ASSERTE(..); see nullptr usage.

    System::Diagnostics::Debug::Assert(unmgdClassPtr != nullptr);
    this->unmgdPtr = unmgdClassPtr;
    }

    public: // Some methods that use the unmgdPtr

    };

    class UnmanagedClass
    {
    public: UnmanagedClass()
    {
    }

    public: void SomeUnmgdMethod()
    {
    }

    // imagine a ton of other public methods


    };

    Likewise, an unmanaged class can bear a managed reference as its member and can invoke methods on it. But like a managed class holding the pointer to unmanaged, it cannot directly have the reference; instead, it is the following way:-

    // ref class Managed Class - Some managed class

    class UnmanagedClass
    {
    private: gcroot<ManagedClass^> mgdRef;
    public: UnmanagedClass(ManagedClass^ mgdClassRef)
    {
    Debug::Assert(mgdClassRef != nullptr);
    this->mgdRef = mgdClassRef;
    }

    // Methods that use mgdRef and invoke methods: mgd->SomeMethod();


    };

    gcroot is itself an unmanaged entity which has the ability to refer to managed entities. So, an instance of gcroot<managed> can be a statically or dynamically allocated member inside the unmanaged class.

  • 相关阅读:
    在Bootstrap开发框架中使用Grid++报表
    在Winform开发中使用Grid++报表
    客户关系管理系统中实现对客户信息标记颜色的处理
    企业微信和后台管理系统的结合管理(2)---创建企业微信应用并配置
    企业微信和后台管理系统的结合管理(1)---各个系统之间的关系和协同。
    基于Metronic的Bootstrap开发框架--资产编码打印处理
    企业微信发送应用消息的实现
    在后台业务管理系统中使用Autofac实现微信接口的处理
    企业微信支付的发送红包及相关接口使用
    开发框架数据库配置的几种应用场景
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1772969.html
Copyright © 2011-2022 走看看