C#获取C++中修改过的float数组(指针),dll

C++中

struct rankPoint{
float sim;
}; extern "C" __declspec(dllexport) int getRank(rankPoint prank[]){
for(int i = ; i < ; i++)
prank[i].sim = ; for(int i = ; i < ; i++)
prank[i].sim = i;
return ;
}

C#中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices; namespace DllTest
{
[StructLayout(LayoutKind.Sequential)]
struct rankP
{
public float mark;
}
public class Cpp {
[DllImport("Cdll.dll")]
public static extern int getRank(IntPtr p);
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int size = Marshal.SizeOf(typeof(rankP)) * ;
byte[] bytes = new byte[size];
IntPtr pBuff = Marshal.AllocHGlobal(size);
rankP[] rBuf = new rankP[];
Cpp.getRank(pBuff);
for(int i = ; i < ; i++)
{
IntPtr pPointor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(rankP)) * i);
rBuf[i] = (rankP)Marshal.PtrToStructure(pPointor,typeof(rankP));
}
int si = ; //设置断点监测,的确修改了rBuf数组,也就是实现了数组的传递,只不过是通过结构体实现的
Marshal.FreeHGlobal(pBuff);
}
} }

转一篇简单易懂的C++编写dll的文档http://wenku.baidu.com/view/ebb7e1edb8f67c1cfad6b867.html

上一篇:xfce4 dev tools的一些说明


下一篇:caffe实现年龄及性别预测