From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

NI LabVIEW,CVI,数据采集等产品讨论区

取消
显示结果 
搜索替代 
您的意思是: 

LabVIEW调用返回结构体数组的dll时出错

已解决!
转到解答

请教各位大神一个问题,现有这样一个dll,其代码如下:

typedef struct {
double DBL;
int INT;
} TD2;

_declspec(dllexport) void CLUSTERArray(TD2 *input, TD2 *output[2])
{

output[0]->DBL = input->DBL * input->DBL;
output[1]->DBL = input->DBL * input->DBL;
output[0]->INT = input->INT * input->INT;
output[1]->INT = input->INT * input->INT;

}

当我使用LabVIEW进行调用时出错(采用了两种方式分配返回参数的内存均报错),LabVIEW调用方式如下:

QQ截图20170619134923.png

 QQ截图20170619135147.png

QQ截图20170619135141.png

 

 

0 项奖励
1 条消息(共 11 条)
6,006 次查看

如果函数原型是像下面这样写的话是可以成功调用的

typedef struct {
double DBL;
int INT;
} TD2;
_declspec(dllexport) void CLUSTERArray(TD2 *input, TD2 *output)
{

output->DBL = input->DBL * input->DBL;
output->INT = input->INT * input->INT;
}

0 项奖励
2 条消息(共 11 条)
6,003 次查看

Try:

_declspec(dllexport) void CLUSTERArray(TD2 input, TD2 *output1, TD2 *output2)

 

Use "External C" to avoid function name mangling.

 

George Zou
0 项奖励
3 条消息(共 11 条)
5,966 次查看

感谢你的回复。你说的这种方式是可以的,但是实际应用中我要返回的数组长度可能会比较长,这种方式不够灵活,我还是想尝试下返回数组。

0 项奖励
4 条消息(共 11 条)
5,958 次查看

_declspec(dllexport) void CLUSTERArray(TD2 *input, TD2 *output) 就行了.  不需要[2]

 

George Zou
0 项奖励
5 条消息(共 11 条)
5,950 次查看

_declspec(dllexport) void CLUSTERArray(TD2 *input, TD2 *output[2])/*如果这里的[2]不要了,那我下面的函数主体里该怎么写才能体现output是个数组呢*/

{ output[0]->DBL = input->DBL * input->DBL;

  output[1]->DBL = input->DBL * input->DBL;

  output[0]->INT = input->INT * input->INT;

  output[1]->INT = input->INT * input->INT;

}

0 项奖励
6 条消息(共 11 条)
5,949 次查看
解答
已被主题作者 allen.li 接受

TD2 *output is a pointer, pointing to a TD2 type data.  Same for one or array.

You can use TD2 *output, or use TD2 output[2].

 

 

George Zou
0 项奖励
7 条消息(共 11 条)
5,931 次查看

函数改为下面这种方式后,调用Dll不在报内存错误,但是返回的output数组只有第一个元素是对的,如下图所示。

_declspec(dllexport) void CLUSTERArray(TD2 *input, TD2 output[2])
{

output[0].DBL = input->DBL * input->DBL;
output[1].DBL = input->DBL * input->DBL;

output[0].INT = input->INT * input->INT;
output[1].INT = input->INT * input->INT;

}

QQ截图20170621100319.png

0 项奖励
8 条消息(共 11 条)
5,925 次查看

The 2nd parameter is an array, not cluster!

 

 

George Zou
0 项奖励
9 条消息(共 11 条)
5,918 次查看

请问你这个最后是怎么解决的!我这也有一个调整DLL结构体,想请你指导指导!

0 项奖励
10 条消息(共 11 条)
3,948 次查看