我们在 c# 和 c dll 之间编码了以下代码。但是,当在 C dll 函数中打印值时,与 double 组属性关联的值都是 0.0000000。我已经为有问题的代码添加了一些行注释。
我们是否错过了配置编码行为的任何内容?
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
最佳答案
从[DllImport]声明中删除Pack属性,这是错误的。您没有在 C 代码中使用 #pragma pack 指令,Pack 的默认值是合适的。如果它有效,那么您的 C 代码将报告 16。
您看到的是 24,因为有 4 个字节的填充用于对齐 double 型,并且结构末尾有 4 个字节的填充,以便在数组中使用该结构时进行 double 对齐。 4 + 4 + 8 + 4 + 4 = 24。有效的打包为 8,默认值。
您可以通过交换 Value2 和 Value3 来获得 16 字节的结构,从而提高效率,无需填充。万一重要的话。这就是 JIT 编译器的作用。
<小时/>下一个问题更棘手,P/Invoke 编码器会将嵌入式数组编码为 SAFEARRAY。您可以使用以下代码在非托管端解决该问题:
#include "stdafx.h"
#include <stdio.h>
#include <objidl.h>
struct PurchaseOrder
{
char* Value1;
double Value2;
LPSAFEARRAY Value3;
int fence;
};
extern "C"
__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);
double* arrayPtr;
if (S_OK == SafeArrayAccessData((hostPurchaseOrders+i)->Value3, (void**)&arrayPtr)) {
printf("\nValue3[0]: %f", arrayPtr[0]);
printf("\nValue3[1]: %f", arrayPtr[1]);
}
}
return 0;
}
我使用 C++ 编译器编写了代码,您可能需要调整。
关于c# - 通过引用将具有双成员数组的 C# 结构数组传递给 C DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3859946/