c函数将结构返回给c# [英] c function that return a struct to c#

查看:66
本文介绍了c函数将结构返回给c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个返回结构的交流函数,我已经构建了一个.dll,我想从c#中调用它,如下所示:

i have a c function that return a structure, i have build a .dll and i want to call it from c# like this:

typedef struct predtionresult { float prediction[5]; char *name[5]; }PREDICTIONRESULT; PREDICTIONRESULT predict() { PREDICTIONRESULT predres ; for(i = 0; i < 5; ++i){ predres.name[i]= names[i]; //names[i] are not empty predres.prediction[i] = predictions[i] // prediction are not empty there is

//代码的某些部分,我不能分享
}

返回预测;
}

i已经声明了这样的c#结构:


[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi,Pack = 1) ]
public struct PREDICTIONRESULT {

[MarshalAs(UnmanagedType.ByValArray,SizeConst = 5)]
public float [] prediction;

[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 5)]
public string [] name;
};

//some part of code that i can't share } return predres; } i have declared the c# structure like this: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct PREDICTIONRESULT{ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] public float[] prediction; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)] public string[] name; };

并调用这样的函数:

PREDICTIONRESULT prediction;

prediction = predict();

问题是预测数组和名称数组是空的,我没有得到返回的值c函数,所以我认为我的c#结构有一个我没有得到的问题。

The problem is the prediction array and the name array are empty, i didn't get the returned value from the c function, so i think that my c# structure have a problem that i didn't get.

谢谢 

Lafi




推荐答案

嗨读lafi,

Hi Read lafi,

感谢您在此发帖。

我想根据您的C ++代码制作它。但我不擅长C ++。您的C ++代码中存在一些编译器错误。因此,我举了一个简单的例子供你参考。

I want to make it according to your C++ code. But I am not good at C++. There are some complier errors in your C++ code. Hence I make a simple example for your reference.

My C ++ dll

My C++ dll

// MyDll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

#define EXPORT_DLL extern "C" __declspec(dllexport) 

EXPORT_DLL int Add(int a, int b)
{
	return a + b;
}
struct Bar
{
public:
	int id;
};

EXPORT_DLL void GetBar(Bar& bar)
{
	//Bar b;
	bar.id = 10;
}

我的C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct Bar
    {
        /// int
        public int id;
    }
    class CSharp_call_C__
    {
        [DllImport(@"D:\Visual Studio 2017\Projects\ConsoleApp\Debug\MyDll.dll", EntryPoint = "Add", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        extern static int Add(int a, int b);

        [DllImport(@"D:\Visual Studio 2017\Projects\ConsoleApp\Debug\MyDll.dll", EntryPoint = "GetBar", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        extern static void GetBar(ref Bar bar);

        static void Main(string[] args)
        {
            int A = Add(1, 2);
            Console.WriteLine(Add(5, 4));

            Bar bar = new Bar();
            GetBar(ref bar);
            Console.WriteLine("BAR id is " + bar.id);

            Console.ReadKey();
        }
    }
}

希望它会有所帮助。

最诚挚的问候,

Wendy


这篇关于c函数将结构返回给c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆