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

相关推荐
‎ദ്ദിᵔ.˛.ᵔ₎1 小时前
c++11:fuction包装器
开发语言·c++
淡海水2 小时前
02-07-原理篇-GC暂停时间的本质
开发语言·unity·c#·gc·暂停时间
霍霍的袁2 小时前
【C++】哈希表实现指南:从原理到开放定址法、链地址法的实现
c++·学习·哈希算法·散列表·visual studio
WL学习笔记3 小时前
类和对象(下)
c++·类和对象
小羊没烦恼!3 小时前
OS X 上使用.NET开发应用程序
开发语言·前端·数据库·人工智能·c#·策略模式
傲世仙尊3 小时前
拷贝构造-传值传参的连锁反应与隐式函数的三个局限
c++
垆边人似月.4 小时前
华为机试题 :最大数
c++·算法·华为
方便面不加香菜4 小时前
C++ 关于多态的知识点讲解
开发语言·c++
jimy15 小时前
C++ 类成员函数的内联定义(inline)详解
开发语言·c++
Behavior5 小时前
Unity 7.0 Alpha 拆解:编辑器干掉 Mono 换 CoreCLR
c#·unity3d·游戏开发