我们在c#和c dll之间编有以下代码编组。但是,在C dll函数内打印值时,与双数组属性相关的值全部为0.0000000。对于有问题的代码,我已经提出了一些评论。通过引用将具有双重成员数组的C#数组结构传递给C DLL
我们错过任何配置编组行为的事情吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
MonteCarlo montecarlo = new MonteCarlo();
montecarlo.RunMonteCarlo();
}
}
class MonteCarlo
{
[DllImport("MonteCarloCUDA.dll")]
public static extern int MonteCarloPrint([In, Out]PurchaseOrder[] purchaseorders, int length);
public void RunMonteCarlo()
{
PurchaseOrder[] purchaseorders = new PurchaseOrder[3];
purchaseorders[0] = new PurchaseOrder();
purchaseorders[0].Value1 = "AAAAA";
purchaseorders[0].Value2 = 0.111;
purchaseorders[0].Value3 = new double[2]; // Assign the values to array of double
purchaseorders[0].Value3[0] = 0.11111;
purchaseorders[0].Value3[1] = 0.22222;
purchaseorders[1] = new PurchaseOrder();
purchaseorders[1].Value1 = "BBB";
purchaseorders[1].Value2 = 0.222;
purchaseorders[1].Value3 = new double[2];
purchaseorders[1].Value3[0] = 0.33333;
purchaseorders[1].Value3[1] = 0.44444;
purchaseorders[2] = new PurchaseOrder();
purchaseorders[2].Value1 = "CCC";
purchaseorders[2].Value2 = 0.333;
purchaseorders[2].Value3 = new double[2];
purchaseorders[2].Value3[0] = 0.55555;
purchaseorders[2].Value3[1] = 0.66666;
int result = MonteCarloPrint(purchaseorders, purchaseorders.Length);
Console.ReadKey();
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public unsafe struct PurchaseOrder
{
public string Value1;
public double Value2;
public double[] Value3; // Array of double member
}
}
}
// C代码
#include <stdio.h>
typedef struct PurchaseOrder
{
char* Value1;
double Value2;
double* Value3;
};
__declspec(dllexport) int __stdcall MonteCarloPrint(PurchaseOrder *hostPurchaseOrders, int length)
{
printf("\nSize of PurchaseOrder: %d",sizeof(PurchaseOrder));
for (int i = 0; i < length; i++)
{
printf("\n\nAddress: %u",hostPurchaseOrders+i);
printf("\nValue1: %s",(hostPurchaseOrders+i)->Value1);
printf("\nValue2: %f",(hostPurchaseOrders+i)->Value2);
printf("\nValue3[0]: %f",(hostPurchaseOrders+i)->Value3[0]);
printf("\nValue3[1]: %f",(hostPurchaseOrders+i)->Value3[1]);
}
}}
从C DLL函数
Size of PurchaseOrder: 24 Address: 13180880 Value1: AAAAA Value2: 0.111000 Value3[0]: 0.000000 // No value are marshalled Value3[1]: 0.000000 Address: 13180904 Value1: BBB Value2: 0.222000 Value3[0]: 0.000000 Value3[1]: 0.000000 Address: 13180928 Value1: CCC Value2: 0.333000 Value3[0]: 0.000000 Value3[1]: 0.000000
斯特拉特福德,已经多次尝试改进源代码的格式。不要撤消它。混淆你的代码并不会让你得到更好的答案。 – dtb 2010-10-05 00:00:38