1. DLL(Dynamic Linkable Library) DLL lib EXE DLL EXE EXE EXE DLL 1 DLL DLL DLL Windows DLL Windows API Visual Basic Visual C++ Delphi 2 Windows system



Similar documents
bingdian001.com

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

ebook50-15

FY.DOC

ebook50-14

概述

INTRODUCTION TO COM.DOC

Microsoft Word - CIN-DLL.doc

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

ebook50-11

新版 明解C++入門編

概述

Java

untitled

提问袁小兵:

untitled

ebook140-8

epub83-1

用户大会 论文集2.2.doc

c_cpp

CC213

Microsoft Word - 11.doc


VB程序设计教程

ebook140-9

CHAPTER 1

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

C 1

untitled

新・解きながら学ぶJava


mvc

新版 明解C言語入門編

nooog

_汪_文前新ok[3.1].doc

Microsoft PowerPoint - ch6 [相容模式]

新・解きながら学ぶC言語

ebook51-14

02

(\244j\257d\276\307\274\351_ C.indd_70%.pdf)

C/C++程序设计 - 字符串与格式化输入/输出

MFC 2/e PDF GBK mirror - anyway solution MFC 1/e MFC 2/e

Microsoft Word - 01.DOC

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

CHAPTER VC#

untitled

C/C++ - 字符输入输出和字符确认

全国计算机技术与软件专业技术资格(水平)考试

Microsoft Word - ch04三校.doc

PIC_SERVER (11) SMTP ( ) ( ) PIC_SERVER (10) SMTP PIC_SERVER (event driven) PIC_SERVER SMTP 1. E-

云 浮 市 总 工 会 学 习 贯 彻 市 委 五 届 九 次 全 会 精 神 全 省 工 会 第 二 季 度 暨 上 半 年 劳 资 纠 纷 研 判 会 召 开 河 源 市 总 工 会 召 开 劳 资 纠 纷 研 判 会 议 湛 江 市 总 工 会 召 开 上 半 年 劳 资 纠 纷 研 判 会

新・明解C言語入門編『索引』

Microsoft Word - 第3章.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

附录J:Eclipse教程

Microsoft PowerPoint - Introduction to Windows Programming and MFC

C/C++ - 文件IO

Microsoft PowerPoint - ds-1.ppt [兼容模式]

untitled

untitled

Strings

华恒家庭网关方案

提纲 1 2 OS Examples for 3

TwinCAT 1. TwinCAT TwinCAT PLC PLC IEC TwinCAT TwinCAT Masc

高 职 计 算 机 类 优 秀 教 材 书 目 * 序 号 书 号 (ISBN) 书 名 作 者 定 价 出 版 / 印 刷 日 期 ** 配 套 资 源 页 码 计 算 机 基 础 课 计 算 机 应 用 基 础 刘 升 贵 年 8 月

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

bingdian001.com

CC213

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

年 中 央 国 家 机 关 政 府 采 购 中 心 网 上 竞 价 品 目 表 中 央 国 家 机 关 政 府 采 购 中 心 2016 年 7 月 21 日 - 2 -

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

24 第 3 章 创 建 Access 数 据 库 和 表 3.1 创 建 Access 数 据 库 Access 2003 中 的 数 据 库 属 于 关 系 型 数 据 库, 以.mdb 为 文 件 的 后 缀, 建 立 一 个 数 据 库 的 同 时, 就 创 建 了 数 据 库 中 的 对

1. 软 件 核 武 器 CTreeNode 基 本 原 理 以 下 详 细 介 绍 这 一 技 术 的 基 本 原 理 -CTreeNode 这 是 一 种 数 据 结 构, 类 似 于 CObject 存 在 于 内 存 中 1. 能 够 以 树 形 的 方 式 嵌 套 存 贮 非 常 复 杂

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

Microsoft Word - 新建 Microsoft Word 文档

蒙 恬 科 技 軟 件 用 戶 授 權 協 議 本 許 可 協 議 為 蒙 恬 科 技 股 份 有 限 公 司 ( 以 下 簡 稱 蒙 恬 公 司 ) 授 予 您 合 法 使 用 本 軟 件 程 式 ( 本 軟 件 ) 之 協 議 書, 如 果 您 不 同 意 此 協 議 中 的 任 何 條 款,

The golden pins of the PCI card can be oxidized after months or years



从 因 人 设 事 谈 起 一 部 文 学 作 品 ( 尤 其 是 长 篇 小 说 ) 的 结 构 至 关 重 要, 因 为 它 是 文 本 整 体 的 组 织 方 式 和 内 部 构 造, 既 是 形 式 又 是 内 容 ; 乃 是 表 达 主 题 最 有 效 的 艺 术 手 段 元 代 戏 曲

循经指压疗法

Microsoft Word - HERBRECIPES《中國藥膳》.doc

毛主席的猪

附件1.FIT)


北魏山东佛教文化个案研究

C++ 程式設計

中 国 中 西 医 结 合 杂 志 年 月 第 卷 第 期!" 通 透 性 增 加 产 生 蛋 白 水 解 酶 促 进 血 管 内 皮 细 胞 有 丝 分 裂 内 皮 细 胞 从 基 底 膜 上 迁 移 到 血 管 周 围 间 隙 粘 附 聚 集 重 构 为 三 维 管 腔 并 与 周 围 血 管

教育部高等学校教学

Microsoft PowerPoint - OPVB1基本VB.ppt

第3章.doc

Symantec™ Sygate Enterprise Protection 防护代理安装使用指南

ebook129-9

51 C 51 isp 10 C PCB C C C C KEIL

Strings

Microsoft PowerPoint - WP20.ppt

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Transcription:

VC++ e-mail 21cnbao@21cn.com Pconline DLL DLL DLL WINRAR VC++6.0 DLL C C++ MFC 1 2 3 4 MFC DLL 5 MFC DLL 6 MFC DLL

1. DLL(Dynamic Linkable Library) DLL lib EXE DLL EXE EXE EXE DLL 1 DLL DLL DLL Windows DLL Windows API Visual Basic Visual C++ Delphi 2 Windows system32 kernel32.dll user32.dll gdi32.dll windows API DLL kernel32.dll user32.dll gdi32.dll MessageBox user32.dll DLL (3)VC Visual C++ DLL Non-MFC DLL MFC MFC Regular DLL MFC DLL MFC Extension DLL MFC DLL MFC MFC C MFC MFC MFC DLL CWinApp MFC DLL MFC MFC

2 DLL 1 1 VC++6.0 new libtest static library libtest.zip lib.h lib.cpp lib.h lib.cpp #include <stdio.h> #include "..\lib.h" #pragma comment( lib, "..\\debug\\libtest.lib" ) int main(int argc, char* argv[]) printf( "2 + 3 = %d", add( 2, 3 ) ); //.lib add.lib add Turbo C2.0 C scanf printf memcpy strcpy

3 DLL F5 debug CTRL+F5 3 EXE 3 VC F11 2 libtest libcall 4 4 Visual C++ Depends

Depends user32.dll MessageBox 5 Depends DLL Depends DLL DLL DLL MFC DLL

4. MFC DLL 4.1 DLL 2 add add 6 VC++ new Win32 Dynamic-Link Library dlltest MFC AppWizard(dll) MFC AppWizard(dll) 5 6 MFC 6 MFC DLL lib.h lib.cpp /* lib.h */ #ifndef LIB_H #define LIB_H extern "C" int declspec(dllexport)add(int x, int y); #endif /* lib.cpp */

#include "lib.h" int add(int x, int y) return x + y; 2 DLL dllcall DLL add #include <stdio.h> #include <windows.h> typedef int(*lpaddfun)(int, int); // int main(int argc, char *argv[]) HINSTANCE hdll; //DLL lpaddfun addfun; // hdll = LoadLibrary("..\\Debug\\dllTest.dll"); if (hdll!= NULL) addfun = (lpaddfun)getprocaddress(hdll, "add"); if (addfun!= NULL) int result = addfun(2, 3); printf("%d", result);

FreeLibrary(hDll); return 0; dlltest lib.cpp 2 lib.h add declspec(dllexport) add DLL DLL (1)DLL (2) DLL DLL DLL 2 typedef int ( * lpaddfun)(int,int) add main lpaddfun addfun main DLL HINSTANCE hdll Win32 Api LoadLibrary DLL DLL hdll main Win32 Api GetProcAddress DLL add addfun DLL add DLL main Win32 Api FreeLibrary DLL DLL (1)DLL (2) DLL

4.2 DLL 4.1 declspec(dllexport) (.def).def.def add DLL dlltest lib.def ; lib.def : DLL LIBRARY dlltest EXPORTS add @ 1.def (1)LIBRARY.def DLL (2)EXPORTS.def @n n (3).def (;) lib.def dlltest add add 1 4.3 DLL 4.1 LoadLibrary-GetProcAddress-FreeLibrary Api DLL -DLL -DLL DLL API DLL DLL DLL IP DHCP DLL

DLL DLL DLL DLL DLL Windows DLL 1 DLL dlltest.lib.dll dllcall dllcall #pragma comment(lib,"dlltest.lib") //.lib DLL extern "C" declspec(dllimport) add(int x,int y); int main(int argc, char* argv[]) int result = add(2,3); printf("%d",result); return 0; (1) DLL.lib #pragma comment(lib,"dlltest.lib") DLL.lib DLL.lib DLL (2) extern "C" declspec(dllimport) add(int x,int y) declspec(dllimport) API DLL DLL

.lib EXE.lib DLL EXE DLL Windows DLL EXE DLL 4.4 DllMain Windows DLL DOS main WIN32 WinMain DLL DllMain DLL Windows DllMain DllMain DLL DllMain Windows DLL DllMain DLL DLL DLL DllMain DllMain DllMain BOOL APIENTRY DllMain( HANDLE hmodule, DWORD ul_reason_for_call, LPVOID lpreserved ) switch (ul_reason_for_call) case DLL_PROCESS_ATTACH: printf("\nprocess attach of dll"); break; case DLL_THREAD_ATTACH: printf("\nthread attach of dll");

break; case DLL_THREAD_DETACH: printf("\nthread detach of dll"); break; case DLL_PROCESS_DETACH: printf("\nprocess detach of dll"); break; return TRUE; DllMain DLL DLLMain ul_reason_for_call 4 PROCESS_ATTACH PROCESS_DETACH THREAD_ATTACH THREAD_DETACH switch DllMain BOOL APIENTRY DllMain( HANDLE hmodule, WORD ul_reason_for_call, LPVOID lpreserved ) APIENTRY stdcall Pascal WINAPI DLL 32 HINSTANCE DLL Win32 HINSTANCE HMODULE hmodule hdll = LoadLibrary("..\\Debug\\dllTest.dll"); if (hdll!= NULL)

addfun = (lpaddfun)getprocaddress(hdll, MAKEINTRESOURCE(1)); //MAKEINTRESOURCE if (addfun!= NULL) int result = addfun(2, 3); printf("\ncall add in dll:%d", result); FreeLibrary(hDll); process attach of dll call add in dll:5 process detach of dll DllMain GetProcAddress ( hdll, MAKEINTRESOURCE ( 1 ) ).def add add MAKEINTRESOURCE ( 1 ) MAKEINTRESOURCE winuser.h #define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i))) #define MAKEINTRESOURCEW(i) (LPWSTR)((DWORD)((WORD)(i))) #ifdef UNICODE #define MAKEINTRESOURCE MAKEINTRESOURCEW #else #define MAKEINTRESOURCE MAKEINTRESOURCEA

4.5 stdcall VC++ DLL stdcall WINAPI C/C++ cdecl stdcall cdecl C ( C++ extern "C") stdcall @ _functionname@number cdecl _functionname Windows stdcall cdecl windef.h #define CALLBACK stdcall // #define WINAPI stdcall // WINAPI #define WINAPIV cdecl #define APIENTRY WINAPI //DllMain #define APIPRIVATE stdcall #define PASCAL stdcall lib.h add int stdcall add(int x, int y); typedef int( stdcall *lpaddfun)(int, int); lib.h stdcall typedef int (* lpaddfun)(int,int) cdecl 7

7 8 This is usually a result of stdcall 4.6 DLL DLL DLL DLL /* lib.h */ #ifndef LIB_H #define LIB_H extern int dllglobalvar; #endif /* lib.cpp */ #include "lib.h" #include <windows.h> int dllglobalvar;

BOOL APIENTRY DllMain(HANDLE hmodule, DWORD ul_reason_for_call, LPVOID lpreserved) switch (ul_reason_for_call) case DLL_PROCESS_ATTACH: dllglobalvar = 100; // dll 100 break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; return TRUE; ; lib.def ; DLL LIBRARY "dlltest" EXPORTS dllglobalvar CONSTANT ; dllglobalvar DATA GetGlobalVar

lib.h lib.cpp DLL.def EXPORTS CONSTANT // DATA //VC++ DLL #include <stdio.h> #pragma comment(lib,"dlltest.lib") extern int dllglobalvar; int main(int argc, char *argv[]) printf("%d ", *(int*)dllglobalvar); *(int*)dllglobalvar = 1; printf("%d ", *(int*)dllglobalvar); return 0; extern int dllglobalvar DLL DLL *(int*)dllglobalvar DLL dllglobalvar = 1;

dllglobalvar DLL DLL #include <stdio.h> #pragma comment(lib,"dlltest.lib") extern int _declspec(dllimport) dllglobalvar; // _declspec(dllimport) int main(int argc, char *argv[]) printf("%d ", dllglobalvar); dllglobalvar = 1; //, printf("%d ", dllglobalvar); return 0; _declspec(dllimport) DLL 4.7 DLL DLL DLL point circle // point.h point #ifndef POINT_H

#define POINT_H #ifdef DLL_FILE class _declspec(dllexport) point // point #else class _declspec(dllimport) point // point #endif public: float y; float x; point(); point(float x_coordinate, float y_coordinate); ; #endif // point.cpp point #ifndef DLL_FILE #define DLL_FILE #endif #include "point.h" // point point::point()

x = 0.0; y = 0.0; // point point::point(float x_coordinate, float y_coordinate) x = x_coordinate; y = y_coordinate; // circle.h circle #ifndef CIRCLE_H #define CIRCLE_H #include "point.h" #ifdef DLL_FILE class _declspec(dllexport)circle // circle #else class _declspec(dllimport)circle // circle #endif public: void SetCentre(const point repoint);

void SetRadius(float r); float GetGirth(); float GetArea(); circle(); private: float radius; point centre; ; #endif // circle.cpp circle #ifndef DLL_FILE #define DLL_FILE #endif #include "circle.h" #define PI 3.1415926 //circle circle::circle() centre = point(0, 0); radius = 0;

// float circle::getarea() return PI *radius * radius; // float circle::getgirth() return 2 *PI * radius; // void circle::setcentre(const point repoint) centre = centrepoint; // void circle::setradius(float r) radius = r;

#include "..\circle.h" // #pragma comment(lib,"dlltest.lib"); int main(int argc, char *argv[]) circle c; point p(2.0, 2.0); c.setcentre(p); c.setradius(1.0); printf("area:%f girth:%f", c.getarea(), c.getgirth()); return 0; DLL DLL_FILE class _declspec(dllexport) point // point class _declspec(dllexport) circle // circle

DLL_FILE point.h circle.h class _declspec(dllimport) point // point class _declspec(dllimport) circle // circle DLL class _declspec(dllexport) class_name // circle class _declspec(dllimport) class_name //

class _declspec(dllexport) class_name class _declspec(dllimport) class_name #ifdef DLL_FILE class _declspec(dllexport) class_name // #else class _declspec(dllimport) class_name // #endif MFC DLL _declspec(dllexport) _declspec(dllimport) DLL DLL DLL VC++ MFC DLL MFC DLL 4 MFC DLL MFC DLL e-mail

5. MFC DLL 5.1 MFC DLL 1 MFC MFC DLL MFC 2 MFC DLL MFC DLL MFC DLL MFC DLL MFC Regular DLL DLL MFC CWinApp DllMain MFC Regular DLL 1 MFC DLL MFC DLL MFC MFC DLL MFC.dll DLL MFC DLL

MFC DLL DLL 2 MFC DLL MFC DLL MFC DLL MFC DLL MFC MFC DLL ID MFC DLL DLL MFC Visual C++ MFC DLL MFC DLL MFC DLL 8 Visual C++ project -> Settings -> General Microsoft Foundation Classes 8 / MFC DLL 5.2 MFC DLL

MFC MFC DLL project 9 project MFC AppWizard(dll) OK 10 9 MFC DLL 10 1 MFC DLL 2 automation Microsoft Word Microsoft Excel 3 Windows Sockets TCP/IP CWinApp InitInstance StdAfx.h include <AfxSock.h> socket InitInstance BOOL CRegularDllSocketApp::InitInstance()

if (!AfxSocketInit()) AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; return TRUE; 4 MFC Yes,please 10 MFC DLL 5.3 MFC DLL DLL MFC DLL 11

11 MFC DLL DLL MFC 11 DLL Hello MessageBox Hello,pconline MFC CWinApp // RegularDll.h : main header file for the REGULARDLL DLL #if!defined(afx_regulardll_h 3E9CB22B_588B_4388_B778_B3416ADB79B3 INCLUDED_) #define AFX_REGULARDLL_H 3E9CB22B_588B_4388_B778_B3416ADB79B3 INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef AFXWIN_H

#error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols class CRegularDllApp : public CWinApp public: CRegularDllApp(); DECLARE_MESSAGE_MAP() ; #endif // RegularDll.cpp : Defines the initialization routines for the DLL. #include "stdafx.h" #include "RegularDll.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = FILE ;

#endif BEGIN_MESSAGE_MAP(CRegularDllApp, CWinApp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CRegularDllApp construction CRegularDllApp::CRegularDllApp() ///////////////////////////////////////////////////////////////////////////// // The one and only CRegularDllApp object CRegularDllApp theapp; CWinApp CRegularDllApp theapp MFC MFC App CWinApp theapp MFC CWinApp SDK WinMain SDK WinMain CWinApp virtual BOOL InitApplication( );

virtual BOOL InitInstance( ); virtual BOOL Run( ); // MFC MFC DLL MFC CWinApp MFC DLL CWinApp::Run DLL DLL PreTranslateMessage MFC DLL MFC DLL InitInstance ( ) MFC MFC ON_BN_CLICKED(IDC_HELLO_BUTTON, OnHelloButton) DLL //NO_DEPENDENCIES // Microsoft Developer Studio generated include file. // Used by RegularDll.rc // #define IDD_DLL_DIALOG 1000 #define IDC_HELLO_BUTTON 1000

MFC DLL MFC Visual C++ MFC DLL #include "StdAfx.h" #include "DllDialog.h" extern "C" declspec(dllexport) void ShowDlg(void) CDllDialog dlldialog; dlldialog.domodal(); MFC CdllDialog MFC DLL declspec(dllexport).def MFC DLL 5.4 MFC DLL 12 MFC 5.3 MFC DLL DLL 5.3 MFC DLL

12 MFC DLL DLL void CRegularDllCallDlg::OnCalldllButton() typedef void (*lpfun)(void); HINSTANCE hdll; //DLL hdll = LoadLibrary("RegularDll.dll"); if (NULL==hDll) MessageBox("DLL "); lpfun addfun; // lpfun pshowdlg = (lpfun)getprocaddress(hdll,"showdlg"); if (NULL==pShowDlg)

MessageBox("DLL "); pshowdlg(); 4 MFC DLL EXE MFC DLL.lib.dll RegularDllCallDlg.cpp 12 #pragma comment(lib,"regulardll.lib") void ShowDlg(void); void CRegularDllCallDlg::OnCalldllButton() void CRegularDllCallDlg::OnCalldllButton() ShowDlg(); 5.5 MFC DLL DLL DLL HINSTANCE DLL EXE 0x400000 DLL 0x10000000 DLL

DLL HINSTANCE DLL MFC DLL MFC DLL HINSTANCE HINSTANCE EXE DLL ID DLL DLL EXE EXE MFC DLL DLL 13 13 DLL DLL MFC 12 14

14 EXE 13 14 caption DLL EXE 13 14 ID=2000 DLL EXE resource.h //DLL ID #define IDD_DLL_DIALOG 2000 //EXE ID #define IDD_EXE_DIALOG 2000 5.3 MFC DLL DLL DLL ShowDlg #include "StdAfx.h" #include "SharedDll.h" void ShowDlg(void) CDialog dlg(idd_dll_dialog); // ID 2000

dlg.domodal(); DLL void CSharedDllCallDlg::OnCalldllButton() ShowDlg(); DLL 13 14 EXE MFC DLL MFC DLL MFC DLL EXE DLL AFX_MANAGE_STATE(AfxGetStaticModuleState()); DLL ShowDlg void ShowDlg(void) // 1: // AFX_MANAGE_STATE(AfxGetStaticModuleState()); //

AFX_MANAGE_STATE(AfxGetStaticModuleState()); CDialog dlg(idd_dll_dialog);// 2000 dlg.domodal(); EXE DLL DLL 13 AfxGetStaticModuleState AFX_MODULE_STATE* AFXAPI AfxGetStaticModuleState( ); AFX_MODULE_STATE pmodulestate AFX_MODULE_STATE // AFX_MODULE_STATE (global data for a module) class AFX_MODULE_STATE : public CNoTrackObject public: #ifdef _AFXDLL AFX_MODULE_STATE(BOOL bdll, WNDPROC pfnafxwndproc, DWORD dwversion); AFX_MODULE_STATE(BOOL bdll, WNDPROC pfnafxwndproc, DWORD dwversion,bool bsystem); #else

AFX_MODULE_STATE(BOOL bdll); #endif ~AFX_MODULE_STATE(); CWinApp* m_pcurrentwinapp; HINSTANCE m_hcurrentinstancehandle; HINSTANCE m_hcurrentresourcehandle; LPCTSTR m_lpszcurrentappname; // AFX_MODULE_STATE call pc sp AFX_MANAGE_STATE AFX_MANAGE_STATE( AFX_MODULE_STATE* pmodulestate ) pmodulestate pmodulestate AFX_MODULE_STATE DLL AfxGetResourceHandle(); AfxSetResourceHandle(HINSTANCE xxx);

AfxGetResourceHandle AfxSetResourceHandle DLL ShowDlg void ShowDlg(void) // 2 HINSTANCE save_hinstance = AfxGetResourceHandle(); AfxSetResourceHandle(theApp.m_hInstance); CDialog dlg(idd_dll_dialog);// 2000 dlg.domodal(); // 2 AfxSetResourceHandle(save_hInstance); AfxGetResourceHandle AfxSetResourceHandle DLL ShowDlg extern CSharedDllApp theapp; // theapp void ShowDlg(void) // 2

HINSTANCE save_hinstance = AfxGetResourceHandle(); AfxSetResourceHandle(theApp.m_hInstance); CDialog dlg(idd_dll_dialog);// 2000 dlg.domodal(); // 2 AfxSetResourceHandle(save_hInstance); // 2 CDialog dlg1(idd_dll_dialog); // ID 2000 dlg1.domodal(); DLL 13 EXE 14 DLL DLL void ShowDlg(void) CDialog dlg(idd_dll_dialog); // ID 2000

dlg.domodal(); OnCalldllButton void CSharedDllCallDlg::OnCalldllButton() // 3 // EXE HINSTANCE exe_hinstance = GetModuleHandle(NULL); // HINSTANCE exe_hinstance = AfxGetResourceHandle(); // DLL HINSTANCE dll_hinstance = GetModuleHandle("SharedDll.dll"); AfxSetResourceHandle(dll_hInstance); // ShowDlg(); // DLL AfxSetResourceHandle(exe_hInstance); // // ShowDlg ShowDlg(); // EXE Win32 GetModuleHandle DLL DLL EXE Null GetModuleHandle AfxGetResourceHandle

AfxSetResourceHandle DLL DLL 13 EXE 14 MFC DLL MFC DLL MFC MFC DLL MFC DLL MFC DLL MFC MFC DLL MFC DLL MFC MFC CStatic CButton Visual C++ MFC DLL MFC DLL DllMain extern "C" int APIENTRY DllMain(HINSTANCE hinstance, DWORD dwreason, LPVOID lpreserved) // Remove this if you use lpreserved UNREFERENCED_PARAMETER(lpReserved); if (dwreason == DLL_PROCESS_ATTACH) TRACE0("MFCEXPENDDLL.DLL Initializing!\n"); // Extension DLL one-time initialization if (!AfxInitExtensionModule(MfcexpenddllDLL, hinstance)) return 0; // Insert this DLL into the resource chain // NOTE: If this Extension DLL is being implicitly linked to by // an MFC Regular DLL (such as an ActiveX Control) // instead of an MFC application, then you will want to // remove this line from DllMain and put it in a separate // function exported from this Extension DLL. The Regular DLL // that uses this Extension DLL should then explicitly call that // function to initialize this Extension DLL. Otherwise, // the CDynLinkLibrary object will not be attached to the // Regular DLL's resource chain, and serious problems will // result. new CDynLinkLibrary(MfcexpenddllDLL);

else if (dwreason == DLL_PROCESS_DETACH) TRACE0("MFCEXPENDDLL.DLL Terminating!\n"); // Terminate the library before destructors are called AfxTermExtensionModule(MfcexpenddllDLL); return 1; // ok MFC DLL MFC DLL DLL MFC DLL 6.1 MFC DLL MFC DLL CSXButton MFC CButton CSXButton CButton MFC CSXbutton Internet MFC DLL MFC DLL DLL DLL // for data #ifndef AFX_DATA_EXPORT #define AFX_DATA_EXPORT declspec(dllexport) #endif #ifndef AFX_DATA_IMPORT #define AFX_DATA_IMPORT declspec(dllimport) #endif // for classes #ifndef AFX_CLASS_EXPORT #define AFX_CLASS_EXPORT declspec(dllexport) #endif

#ifndef AFX_CLASS_IMPORT #define AFX_CLASS_IMPORT declspec(dllimport) #endif // for global APIs #ifndef AFX_API_EXPORT #define AFX_API_EXPORT declspec(dllexport) #endif #ifndef AFX_API_IMPORT #define AFX_API_IMPORT declspec(dllimport) #endif #ifndef AFX_EXT_DATA #ifdef _AFXEXT #define AFX_EXT_CLASS AFX_CLASS_EXPORT #define AFX_EXT_API AFX_API_EXPORT #define AFX_EXT_DATA AFX_DATA_EXPORT #define AFX_EXT_DATADEF #else #define AFX_EXT_CLASS AFX_CLASS_IMPORT #define AFX_EXT_API AFX_API_IMPORT #define AFX_EXT_DATA AFX_DATA_IMPORT #define AFX_EXT_DATADEF #endif #endif AFX_EXT_CLASS CSXButton #ifndef _SXBUTTON_H #define _SXBUTTON_H #define SXBUTTON_CENTER -1 class AFX_EXT_CLASS CSXButton : public CButton // Construction public: CSXButton(); // Attributes private: // Positioning

BOOL m_buseoffset; CPoint m_pointimage; CPoint m_pointtext; int m_nimageoffsetfromborder; int m_ntextoffsetfromimage; // Image HICON m_hicon; HBITMAP m_hbitmap; HBITMAP m_hbitmapdisabled; int m_nimagewidth, m_nimageheight; // Color Tab char m_bcolortab; COLORREF m_crcolortab; // State BOOL m_bdefault; UINT m_noldaction; UINT m_noldstate; // Operations public: // Positioning int SetImageOffset( int npixels ); int SetTextOffset( int npixels ); CPoint SetImagePos( CPoint p ); CPoint SetTextPos( CPoint p ); // Image BOOL SetIcon( UINT nid, int nwidth, int nheight ); BOOL SetBitmap( UINT nid, int nwidth, int nheight ); BOOL SetMaskedBitmap( UINT nid, int nwidth, int nheight, COLORREF crtransparentmask ); BOOL HasImage() return (BOOL)( m_hicon!= 0 m_hbitmap!= 0 ); // Color Tab void SetColorTab(COLORREF crtab); // State BOOL SetDefaultButton( BOOL bstate = TRUE ); private: BOOL SetBitmapCommon( UINT nid, int nwidth, int nheight, COLORREF crtransparentmask, BOOL busemask ); void CheckPointForCentering( CPoint &p, int nwidth, int nheight );

void Redraw(); // Overrides // ClassWizard generated virtual function overrides //AFX_VIRTUAL(CSXButton) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpdrawitemstruct); //AFX_VIRTUAL // Implementation public: virtual ~CSXButton(); // Generated message map functions protected: //AFX_MSG(CSXButton) afx_msg LRESULT OnGetText(WPARAM wparam, LPARAM lparam); //AFX_MSG DECLARE_MESSAGE_MAP() ; #endif SXBUTTON.CPP mfcexpenddll.lib mfcexpenddll.dll Visual Studio Depends.dll 15 15 ( )

declspec(dllexport).lib.map.map 16 DLL settings Link MAP Generate mapfile.map mfcexpenddll.map 15 symbol 0001:00000380?HasImage@CSXButton@@QAEHXZ 10001380 f i SXBUTTON.OBJ 0001:000003d0??0CSXButton@@QAE@XZ 100013d0 f SXBUTTON.OBJ 0001:00000500??_GCSXButton@@UAEPAXI@Z 10001500 f i SXBUTTON.OBJ 0001:00000570??_ECSXButton@@UAEPAXI@Z 10001570 f i SXBUTTON.OBJ 0001:00000630??1CSXButton@@UAE@XZ 10001630 f SXBUTTON.OBJ 0001:00000700?_GetBaseMessageMap@CSXButton@@KGPBUAFX_MSGMAP@@XZ 10001700 f SXBUTTON.OBJ 0001:00000730?GetMessageMap@CSXButton@@MBEPBUAFX_MSGMAP@@XZ 10001730 f SXBUTTON.OBJ 0001:00000770?Redraw@CSXButton@@AAEXXZ 10001770 f i SXBUTTON.OBJ 0001:000007d0?SetIcon@CSXButton@@QAEHIHH@Z 100017d0 f SXBUTTON.OBJ..//

16.map ( ) MFC DLL.lib 6.2 MFC DLL DLL dllcall MFC EXE SXBUTTON1 SXBUTTON2 Owner draw 17 17 Owner draw ICON IDI_MSN_ICON MSN IDI_REFBAR_ICON Windows calldlldlg.h #include "..\..\mfcexpenddll\sxbutton.h" // dll #pragma comment(lib,"mfcexpenddll.lib") // dll ///////////////////////////////////////////////////////////////////////////// // CCalldllDlg dialog class CCalldllDlg : public CDialog // Construction public: CCalldllDlg(CWnd* pparent = NULL); // standard constructor // Dialog Data //AFX_DATA(CCalldllDlg) enum IDD = IDD_CALLDLL_DIALOG ;

// CSXButton m_button1; CSXButton m_button2; calldlldlg.cpp m_button1 m_button2 void CCalldllDlg::DoDataExchange(CDataExchange* pdx) CDialog::DoDataExchange(pDX); //AFX_DATA_MAP(CCalldllDlg) DDX_Control(pDX, IDC_BUTTON2, m_button2); DDX_Control(pDX, IDC_BUTTON1, m_button1); //AFX_DATA_MAP BOOL CCalldllDlg::OnInitDialog() ICON BOOL CCalldllDlg::OnInitDialog() CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* psysmenu = GetSystemMenu(FALSE); if (psysmenu!= NULL) CString straboutmenu; straboutmenu.loadstring(ids_aboutbox); if (!straboutmenu.isempty()) psysmenu->appendmenu(mf_separator); psysmenu->appendmenu(mf_string, IDM_ABOUTBOX, straboutmenu);

// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_button1.seticon(idi_msn_icon,16,16); m_button2.seticon(idi_refbar_icon,16,16); return TRUE; // return TRUE unless you set the focus to a control 18 MFC DLL 18 DLL void CCalldllDlg::DoDataExchange(CDataExchange* pdx) DDX_Control(pDX, IDC_BUTTON2, m_button2); DDX_Control(pDX, IDC_BUTTON1, m_button1); BOOL CCalldllDlg::OnInitDialog() m_button1 m_button2 IDC_BUTTON1 IDC_BUTTON2 m_button1.subclassdlgitem(idc_button1, this); m_button2.subclassdlgitem(idc_button2, this);

DDX_Control SubclassDlgItem 6.3 MFC DLL MFC DLL MFC DLL MFC MFC DLL MFC DLL