我的算法是用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]);
  }
  }
  }

解决方案 »

  1.   


    // 楼主,代码放在这里面,好看点儿
    // 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]);
              }
          }
      }
      

  2.   

    http://www.cnblogs.com/sopper/archive/2010/04/13/1710962.html参考: 
      

  3.   

    http://hi.baidu.com/yb223732/blog/item/948fb6eefb3d60222cf5346c.html
      

  4.   

    DllImport 只是DLL不可修改的情况下推荐使用.按你这样的C#,C++代码都可以自己修改的话 还不如写成 通信模式来交互...  (执行效率不一定就低了,因为走的是回环地址)当然,可能开发效率低了点,多了些代码.
      

  5.   

    7楼推荐的方法很不错,我没试过。 我试过的方法非常地复杂:byte[] data = arrWaveData[i];
    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
    arrWaveHdr[i].lpData = dataHandle.AddrOfPinnedObject();
    //arrWaveHdr[i].lpData 对应于c的数组,在C#声明为IntPtr
      

  6.   


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

  7.   

    可以把c++组件写成com,在.net中引用。然后定义一个实例就可以用了。