转载请注明作者:小刘
DLL编程实例
一. 未进行Dll封装的信号平坦区检测函数。
#include<iostream>
#include<vector>
using namespace std;
int SignalJudge(double *Signal,int nSize,
double HighLow,int FlatMax);
int main()
{
double Signal[110]={1,1,1,5,5,5,5,1,1,1,5,
5,5,5,5,1,1,1,1,1,5.1,5.3,
5.1,5,1,1,1,1,1,1,5,5,5,5,
1,1,1,5,5,5,5,1,1,5,5,5,1,
5,5,1,1,1,1,5,5,5,5,5,5,5,
5,5,1,1,1,5,5,5,6,5,1,1,1,
5,5,5,6,7,1,1,1,5,5.6,5.4,
6.1,6.2,2,1,1,1,5,5,5,1,1.2,
1.3,5,5,5,6,1};
double HighLow=3.0;
int Number,FlatMin=5;
Number=SignalJudge(Signal,110,HighLow,FlatMin);
if(Number>=1)
{
cout<<"平坦区的个数为:"<<Number<<endl;
}
else
cout<<"没有符合要求的结果!"<<endl;
return 0;
}
int SignalJudge(double *Signal,int nSize,
double HighLow,int FlatMin)
{
int p,q,s=1,FlatWidth,InitFlatWidth=3,temp;
int FlatNum=0,sum=0,k=1;
double Max,Min,average;
vector<int> position,RealFlatWidth;
while(s<nSize)
{
if((Signal[s]-Signal[s-1])>HighLow)
{
FlatWidth=InitFlatWidth;
while(1)
{
for(p=s;p<s+FlatWidth;p++)
{
for(q=s+FlatWidth-1;q>p;q--)
{
if(Signal[q]<Signal[q-1])
{
temp=Signal[q-1];
Signal[q-1]=Signal[q];
Signal[q]=temp;
}
}
}
Max=Signal[s+FlatWidth-1];
Min=Signal[s];
if((Max-Min)<HighLow)
FlatWidth++;
else
break;
}
if(FlatWidth-1>=FlatMin)
{
position.push_back(s);
RealFlatWidth.push_back(FlatWidth-1);
FlatNum++;
}
s=s+FlatWidth-1;
}
else
s++;
}
for(int i=0;i<FlatNum;i++)
sum=sum+RealFlatWidth[i];
for(vector<int>::iterator iter1=position.begin(),
iter2=RealFlatWidth.begin();iter1!=position.end(),
iter2!=RealFlatWidth.end();iter1++,iter2++)
{
cout<<"第"<<k<<"个有效平坦区的起始位置:"<<*iter1<<endl;
cout<<"第"<<k<<"个有效平坦区的长度:"<<*iter2<<endl;
k++;
}
if(FlatNum>=1)
cout<<"有效平坦区的平均长度为:"<<(double)sum/FlatNum<<endl;
return FlatNum;
}
编译运行如下:
二.接下来,将上面代码中红色的函数部分封装成dll。(即函数SignalJudge)
1).新建工程MFC appWizard(dll),名为dlltest
2)在dlltest.h中添加代码段如下:
3)在dlltest.cpp中添加代码段如下:
int SignalJudge(double *Signal,int nSize, double HighLow,int FlatMin)
{
int p,q,s=1,FlatWidth,InitFlatWidth=3,temp;
int FlatNum=0,sum=0,k=1;
double Max,Min,average;
vector<int> position,RealFlatWidth;
while(s<nSize)
{
if((Signal[s]-Signal[s-1])>HighLow)
{
FlatWidth=InitFlatWidth;
while(1)
{
for(p=s;p<s+FlatWidth;p++)
{
for(q=s+FlatWidth-1;q>p;q--)
{
if(Signal[q]<Signal[q-1])
{
temp=Signal[q-1];
Signal[q-1]=Signal[q];
Signal[q]=temp;
}
}
}
Max=Signal[s+FlatWidth-1];
Min=Signal[s];
if((Max-Min)<HighLow)
FlatWidth++;
else
break;
}
if(FlatWidth-1>=FlatMin)
{
position.push_back(s);
RealFlatWidth.push_back(FlatWidth-1);
FlatNum++;
}
s=s+FlatWidth-1;
}
else
s++;
}
for(int i=0;i<FlatNum;i++)
sum=sum+RealFlatWidth[i];
for(vector<int>::iterator iter1=position.begin(),
iter2=RealFlatWidth.begin();iter1!=position.end(),
iter2!=RealFlatWidth.end();iter1++,iter2++)
{
cout<<"第"<<k<<"个有效平坦区的起始位置:"<<*iter1<<endl;
cout<<"第"<<k<<"个有效平坦区的长度:"<<*iter2<<endl;
k++;
}
if(FlatNum>=1)
cout<<"有效平坦区的平均长度为:"<<(double)sum/FlatNum<<endl;
return FlatNum;
}
注意:在testdll.cpp中添加必要的头文件
#include <vector>
#include <iostream>
using namespace std;
4)编译dll工程,生成testdll.dll和test.lib文件
三.创建应用程序工程对dll调用进行测试
1)新建win32 console application,名为dllcall.在工程中添加dllcall.cpp
代码如下:
#include<iostream>
#include<vector>
using namespace std;
extern "C" _declspec(dllimport) SignalJudge(double *Signal,int nSize,
double HighLow,int FlatMax);
int main()
{
double Signal[110]={1,1,1,5,5,5,5,1,1,1,5,
5,5,5,5,1,1,1,1,1,5.1,5.3,
5.1,5,1,1,1,1,1,1,5,5,5,5,
1,1,1,5,5,5,5,1,1,5,5,5,1,
5,5,1,1,1,1,5,5,5,5,5,5,5,
5,5,1,1,1,5,5,5,6,5,1,1,1,
5,5,5,6,7,1,1,1,5,5.6,5.4,
6.1,6.2,2,1,1,1,5,5,5,1,1.2,
1.3,5,5,5,6,1};
double HighLow=3.0;
int Number,FlatMin=4;
Number=SignalJudge(Signal,110,HighLow,FlatMin);
if(Number>=1)
{
cout<<"平坦区的个数为:"<<Number<<endl;
}
else
cout<<"没有符合要求的结果!"<<endl;
return 0;
}
2)将testdll拷贝至dllcall工程的debug目录下,将testdll.lib与testdll.h拷贝到dllcall工程目录下。在testcall中,工程--->设置--->link--->module library加入testdll.lib
3)编译,运行工程dllcall。结果如下:
4)显然,制作dll前后运行结果一样。函数功能均能实现,至此,SignalJudge函数实现了dll封装。
转载请注明作者:小刘
原文地址:http://blog.csdn.net/u013018721/article/details/39049119