函数指针:DLL中如何应用函数指针与主程序交互



虽然在DLL中定义指向主指针看似逻辑有些混乱但工程实际应用中有时却得这么干因此简单整理总结指针在开发中应用
步: 创建动态库工程Win32s1

第 2步: 在动态库.h文件中自定义指针类型声明导出
注:导出应用到外部主相关计算和结果
typedef float (*outFun)(, , ); //指针类型
// typedef + 类型标识 + (指针变量名称) + (参数列表)
SBUI_API void getProfit(outFun fun); //声明导出
注:导出返回类型和指针应用无关

第 3步: 在动态库.cpp文件中实现个由外部执行 + 动态库执行个动作
void getProfit(outFun fun)
{
nGrossIncome = 90000; //总收入
nLabourCosts = 1000; //人工成本
nMaterialCosts = 2000; //材料成本
double nNetProfit = fun(
nGrossIncome,
nLabourCosts,
nMaterialCosts)/*求得税前利润*/
* (1 - 0.18);
char chNetProfit[512] = "\0";
sprf(chNetProfit, "当期纯利润为 %f", nNetProfit); // # <stdio.h>
MessageBox(NULL, chNetProfit, "收益计算", MB_OK|MB_ICONINFORMATION);
}

第 4步: 在主中实现成本计算 ProfitDLL
float Profit( _nGrossIncome, _nLabourCosts, _nMaterialCosts)
{
nSellingCosts = 5000;
float(_nGrossIncome - nSellingCosts - _nLabourCosts - _nMaterialCosts);
}
# "dll1.h"
#pragma comment(lib,"Win32s1.lib")
void CTestDlg::OnButton1
{
getProfit(Profit);
}
Tags:  函数指针类型 函数指针数组 指向函数的指针 函数指针

延伸阅读

最新评论

发表评论