先说下我们的调用代码:
在初始化中:
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;}
}
}