CVI显性调用VC生成地DLL.docx
《CVI显性调用VC生成地DLL.docx》由会员分享,可在线阅读,更多相关《CVI显性调用VC生成地DLL.docx(10页珍藏版)》请在冰豆网上搜索。
CVI显性调用VC生成地DLL
CVI显性调用VC生成的DLL的详细步骤与须知事项
2017/4/16Createby狄阁老
Ifyouhaveanyquestion,pleasemailto:
mlfannuaa163.
关键字
最近在忙于用Labwindows/CVI实现某种功能,需要用利用VC++6.0环境开发的dll,然后供Labwindows/CVI调用。
详细步骤如下:
步骤1,打开VC++6.0环境,文件->新建->选择Win32Dynamic-LinkLibrary,输入工程名,例如test04,见图1;
图1,步骤1
步骤2,点击确定,选择一个“一个可以导出某些符号的DLL工程〞,如图2,点击完成,出现一个确定界面,点击界面上确实定按钮,
图2步骤2
步骤3,去除test04.cpp中的默认代码〔红色〕,再在test04.h中去除默认的定义〔绿色〕
//Thisisanexampleofanexportedvariable
TEST04_APIintnTest04=0;
//Thisisanexampleofanexportedfunction.
TEST04_APIintfnTest04(void)
{
return42;
}
//Thisistheconstructorofaclassthathasbeenexported.
//seetest04.hfortheclassdefinition
CTest04:
:
CTest04()
{
return;
}
classTEST04_APICTest04{
public:
CTest04(void);
//TODO:
addyourmethodshere.
};
externTEST04_APIintnTest04;
TEST04_APIintfnTest04(void);
步骤4,在test04.cpp中添加自己的函数,例如:
TEST04_APIintadd(intx,inty)
{
returnx+y;
}
TEST04_APIintmaxvalue(intx,inty)
{
if(x>y)
returnx;
else
returny;
}
TEST04_APIfloatsub(floatx,floaty)
{
returnx-y;
}
TEST04_APIvoidhello(void)
{
MessageBox(0,"LabWindowsCVI调用dll成功\n","欢迎",MB_ICONINFORMATION);
}
TEST04_APIchar*returnstr(constchar*s1,constchar*s2)
{
intm1,m2;//两字符串长度
m1=strlen(s1);m2=strlen(s2);//求长度
char*s=(char*)malloc(sizeof(char)*(m1+m2+1));//申请内存空间,多1字节
strcpy(s,s1);//复制第一个字符串
strcpy(s+m1,s2);//复制第二个字符串
returns;//返回结果
}
TEST04_APIintadd(intx,inty);
TEST04_APIintmaxvalue(intx,inty);
TEST04_APIfloatsub(floatx,floaty);
TEST04_APIvoidhello(void);
TEST04_APIchar*returnstr(constchar*s1,constchar*s2);
在工程路径下新建一个.txt文本,在里面添加如下内容〔红色〕,见图3;
LIBRARY"testmydll"//引号内的随便写
EXPORTS
add
maxvalue
sub
hello
returnstr
图3步骤6
步骤7,将.def添加到工程中,见图4;
图4步骤7
步骤8,然后在工程下debug下找到test04.dll拷贝到CVI工程下,
CVI工程代码如下:
//==============================================================================
//
//Title:
testDLL
//Purpose:
Ashortdescriptionoftheapplication.
//
//Createdon:
2017/4/15at20:
20:
28bydigelao.
//Copyright:
.AllRightsReserved.
//
//==============================================================================
//==============================================================================
//Includefiles
#include
#include
#include
#include
#include"testDLL.h"
#include"toolbox.h"
//==============================================================================
//Constants
//==============================================================================
//Types
//==============================================================================
//Staticglobalvariables
staticintpanelHandle;
HINSTANCEhDll;//句柄
typedefint(*myfuction)(int,int);
myfuctionmf=NULL;
typedefvoid(*hello)(void);
hellohl=NULL;
typedefchar*(*returnstr)(constchar*,constchar*);
returnstrrs=NULL;
typedefint(*maxvalue)(int,int);
maxvaluemx=NULL;
typedeffloat(*sub)(float,float);
subsb=NULL;
//==============================================================================
//Staticfunctions
//==============================================================================
//Globalvariables
//==============================================================================
//Globalfunctions
///HIFNThemainentry-pointfunction.
intmain(intargc,char*argv[])
{
interror=0;
/*initializeandloadresources*/
nullChk(InitCVIRTE(0,argv,0));
errChk(panelHandle=LoadPanel(0,"testDLL.uir",PANEL));
//以下至/*displaythepanelandruntheuserinterface*/之间是加载dll中的函数
hDll=LoadLibrary("test04.dll");//动态加载DLL模块句柄
mf=(myfuction)GetProcAddress(hDll,"add");//得到所加载DLL模块中函数的地址
if(mf==NULL)
{
MessagePopup("","调用函数add失败!
");
}
hl=(hello)GetProcAddress(hDll,"hello");//得到所加载DLL模块中函数的地址
if(hl==NULL)
{
MessagePopup("","调用函数hello失败!
");
}
rs=(returnstr)GetProcAddress(hDll,"returnstr");//得到所加载DLL模块中函数的地址
if(rs==NULL)
{
MessagePopup("","调用函数returnstr失败!
");
}
mx=(maxvalue)GetProcAddress(hDll,"maxvalue");//得到所加载DLL模块中函数的地址
if(mx==NULL)
{
MessagePopup("","调用函数maxvalue失败!
");
}
sb=(sub)GetProcAddress(hDll,"sub");//得到所加载DLL模块中函数的地址
if(mx==NULL)
{
MessagePopup("","调用函数sub失败!
");
}
/*displaythepanelandruntheuserinterface*/
errChk(DisplayPanel(panelHandle));
errChk(RunUserInterface());
Error:
/*cleanup*/
DiscardPanel(panelHandle);
return0;
}
//==============================================================================
//UIcallbackfunctionprototypes
///HIFNExitwhentheuserdismissesthepanel.
intCVICALLBACKpanelCB(intpanel,intevent,void*callbackData,
inteventData1,inteventData2)
{
if(event==EVENT_CLOSE)
QuitUserInterface(0);
return0;
}
intCVICALLBACKOkCallback(intpanel,intcontrol,intevent,
void*callbackData,inteventData1,inte