VC++中DLL的创建和使用

1、  函数

a)         DLL中:

extern "C" __declspec(dllexport) BOOL isPrime(int num)

{

       BOOL flag 
= false;

       
for(int i = 2 ; i < num ; i ++)

       
{

              
if(num % i == 0)

                     
break;

       }


       
if (i == num)

              flag 
= true;

       
else

              flag 
= false;

 

       
return flag;

}



b)        应用程序
typedef BOOL ISPRIME(int);

       ISPRIME 
*isPrime;

       hm 
= ::LoadLibrary("mydll2.dll");

       isPrime 
= (ISPRIME *)::GetProcAddress(hm,"isPrime");

       
if(isPrime(8))

              MessageBox(
"是素数");

       
else

              MessageBox(
"不是素数");

       hm 
= ::LoadLibrary("mydll2.dll");


 2、 
a)         DLL
 i.              IloveYou.h头文件

class __declspec(dllexport)  CILoveYou  

{

public:

       
int GetValue();

       
void SetValue(int v);

       CILoveYou();

       
virtual ~CILoveYou();

 

private:

       
int a;

}
;

                       ii.              IloveYou.cpp程序文件

CILoveYou::CILoveYou()

{

       a 
= 0;

}


 

CILoveYou::
~CILoveYou()

{

 

}


 

__declspec(dllexport) 
void CILoveYou::SetValue(int v)

{

       
this->= v;

}


 

__declspec(dllexport) 
int CILoveYou::GetValue()

{

       
return a;

}



b)        应用程序
先把#include "ILoveYou.h"文件导入进来,然后在StdAfc.h头文件加入:

class __declspec(dllimport)  CILoveYou;

访问该类的代码:
CILoveYou ily;

ily.SetValue(
900);

char s[100];

wsprintf(s,
"调用了类中的成员哦,值是:%d",ily.GetValue());

ShowMessage(
this->GetSafeHwnd(),s);

 


MFC 规则DLL

1、  函数

a)         DLL

此类DLL有一个继承了CwinApp的类,但是函数可以不放在该类中。

extern "C" __declspec(dllexport)  BOOL isOdd(int num)

{

       AFX_MANAGE_STATE(AfxGetStaticModuleState());
//此句一定要

       
if(num % 2 == 0)

              
return true;

       
else 

              
return false;

}



b)        应用程序

void CTestdll2Dlg::OnButton5() 

{

       
// TODO: Add your control notification handler code here

       typedef BOOL ISODD(
int);

       ISODD 
*isOdd;

 

       HINSTANCE hm;

       
if(hm = ::LoadLibrary("mfcdll4.dll"))

       
{

              isOdd 
= (ISODD *)::GetProcAddress(hm,"isOdd");

              
if(isOdd)

              
{

                     
if(isOdd(9))

                            MessageBox(
"是偶数");

                     
else

                     MessageBox(
"不是偶数");

              }


              
else

              
{

                     MessageBox(
"有问题");

              }


              ::FreeLibrary(hm);

       }


       
else

       
{

              MessageBox(
"DLL加载失败");

       }


}



2、 

a)         DLL中的代码

                         i.              Clzh类的头文件:lzh.h

class AFX_EXT_CLASS Clzh  //此处一定要用AFX_EXT_CLASS

{

public:

       CString GetValue();

       
void SetValue(CString str);

       Clzh();

 

private:

       CString str;

}
;


    ii.              Clzh类的实现文件:lzh.cpp

Clzh::Clzh()

{

 

}


 

__declspec(dllexport) 
void Clzh::SetValue(CString str)

{

       AFX_MANAGE_STATE(AfxGetStaticModuleState());

       
this->str = str;

}


 

__declspec(dllexport) CString Clzh::GetValue()

{

       AFX_MANAGE_STATE(AfxGetStaticModuleState());

       
return str;

}



b)        应用程序

StdAfx.h中头文件中加入:class __declspec(dllimport)  Clzh;

在要访问该类的地方加入头文件:#include "lzh.h"

程序如下:

void CTestdll2Dlg::OnButton7() 

{

       
// TODO: Add your control notification handler code here

       Clzh lzh;

       lzh.SetValue(
"abc");

       MessageBox(lzh.GetValue());

}


原文地址:https://www.cnblogs.com/CoCo/p/99537.html