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

相关推荐
独自归家的兔2 小时前
通义千问3-VL-Plus - 界面交互(坐标改进)
数据库·microsoft·交互
应用市场2 小时前
TCP网络连接断开检测机制详解——C++实现网络连通性判断与断线类型识别
网络·c++·tcp/ip
浅尝辄止;2 小时前
C# 优雅实现 HttpClient 封装(可直接复用的工具类)
开发语言·c#
雾岛听蓝2 小时前
C/C++内存管理
c语言·c++
AuroraWanderll2 小时前
类和对象(三)-默认成员函数详解与运算符重载
c语言·开发语言·数据结构·c++·算法
Minecraft红客2 小时前
C++制作迷宫第一版
c++·游戏·电脑·娱乐
雪域迷影2 小时前
Windows11中VS2026使用C++ 现代化json库nlohmann的3种方式
开发语言·c++·json
羑悻的小杀马特2 小时前
LRU Cache:高频访问数据的“智能保鲜舱”与经典淘汰艺术
c++·后端·lru cache·热点数据与冷数据
zephyr052 小时前
C++ STL string 用法详解与示例
开发语言·c++