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

// 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调用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloworddm

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值