C++与C#交互 回调封装为await

cpp 复制代码
// HSProcWrapper.h
#pragma once
#include "HSProc.h"
#include <msclr/marshal_cppstd.h>

using namespace System;
using namespace System::Threading::Tasks;
using namespace System::Runtime::InteropServices;

namespace HSManaged
{
    public ref class HSProcWrapper
    {
    private:
        HSProc* nativeProc;

        ref struct InitContext
        {
            TaskCompletionSource<bool>^ tcs;
        };

        static void NativeCallback(
            int retCode,
            const char* errMsg,
            void* userData)
        {
            GCHandle handle = GCHandle::FromIntPtr(IntPtr(userData));
            InitContext^ ctx = (InitContext^)handle.Target;

            handle.Free();

            if (retCode == 0)
            {
                ctx->tcs->SetResult(true);
            }
            else
            {
                String^ msg = gcnew String(errMsg);
                ctx->tcs->SetException(
                    gcnew InvalidOperationException(msg)
                );
            }
        }

    public:
        HSProcWrapper()
        {
            nativeProc = new HSProc();
        }

        ~HSProcWrapper()
        {
            this->!HSProcWrapper();
        }

        !HSProcWrapper()
        {
            delete nativeProc;
            nativeProc = nullptr;
        }

        /// <summary>
        /// await 版本初始化
        /// </summary>
        Task^ InitAsync(
            int bitdepth,
            String^ maskfile,
            CAMTYPE camera,
            int maskRoiX,
            int maskRoiY,
            int maskRoiWidth,
            int maskRoiHeight)
        {
            auto tcs = gcnew TaskCompletionSource<bool>();
            auto ctx = gcnew InitContext();
            ctx->tcs = tcs;

            GCHandle handle = GCHandle::Alloc(ctx);

            std::wstring nativeMask =
                msclr::interop::marshal_as<std::wstring>(maskfile);

            nativeProc->initAsync(
                bitdepth,
                nativeMask,
                camera,
                maskRoiX,
                maskRoiY,
                maskRoiWidth,
                maskRoiHeight,
                &NativeCallback,
                GCHandle::ToIntPtr(handle).ToPointer()
            );

            return tcs->Task;
        }
    };
}

WPF调用

csharp 复制代码
private async void Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        await _proc.InitAsync(
            16,
            "mask.png",
            CAMTYPE.CAM_A,
            0, 0, 0, 0
        );

        MessageBox.Show("初始化完成");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

参考;

https://chatgpt.com/share/6943c959-0564-8008-8deb-afe5523c07c1

相关推荐
·薯条大王9 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
hez201011 小时前
.NET 11 Runtime Async 详解
c#·.net·.net core
猿长大人13 小时前
C# | Serilog 新手入门
开发语言·c#·.net·log
hansang_IR13 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘13 小时前
day7.c++继承
c++
在世修行14 小时前
从零打造 C# 工业视觉检测系统(七):串口通信 SerialPort 封装与开发
开发语言·c#·modbus rtu·rs232/rs485
沫璃染墨14 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌14 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
醉城夜风~15 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
烟花落o16 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门