【发布时间】:2014-08-15 07:43:42
【问题描述】:
我有一个由 C++ 编写的 dll 文件(文件名是“DllForTest.dll”),这是它的代码:
#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;
}
这是从上面的 dll 文件中调用 f 函数以获取返回值的 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]);
}
}
}
当我运行时,它会产生错误:
无法封送“返回值”:托管/非托管类型组合无效。
如何解决它以获得想要的结果?谢谢。
【问题讨论】: