我的算法是用C++写的,C#把整型数组a,和i值(i为数组元素的下标,比如i为100,就取数组前100个元素)传给C++,然后C++通过计算的到一个double类型数组b,我再把这个数组b和它的长度传给传给C#,请问大家这个接口如何写,在大家的帮助下我写了一个,但提示错误“无效的托管/非托管类型组合。”在 b= HelloPrint(a,size );就出错。感谢大家了,我的分数不多了,不是多给点下面是我的程序。我的QQ是346564494,大家帮帮忙哦
C++里面的程序test.cpp的程序如下
#include <stdio.h>
#include <vector>
extern "C"
{
double __stdcall HelloPrint(int*a,int size)
{
double* c = (double*)malloc(size * sizeof(double));for (int i = 0; i < size; i++)
c[i] =a[size-i-1]*1.0;
return *c;
}
}test.def 代码LIBRARY "test"EXPORTS
HelloPrint @1
C#里面的代码Program.cs代码如下using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace calltest
{
class Program
{
[DllImport("test.dll", EntryPoint = "HelloPrint")]
public static extern double[] HelloPrint(int[] a, int size);
static void Main(string[] args)
{
int size = 10;
int[] a;
a = new int[size];
double[] b;
b = new double[size];
// double* b = (double*)malloc(size * sizeof(double));
for (int i = 0; i < size; i++)
a[i] = i;
b= HelloPrint(a,size );
for (int j = 0; j < size; j++)
{
Console.Write(b[j]);
}
}
}
C++里面的程序test.cpp的程序如下
#include <stdio.h>
#include <vector>
extern "C"
{
double __stdcall HelloPrint(int*a,int size)
{
double* c = (double*)malloc(size * sizeof(double));for (int i = 0; i < size; i++)
c[i] =a[size-i-1]*1.0;
return *c;
}
}test.def 代码LIBRARY "test"EXPORTS
HelloPrint @1
C#里面的代码Program.cs代码如下using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace calltest
{
class Program
{
[DllImport("test.dll", EntryPoint = "HelloPrint")]
public static extern double[] HelloPrint(int[] a, int size);
static void Main(string[] args)
{
int size = 10;
int[] a;
a = new int[size];
double[] b;
b = new double[size];
// double* b = (double*)malloc(size * sizeof(double));
for (int i = 0; i < size; i++)
a[i] = i;
b= HelloPrint(a,size );
for (int j = 0; j < size; j++)
{
Console.Write(b[j]);
}
}
}
// 楼主,代码放在这里面,好看点儿
// test.cpp的程序如下
#include <stdio.h>
#include <vector>
extern "C"
{
double __stdcall HelloPrint(int*a,int size)
{
double* c = (double*)malloc(size * sizeof(double)); for (int i = 0; i < size; i++)
c[i] =a[size-i-1]*1.0;
return *c;
}
}// test.def 代码LIBRARY "test"EXPORTS
HelloPrint @1
// C#里面的代码Program.cs代码如下using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace calltest
{
class Program
{
[DllImport("test.dll", EntryPoint = "HelloPrint")]
public static extern double[] HelloPrint(int[] a, int size);
static void Main(string[] args)
{
int size = 10;
int[] a;
a = new int[size];
double[] b;
b = new double[size];
// double* b = (double*)malloc(size * sizeof(double));
for (int i = 0; i < size; i++)
a[i] = i;
b= HelloPrint(a,size );
for (int j = 0; j < size; j++)
{
Console.Write(b[j]);
}
}
}
GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
arrWaveHdr[i].lpData = dataHandle.AddrOfPinnedObject();
//arrWaveHdr[i].lpData 对应于c的数组,在C#声明为IntPtr
extern "C" __declspec(dllexport) void HelloPrint(int* a, double* c, int size)
{
//double* c = (double*)malloc(size * sizeof(double)); for (int i = 0; i < size; i++)
c[i] =a[size-i-1]*1.0;
//return c;
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace calltest
{
class Program
{
[DllImport("test.dll", EntryPoint = "HelloPrint")]
public static extern void HelloPrint(int[] a, double[] c, int size);
static void Main(string[] args)
{
int size = 10;
int[] a;
a = new int[size];
double[] b;
b = new double[size]; // double* b = (double*)malloc(size * sizeof(double)); for (int i = 0; i < size; i++)
a[i] = i;
HelloPrint(a, b, size); for (int j = 0; j < size; j++)
{
Console.Write(b[j]);
}
}
}
}