Click here to Skip to main content
15,886,732 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a dll file which is written by C++ (the name of file is "DllForTest.dll"), this is its code:

C++
#include "stdafx.h"
#include <vector>
using namespace std;

double *ret;
double* _stdcall f(int* n)
{
	vector<double> vret;
	int i=0;
	do
	{
		vret.push_back(i);
		i++;
	} while (the condition to stop this loop);
	*n=i;
	ret = new double[*n];
	for (i=0;i<*n;i++)
		ret[i]=vret[i];
	return ret;
}


This is C# code to call f function from the dll file above to get return value:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowForm
{
    public partial class Form1 : Form
    {
        [DllImport("DllForTest.dll")]
        public static extern double[] f(ref int n);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n=0;
            double[] x;
            x = f(ref n);
            MessageBox.Show("x[0]= " + x[0]);
        }
    }
}


When I run, it generate an error:

Cannot marshal 'return value': Invalid managed/unmanaged type combination.

How to fix it to gain wanted result? Thanks.
Posted
Updated 14-Aug-14 21:47pm
v2

1 solution

You can find a good advice here: http://stackoverflow.com/questions/15806393/pinvoke-issue-with-returned-array-of-doubles[^].

The idea is to change the signature of the C++ function.

—SA
 
Share this answer
 
Comments
Andrewpeter 15-Aug-14 8:23am    
Thanks --SA, it's great ! My solution is yours. Vote 5.
Sergey Alexandrovich Kryukov 15-Aug-14 10:59am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900