如何将包含结构数组的结构从vba传递给c-dll

时间:2017-03-20 20:18:26

标签: c vba dll

我试图通过vba将一个包含多个嵌套结构的相当大的结构传递给c-dll。

只要没有嵌套结构,我就可以做到这一点。但是,将其他结构的数组引入主结构中是行不通的。我使用visual studio 2015,我使用“附加到进程”来查看dll内部的内容。 WL400PoliseRiskInn(主结构)中的前五个变量传递得很好,但在数组“dekninger”中有些东西变坏了。

enter image description here

包括“antallDekninger”在内的所有内容都是正确的,但只要我们查看嵌套类型,事情就不应该如此。变量“alder”具有应包含在“riskOpphoerAlder”中的值,其他所有内容似乎都不同步。 “骑手”应为1,“crtable”应为“DS30”。一旦我们进入阵列,我假设有某种偏移。

我已经检查并仔细检查过我在vba中的类型定义是否与我的c-structs匹配,据我所知,他们确实如此。我在这里错过了什么吗?我是否必须在VBA中的数组声明中做一些神奇的事情。我真的不想篡改我的c声明,因为它们在c-dll中使用,我不想写另一个c-struct并从那个复制(重用和所有......)

我的vba定义是这样的

Type WL400Lext
    lextno As Long
    prosentTillegg As Long
    promilleTillegg As Long
    risikosum As Long
    datoOpphoer As String * 11
End Type

Type WL400DekningRiskInn
    rider As Long
    crtable As String * 5
    kjoenn As Byte
    alder As Long
    riskOpphoerAlder As Long
    sumins As Long
    royker As Byte
    karens As Long
    yrkesfaktor As Double
    antallLext As Long
    L400Lext(0 To 4) As WL400Lext
End Type

Type WL400PoliseRiskInn
    chdrnum As Long
    cnttype As String * 4
    datoBeregning As String * 11
    datoTegning As String * 11
    antallDekninger As Long
    dekninger(0 To 12) As WL400DekningRiskInn
    wrapstatus As Long
 End Type

在接收结束时,我有相应的c-types

 typedef struct
 {
     int lextNo;
     int prosentTillegg;
     int promilleTillegg;
     int risikosum;
     char datoOpphoer[11];
 } WL400Lext;

 typedef struct
 {
     int rider;
     char crtable[5];
     char kjoenn;
     int alder;
     int riskOpphoerAlder
     int sumins;
     char royker;
     int karens;
     double yrkesfaktor;
     int iAntallLext;
     WL400Lext l400Lext[5];
} WL400DekningRiskInn;

typedef struct
{
    int chdrnum;
    char cnttype[4];
    char datoBeregning[11];
    char datoTegning[11];
    int antallDekninger;
    WL400DekningRiskInn dekninger[13];
    int wrapstatus;
} WL400PoliseRiskInn;

vba中的调用代码如下所示

Public Declare Function dllBerL400PoliseRisk Lib "WinL400DLL" (polise As WL400PoliseRiskInn) As Long

Function berPoliseRisk(rngPolise As Range, rngDekninger As Range, rngLext As Range) As Long

    //lots of stuff here to fill the struct

    //Call the dll by reference   
    dllBerL400PoliseRisk polise

    //Return dummy for now whilst sorting things out :|
    berPoliseRisk = 3

End Function

我分配给像这样的单字节值

Dim kjoenn as String
dekning.kjoenn = Asc(kjoenn)

字符串就像这样

Dim crtable as String
dekning.crtable = crtable & Chr(0)

crtable的长度为4(Chr(0)总共为5)

c中的相应代码是

__declspec(dllexport) int _stdcall dllBerL400PoliseRisk(WL400PoliseRiskInn *wL400PoliseRiskInn)
{
    //Do stuff
}

1 个答案:

答案 0 :(得分:0)

一位同事和我看了一下,事实证明这已经在这个帖子Accessing a nested structure in C++ from VBA中得到了解答。阿尔古特的答案解决了它!

那就是......诀窍是介绍

#pragma pack(4)

//All the c declarations

#pragma pack()

在头文件中声明c-structs