通过引用将带有double成员数组的C#数组结构传递给C DLL

时间:2010-10-04 23:40:06

标签: c# c marshalling

我们在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

1 个答案:

答案 0 :(得分:1)

从[DllImport]声明中删除Pack属性,这是错误的。您没有在C代码中使用#pragma pack指令,Pack的默认值是合适的。如果它已经生效,那么你的C代码将报告16。

你看到24因为有4个字节的填充来对齐结构末尾的双精度和4个字节的填充,以便在数组中使用结构时进行双重对齐。 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 ++编译器编写代码,您可能需要调整。