none
c function that return a struct to c# RRS feed

  • Question

  • Hello,

    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

    //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; };

    and call the function like this:

    PREDICTIONRESULT prediction;
    
    prediction = predict();

    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.

    Thank you 

    Lafi


    Friday, May 18, 2018 12:08 AM

All replies

  • Hi Read lafi,

    Thank you for posting here.

    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

    // 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;
    }

    My C# code

    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();
            }
        }
    }
    

    Hope it would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Sunday, May 20, 2018 2:55 PM
    Moderator