使用显示方式调用DLL实现浮点数的四则运算

先说下我们的调用代码:

在初始化中:

GetDlgItem(IDC_Num1)->SetWindowText("");

GetDlgItem(IDC_Num2)->SetWindowText("");
 GetDlgItem(IDC_result)->SetWindowText("0");

 

在主要实现函数中:

void CUseFloatCallDLLDlg::OnCal()
{
   typedef float (*MyCalculate)(float ,float,,int );//宏定义floatcalculate函数指针类型

  HINSTANCE hdll;  //DLL的句柄

Mycalculate calfunc;  //函数指针

  hdll.LoadLibrary("..//Debug//FloatCallDLL.dll");//LoadLibrary函数装载DLL

 //或者 hdll.LoadLibrary("F://c++项目编程//UseFloatCallDLL//Debug//FloatCallDLL.dll");

  if(hdll!=null)

{

calfunc=(Mycalculate)GetProcAddress(hdll,"floatcalculate");

    
}

else

{

AfxMessgeBox("无法加载DLL");

return;

}

Updatedata(turn);

m_floResult=calfunc(m_floNum1,m_floNum2,m_radio);//调用DLL函数进行计算

Updatedata(false);

FressLibrary(hdll);//释放DLL

}

void CUseFloatCallDLLDlg::Oncheng()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
 m_radio=2;
 UpdateData(FALSE);
}

void CUseFloatCallDLLDlg::Onchu()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
 m_radio=3;
 UpdateData(FALSE);
}

void CUseFloatCallDLLDlg::Onjia()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
 m_radio=0;
 UpdateData(FALSE);
 
}

void CUseFloatCallDLLDlg::Onjian()
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
 m_radio=1;
 UpdateData(FALSE);
}

 

在dll文件中:

在calculate.h中:

extern "C"  float _declspec(dllexport)floatcalculate(float x,float y,int type);

 

在calculate.cpp中

  #include "calculate.h"

 float floatcalculate(float x,float,y,int type)

{

switch(type)

{

case 0:

{ retutn x+y;}

case 1:

{return x-y;}

case 2;

{return x*y;}

case 3:

{return x/y;}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值