惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 杨斌

Inside of my heart 春风稀稀-感慨 一个自动检测并安装hotfix的脚本 真正的菜鸟 C/C++是程序员必须掌握的语言吗? 一些KB ISA Proxy SSL Tunnel Extend 关于Exchange DSAccess组件目录检测机制 上海下雪了 关于VPN分隔隧道 - 杨斌 关于Exchange二次开发 微软停止对WindowsNT4.0系统提供无偿的支持 Windows2003ServerPack1测试版发布 有感而发 软件开源 TechED峰会 configure Squid proxy server 关于Cisco Catlasyt2900XL/3500XL上做port-Channel的问题 关于SidHistory
VC++中DLL的创建和使用
杨斌 · 2005-01-30 · via 博客园 - 杨斌

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());

}