Realize the dynamic library dll of C++ calling C#

        In the actual project process, sometimes a C# dll is called in a C++ project to complete a specific function. We all know that Native C++ cannot directly call a .NET platform dll. So is there a way to do this? The answer is yes.

        Let's take the OMCS real-time audio and video framework as an example. The OMCS WinPC SDK is developed in C#. Some customers who use C++ QT to develop Windows applications want to call OMCS for audio and video conversations or remote desktop functions. What should they do?

       Although Native C++ cannot call C# dll, Managed C++ (C++/CLI) can call C# dll, so C++/CLI can be used as a bridge to connect Native C++ and C#.

(1) Create a new C++/CLI library (such as OmcsWrap), add references and call OMCS.dll, use Managed C++ syntax to call components in OMCS.dll, and expose standard C/C++ interfaces.

(2) Compile the C++/CLI library to get OmcsWrap.dll and OmcsWrap.lib. The library's interface is in line with the C++ specification.

(3) In a Native C++ project (such as QT), just link OmcsWrap.dll and OmcsWrap.lib.

1. Example of C++/CLI calling OMCS: connecting a camera

      If you understand the basic usage of OMCS (if you don’t know it, you can check: OMCS Getting Started Demo: Audio and video, electronic whiteboard, remote desktop function display ), then the following C++/CLI calling code is easy to understand.

      Let's create a C++/CLI console project to demonstrate how to connect to any online user's camera through the OMCS camera connector and get the camera's video image Bitmap data.

using namespace System;
using namespace System::Drawing;
using namespace OMCS::Passive;
using namespace OMCS::Passive::Video;

ref class Tester
{
private:
    DynamicCameraConnector^ dynamicCameraConnector;
    int frameCount = 0;

public:

    Tester()
    {
        IMultimediaManager^ mgr = MultimediaManagerFactory::GetSingleton();

        //登陆 aa09
        mgr->Initialize("aa09", "", "127.0.0.1", 9900);
        Console::WriteLine(L"登录OMCS服务器成功!");
    }

    void Start()
    {
        dynamicCameraConnector = gcnew DynamicCameraConnector();
        dynamicCameraConnector->NewFrameReceived += gcnew ESBasic::CbGeneric<array<unsigned char, 1>^>(this, &Tester::OnNewFrameReceived);
        dynamicCameraConnector->ConnectEnded += gcnew ESBasic::CbGeneric<OMCS::Passive::ConnectResult>(this, &Tester::OnConnectEnded);

        //连接自己的摄像头
        dynamicCameraConnector->BeginConnect(L"aa09");
    }

    //摄像头图像数据
    void OnNewFrameReceived(array<unsigned char, 1>^ rgb24)
    {
        Bitmap^  bm = ESBasic::Helpers::ImageHelper::ConstructRGB24Bitmap(rgb24, dynamicCameraConnector->VideoSize.Width, dynamicCameraConnector->VideoSize.Height);

        ++this->frameCount;
        Console::WriteLine(L"收到图像帧:"  + this->frameCount.ToString("00000"));
    }

    //连接摄像头的结果
    void OnConnectEnded(OMCS::Passive::ConnectResult result)
    {
        if (result == ConnectResult::Succeed) //连接成功
        {
            Console::WriteLine(L"连接摄像头成功!");
        }
        else
        {
            Console::WriteLine(L"连接摄像头失败!原因:" + result.ToString());
        }
    }

    void Stop()
    {
        if (this->dynamicCameraConnector != nullptr)
        {
            if (this->dynamicCameraConnector->Connected)
            {
                this->dynamicCameraConnector->Disconnect(); //断开到目标摄像头的连接
                Console::WriteLine(L"断开摄像头连接器!");
            }

            this->dynamicCameraConnector = nullptr;
        }        
    }
};

(1) This is just to print out the frame number of the received camera video image frame. In a real usage scenario, the image frame callback can be passed to QT, and QT can render the image on the UI control. This will allow you to see the video.

(2) Here, the camera is taken as an example, and the desktop is also in exactly the same mode, using DynamicDesktopConnector.

(3) For microphone sound, it is simpler, because it does not require UI rendering, so it is enough to call MicrophoneConnector directly in C++/CLI. When the target microphone is successfully connected, the local computer will automatically play its sound.

       After starting the OMCS server (you can download it from the end of the article), run the console program in this article, and the running effect is shown in the screenshot below:

 

 Here is just a brief indication of how C++/CLI calls OMCS. As for encapsulating a library for Native C++ to call C++/CLI, which APIs this library should provide depends on the specific project requirements, so I won’t give an example here.

2. Demo source code download   

1. C++/CLI calls OMCS Demo: CppCli-CallOMCS-Demo.rar 

2. Demo server + C# client: OMCS.Demos.Simplest.rar

3. Android client: OMCS.AndroidDemo.rar

4. iOS client: OMCS.IOSDemo.zip

5. Web 版:OMCS.WebDemo.rar

For the demo introduction of OMCS real-time video function, please refer to here .

Guess you like

Origin blog.csdn.net/zhuweisky/article/details/125128657