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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 杨斌

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

}