Dll的示例代码
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->namespaceMathFuncs
{
classMyMathFuncs
{
public:
//Returnsa+b
static__declspec(dllexport)doubleAdd(doublea,doubleb);
//Returnsa-b
static__declspec(dllexport)doubleSubtract(doublea,doubleb);
//Returnsa*b
static__declspec(dllexport)doubleMultiply(doublea,doubleb);
//Returnsa/b
//ThrowsDivideByZeroExceptionifbis0
static__declspec(dllexport)doubleDivide(doublea,doubleb);
};
}
#include"MathFuncsDll.h"
#include<stdexcept>
usingnamespacestd;
namespaceMathFuncs
{
doubleMyMathFuncs::Add(doublea,doubleb)
{
returna+b;
}
doubleMyMathFuncs::Subtract(doublea,doubleb)
{
returna-b;
}
doubleMyMathFuncs::Multiply(doublea,doubleb)
{
returna*b;
}
doubleMyMathFuncs::Divide(doublea,doubleb)
{
if(b==0)
{
thrownewinvalid_argument("bcannotbezero!");
}
returna/b;
}
}
//MyExecRefsDll.cpp:定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"MathFuncsDll.h"
#include<iostream>
usingnamespacestd;
int_tmain(intargc,_TCHAR*argv[])
{
doublea=7.4;
intb=99;
cout<<"a+b="<<
MathFuncs::MyMathFuncs::Add(a,b)<<endl;
cout<<"a-b="<<
MathFuncs::MyMathFuncs::Subtract(a,b)<<endl;
cout<<"a*b="<<
MathFuncs::MyMathFuncs::Multiply(a,b)<<endl;
cout<<"a/b="<<
MathFuncs::MyMathFuncs::Divide(a,b)<<endl;
cin>>a;
return0;
}
{
classMyMathFuncs
{
public:
//Returnsa+b
static__declspec(dllexport)doubleAdd(doublea,doubleb);
//Returnsa-b
static__declspec(dllexport)doubleSubtract(doublea,doubleb);
//Returnsa*b
static__declspec(dllexport)doubleMultiply(doublea,doubleb);
//Returnsa/b
//ThrowsDivideByZeroExceptionifbis0
static__declspec(dllexport)doubleDivide(doublea,doubleb);
};
}
#include"MathFuncsDll.h"
#include<stdexcept>
usingnamespacestd;
namespaceMathFuncs
{
doubleMyMathFuncs::Add(doublea,doubleb)
{
returna+b;
}
doubleMyMathFuncs::Subtract(doublea,doubleb)
{
returna-b;
}
doubleMyMathFuncs::Multiply(doublea,doubleb)
{
returna*b;
}
doubleMyMathFuncs::Divide(doublea,doubleb)
{
if(b==0)
{
thrownewinvalid_argument("bcannotbezero!");
}
returna/b;
}
}
//MyExecRefsDll.cpp:定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"MathFuncsDll.h"
#include<iostream>
usingnamespacestd;
int_tmain(intargc,_TCHAR*argv[])
{
doublea=7.4;
intb=99;
cout<<"a+b="<<
MathFuncs::MyMathFuncs::Add(a,b)<<endl;
cout<<"a-b="<<
MathFuncs::MyMathFuncs::Subtract(a,b)<<endl;
cout<<"a*b="<<
MathFuncs::MyMathFuncs::Multiply(a,b)<<endl;
cout<<"a/b="<<
MathFuncs::MyMathFuncs::Divide(a,b)<<endl;
cin>>a;
return0;
}