从C ++ / CLI调用c#类

时间:2016-12-18 01:23:31

标签: c++-cli

我想用c ++ / cli导出我的c#类:

这是我的标题(.h)文件和我的.cpp

using namespace System;
using namespace Microsoft::Kinect;
using namespace std;
using namespace System::Runtime::InteropServices;

class clrPrivate;

class __declspec(dllexport) clr
{
private: clrPrivate* _private;



public: clr();
public: ~clr();


public: String^ calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[]);
};

和我的.cpp文件

// This is the main DLL file.
#using "FULLPATH\MYDLL.dll"
#include <msclr\auto_gcroot.h>
#include "ClassLibrary4.h"
#include <array>
//#include "stdafx.h"


using namespace Distance_Test;
using namespace std;
using namespace System::Runtime::InteropServices; 
using namespace Microsoft::Kinect;
using namespace System;

class clrPrivate
{
public: msclr::auto_gcroot<MINIMUM_DISTANCE^> out;
};


   clrPrivate* _private;

         clr::clr()
        {
            _private = new clrPrivate();
            _private->out = gcnew MINIMUM_DISTANCE;
        };

        String^ clr::calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[])
        {


            String^ output;
        return  output = _private->out->calculate(*Robot_Points_Values, *human_point_cloud, **bodyindexdata);
            //return  output = _private->out->value_return;
        };

  clr::~clr()
{
    delete _private;
};

当我运行调试模式时,它显示2个错误:

1)错误C3395'clr :: calculate':__ declspec(dllexport)不能应用于具有__clrcall调用约定的函数

2)'。value_return'左边的错误C2228必须有class / struct / union

我正在尝试2天寻找解决方案,但我已经陷入困境。如果有人能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

托管类不需要__declspec(dllexport),因为它们会自动导出,除非您将它们声明为private / internal。只需更改类签名:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class LayoutTest extends JPanel {
    private static final int CELL_WIDTH = 100;
    private static final int SIDES = 5;
    private static final int GAP = 25;
    private static final Color BG = Color.WHITE;
    private LayoutPanel layoutPanel = new LayoutPanel(CELL_WIDTH, SIDES);

    public LayoutTest() {
        setBackground(BG);
        JPanel btnPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
        btnPanel.add(new JButton("Button 1"));
        btnPanel.add(new JButton("Button 2"));
        btnPanel.setBackground(BG);

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new BorderLayout(GAP, GAP));
        add(layoutPanel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_START);
    }

    private static void createAndShowGui() {
        LayoutTest mainPanel = new LayoutTest();

        JFrame frame = new JFrame("Layout Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

@SuppressWarnings("serial")
class LayoutPanel extends JPanel {
    private static final Color BG = Color.BLACK;
    private int sides = 5;
    private static final int INNER_GAP = 1;
    private static final Color CELL_BG = Color.WHITE;

    public LayoutPanel(int cellWidth, int sides) {
        setBackground(BG);
        setBorder(BorderFactory.createLineBorder(BG));
        this.sides = sides;
        setLayout(new GridLayout(sides, sides, INNER_GAP, INNER_GAP));
        Dimension cellDim = new Dimension(cellWidth, cellWidth);
        for (int i = 0; i < sides * sides; i++) {
            JLabel label = new JLabel(".", SwingConstants.CENTER);
            label.setPreferredSize(cellDim);
            label.setBackground(CELL_BG);
            label.setOpaque(true);
            add(label);
        }
    }
}

另外,如果你想在C#中使用它,你应该在calculate方法的签名中使用托管数组:

public ref class clr