创建dll文件并利用labview实现调用

一、创建dll(dynamic link library动态链接库)
1、利用c++的编译软件vs2017创建dll文件,选择动态链接库
在这里插入图片描述

2、添加.h文件(头文件),头文件代码如下:

#pragma once
#ifndef mydll_H_
#define mydll_H_
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport) 
#else
#define MYLIBDLL extern "C" _declspec(dllexport) 
#endif
MYLIBDLL int status(double x, int y);
#endif

3、添加.cpp(源文件),代码如下:

   	#include "stdafx.h"
	#include "mydll.h"
	#include <iostream>
	using namespace std;
	int status(double x, int y)
	{
		 if (x >= 1.0&&x < 1.1)
		  y = 1;
		 else if (x >= 1.1&&x < 1.2)
		  y = 2;
 		else if (x >= 1.2&&x < 1.3)
  		y = 3;
		 else if (x >= 1.3&&x < 1.4)
 		 y = 4;
		 else if (x >= 1.4&&x < 1.5)
		  y = 5;
		 else
  		y = 6;
		 return y;
}

4、添加.def文件(模块定义文件),代码如下:

LIBRARY "MyDLL"
EXPORTS
status @1

5、生成dll文件,此时在debug文件夹中能够找到.dll和.lib文件
在这里插入图片描述
在这里插入图片描述
二、利用labview调用库函数节点,实现.dll文件的调用
1、拖入控件调用库函数节点
在这里插入图片描述
2、设置调用路径、函数名和参数,如果使用return返回类型,则设置返回类型,注意函数中各个变量的类型和设置参数的类型要一致,不然可能会出现问题
在这里插入图片描述
3、输入变量即可得到结果,在本程序中,输入变量x即可得到x所属的范围
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/li_little7/article/details/83754863