本文介绍了从VBA访问C ++中的嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我有一个使用C ++编写的库,并使用Visual Studio 2010将其编译为DLL。该DLL具有多个导出的功能。导出的函数使用 Declare Function 从Excel访问。



我正在尝试在程序中实现一个新功能这需要C ++部分中的嵌套结构,然后从VBA访问它们。 C ++代码看起来像这样。



第一个结构

 code> struct参数{
double value;
char * label;
char * description;
char * units;
};

第二结构



此结构用于构建另一个结构,如下所示:

  struct输出{
参数field_1;
参数field_2;
参数field_3;
};

有几种方法我正在考虑从VBA访问结构。其中一个来自这样的 void 函数。

  void Function1(Output * output_function1); 

另一个是返回输出结构,如此。

 输出Function2(); 

上述两个功能的内部结构在这一点上并不重要。我已经证实这两个实现都按照C ++代码的方式工作。



发行



我无法访问来自VBA的这两个结构使用 Function1 Function2



我在VBA中宣布了两种自定义类型。

 类型参数
Value as Double
标签为字符串
描述为字符串
单位为字符串
结束类型

类型输出
Field1为参数
Field2作为参数
Field3作为参数
结束类型

Function1 我宣布导出的函数如下。

  Declare Sub Function1 Lib C:\Path\to\library.dll(ByRef OutputStruct as Output)

并且对于 Function2 ,以下。

  Declare Sub Function2 LibC :\Path\to\library.dll()作为输出

Function2 崩溃Excel和 Function1 给我错误 vba byref参数类型不匹配



我在这里做错什么?什么是正确的方法?

解决方案

如果在中附加结构的定义,Function1将正常工作# pragma pack(4) ... #pragma pack()指令。




Problem

I have a library written in C++ and compiled as a DLL using Visual Studio 2010. The DLL has multiple exported functions. The exported functions are accessed from Excel using Declare Function.

I am trying to implement a new feature in the program which requires nested structures in the C++ portion which are then accessed from VBA. The C++ code looks like this.

First Structure

struct Parameter {
    double value;
    char* label;
    char* description;
    char* units;
};

Second Structure

This structure is used to build another structure as follows:

struct Output {
    Parameter field_1;
    Parameter field_2;
    Parameter field_3;
};

There are couple of ways that I am thinking about accessing the structure from VBA. One of them is from a void function such as this.

void Function1(Output* output_function1);

The other is a function that returns the Output structure, such as this.

Output Function2();

The internals of the two functions above do not matter at this point. I have verified that both implementations work as intended in the C++ code.

Issue

I am unable to access these two structures from VBA using either Function1 or Function2.

I declared two custom types in VBA.

Type Parameter
    Value as Double
    Label as String
    Description as String
    Units as String
End Type

Type Output
    Field1 as Parameter
    Field2 as Parameter
    Field3 as Parameter
End Type

For Function1 I declared the exported function as follows.

Declare Sub Function1 Lib "C:\Path\to\library.dll" (ByRef OutputStruct as Output)

and for Function2, the following.

Declare Sub Function2 Lib "C:\Path\to\library.dll" () as Output

Function2 crashes Excel and Function1 gives me the error vba byref argument type mismatch.

What am I doing wrong here? What is the correct approach?

解决方案

Function1 will work fine if you enclose definitions of your structures in #pragma pack(4) ... #pragma pack() directives.

https://msdn.microsoft.com/en-US/en-en/library/office/bb687915.aspx

C:

#pragma pack(4)
struct Parameter{
    double value;
    char* label;
    char* description;
    char* units;
};

struct Output{
    struct Parameter field_1;
    struct Parameter field_2;
    struct Parameter field_3;
};
#pragma pack()


static char Buffer [4096];

static void Append_Field_Values (struct Parameter* field)
{
    static char b [1024];
    sprintf (b, "%f %s %s %s\n", field -> value, field ->label, field -> description, field -> units);
    strcat (Buffer, b);
}

void _stdcall Function1(struct Output* output_function1)
{
    Buffer [0] = 0;
    Append_Field_Values (&output_function1 -> field_1);
    Append_Field_Values (&output_function1 -> field_2);
    Append_Field_Values (&output_function1 -> field_3);
    MessageBox (0, Buffer, "FUNCTION1", 0);
}

VB:

Type Parameter
    Value As Double
    Label As String
    Description As String
    Units As String
End Type

Type Output
    Field1 As Parameter
    Field2 As Parameter
    Field3 As Parameter
End Type

Declare Sub Function1 Lib "E:\Serge\StackOverflow\NestedStructures\Debug\NestedStructures.dll" (ByRef OutputStruct As Output)

Dim Out1 As Output

Sub Test1()
    Out1.Field1.Value = 3.14
    Out1.Field1.Label = "ARINC 429"
    Out1.Field2.Units = "Miles and Inches"
    Out1.Field3.Description = "Read MSDN"

    Call Function1(Out1)
End Sub

这篇关于从VBA访问C ++中的嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:38