创建VI将结构从DLL导入LabVIEW时出现问题

Posted

技术标签:

【中文标题】创建VI将结构从DLL导入LabVIEW时出现问题【英文标题】:Problem creating a VI importing a struct from DLL to LabVIEW 【发布时间】:2021-11-21 10:57:18 【问题描述】:

我想将 DLL 导入 LabVIEW 并从我的函数创建 VI,但对于具有常见数据类型的简单函数,导入效果很好。我的问题是当函数具有结构数据类型时。

我关注this tutorial导入。

我的 DLL 文件:

test.h:

#ifndef TEST_H_
#define TEST_H_

typedef struct

    int r_sum;
    int r_minus;
    int r_multiply;
    float r_divide;
CALC_t;

int sum(int a, int b);
int minus(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);
CALC_t calc_all(int a, int b);

#endif /* TEST_H_ */

test.c:

#include "test.h"

int sum(int a, int b)

    return a+b;


int minus(int a, int b)

    return a-b;


int multiply(int a, int b)

    return a*b;


float divide(int a, int b)

    return (float)a/b;


CALC_t calc_all(int a, int b)

    CALC_t result;
    result.r_sum = sum(a, b);
    result.r_minus = minus(a, b);
    result.r_multiply = multiply(a, b);
    result.r_divide = divide(a, b);
    return result;

当我导入DLL时,函数summinusmultiplydivide的VI创建成功,并且运行良好。但是函数calc_all没有被创建,LabVIEW显示警告信息:

Cannot create VI

The following VI cannot be created. This might indicate that the associated function contains
parameters of a data type that cannot be converted directly. To work around this issue, you can 
create a custom control or typedef control to represent a complex structure or multidimensional 
array, then re-run the Import Shared Library wizard and select Update VIs from a shared library.
Using the same shared library, step through the wizard again. On the Configure VIs and Controls 
page, assign the custom control or typedef to the associated parameter(s). Complex structures 
include nested structures, structures containing arrays, arrays of strings, and multidimensional 
arrays.

    dll_calc_all.vi

我尝试在配置 VI 和控件页面上进行更改,分配自定义控件、簇和其他数据类型,但没有成功。

我用cygwin32用Eclipse IDE编译库,我的LabVIEW是LabVIEW 2021 32 bits

【问题讨论】:

【参考方案1】:

LabVIEW 的导入功能难以处理非平凡的类型,并且鉴于它无法推断诸如内存分配之类的事情,因此最好避免用于除最简单的调用之外的任何调用。

这通常意味着希望将 C/C++ 库集成到 LabVIEW 中的用户必须创建自己的 VI 来执行库调用,有时还必须通过 create a wrapper in C/C++ 将库的类型转换为 LabVIEW 友好的类型。

在这种情况下,我建议修改calc_all 函数以获取可以作为结果的CALC_t 类型指针。在 LabVIEW 端,我们可以创建一个与 CALC_t 类型具有完全相同数据结构的簇,并将指向它的指针传递给库调用。

此外,对于 32 位系统,it is advisable to enforce the packing alignment 使用 #pragma pack 编译器指令。

把这一切放在一起:

test.h

#ifndef TEST_H_
#define TEST_H_

// enforce packing alignment for LabVIEW <-> C/C++ types when compiling for Windows 32-bit
// see https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019YsYSAU
#pragma pack(push) 
#pragma pack(1)

typedef struct

    int r_sum;
    int r_minus;
    int r_multiply;
    float r_divide;
CALC_t;

#pragma pack(pop)

int sum(int a, int b);
int minus(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);
void calc_all(int a, int b, CALC_t* );

#endif /* TEST_H_ */

test.c

#include "test.h"

.
.
.

void calc_all(int a, int b, CALC_t* result_ptr)

    result_ptr->r_sum = sum(a, b);
    result_ptr->r_minus = minus(a, b);
    result_ptr->r_multiply = multiply(a, b);
    result_ptr->r_divide = divide(a, b);

LabVIEW Snippet(拖放到空白VI程序框图并将调用库节点指向您的.dll

【讨论】:

以上是关于创建VI将结构从DLL导入LabVIEW时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

数据经LabVIEW写入MySQL时出现以下错误,该如何解决???????

怎么用labview实现做的界面上有几个按钮,按下就能显示并运行子VI前面板?

从 dll 导出 std::vector 时出现链接错误

labview中啥是严格类型VI,怎样创建严格类型VI

导入 cv2 (opencv) 时出现“DLL 加载失败”

Pyqt5 已安装,但在导入时出现 dll 加载失败错误?