Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C+发送回调+/CLI到C#使用类还是结构? 我想从C++向C语言发送对象类或结构类回调, 我成功发送了一个int或string,但未能发送回类。请帮忙 参考:_C#_C++_C++ Cli - Fatal编程技术网

从C+发送回调+/CLI到C#使用类还是结构? 我想从C++向C语言发送对象类或结构类回调, 我成功发送了一个int或string,但未能发送回类。请帮忙 参考:

从C+发送回调+/CLI到C#使用类还是结构? 我想从C++向C语言发送对象类或结构类回调, 我成功发送了一个int或string,但未能发送回类。请帮忙 参考:,c#,c++,c++-cli,C#,C++,C++ Cli,代码: C++π.h<P/> C++pi.cpp #include "pch.h" #include "pi.h" // pi.cpp: #include <cmath> double Cpp::PI::compute(Cpp::ComputeCallback callback) { //System::String^ myString2 = "jumped over the lazy dog."; Box Box1; Box1.height = 5.0

代码: C++π.h<P/> C++pi.cpp

#include "pch.h"
#include "pi.h"
// pi.cpp: 
#include <cmath>

double Cpp::PI::compute(Cpp::ComputeCallback callback) {
    //System::String^ myString2 = "jumped over the lazy dog.";
    Box Box1; 
    Box1.height = 5.0;
    Box1.length = 6.0;
    Box1.breadth = 7.0;

    callback(Box1);
    return 0;
}
#包括“pch.h”
#包括“pi.h”
//pi.cpp:
#包括
双Cpp::PI::compute(Cpp::ComputeCallback){
//系统::String ^myString2=“跳过了懒狗。”;
盒子1;
框1.1高度=5.0;
Box1.length=6.0;
Box1.0宽度=7.0;
回调(框1);
返回0;
}
C++桥文件piCLI.cpp

#pragma once
#include "pi.h"

using namespace System;

//bridge file
namespace ClassLibrary2 {
    public delegate void ComputeDelegate(Cpp::Box);

    public ref class PI abstract sealed {
    public:
        static double compute(ComputeDelegate^ callback) {
            using System::IntPtr;
            using System::Runtime::InteropServices::Marshal;

            IntPtr cbPtr = Marshal::GetFunctionPointerForDelegate(callback);
            return Cpp::PI::compute(
                static_cast<Cpp::ComputeCallback>(cbPtr.ToPointer())
            );
        }
    };
}
#pragma一次
#包括“pi.h”
使用名称空间系统;
//桥文件
命名空间类库2{
公共委托无效计算elegate(Cpp::Box);
公共引用类PI摘要密封{
公众:
静态双重计算(ComputeDeleteGate^回调){
使用System::IntPtr;
使用System::Runtime::InteropServices::封送;
IntPtr cbPtr=Marshal::GetFunctionPointerForDelegate(回调);
返回Cpp::PI::compute(
静态_cast(cbPtr.ToPointer())
);
}
};
}
C#

使用ClassLibrary2;
命名空间回调WPF
{  
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
Trace.WriteLine(“开始”);
ClassLibrary2.PI.compute(
Box1=>Trace.WriteLine(Box1));
}
}
}
错误:

System.TypeAccessException:'按方法尝试 “CallbackWpf.MainWindow+c.b_uuu0_u0(Cpp.Box)”访问类型 “Cpp.Box”失败


WPF中的代码> BOX1 <代码>来自哪里?Box 1是C++的,C++代码打包为DLL,C加载文件,不能从C语言访问本机类型。您可以创建一个C++托管类,它封装了本机类型(复制所有的属性等),并返回它。谢谢!你能给我一个学习的例子吗;在C++/CLI项目中需要,以使数据可供C#程序访问。回调必须是PI的成员函数,以便它可以将Box成员复制到ManagedBox,然后调用委托。compute()不应是静态的。
#pragma once
#include "pi.h"

using namespace System;

//bridge file
namespace ClassLibrary2 {
    public delegate void ComputeDelegate(Cpp::Box);

    public ref class PI abstract sealed {
    public:
        static double compute(ComputeDelegate^ callback) {
            using System::IntPtr;
            using System::Runtime::InteropServices::Marshal;

            IntPtr cbPtr = Marshal::GetFunctionPointerForDelegate(callback);
            return Cpp::PI::compute(
                static_cast<Cpp::ComputeCallback>(cbPtr.ToPointer())
            );
        }
    };
}
using ClassLibrary2;

namespace CallbackWpf
{  
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Trace.WriteLine("______ start");

            ClassLibrary2.PI.compute(
                Box1 => Trace.WriteLine(Box1));
        }
    }
}