SlideShare uma empresa Scribd logo
1 de 27
DLL Basics Reference:  Programming Applications for Microsoft Windows Fourth Edition p.p. 675
Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],AdvAPI32.dll    object security, registry manipulation ComDlg32.dll    common dialog boxes ComCtl32.dll    all of the common window controls other DLLs 你可以下  dumpbin /exports User32.dll  看看到底他提供了哪些  function?
Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
要注意的事情 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],VB  或 其他程式語言
要注意的事情 VOID EXEFunc() { PVOID pv = DLLFunc(); // Access the storage pointed to by pv... // Assumes that pv is in EXE's C/C++ run-time heap free(pv);   } PVOID DLLFunc() { // Allocate block from DLL's  // C/C++ run-time heap return(malloc(100)); } 使用你 DLL 的人可能 使用的是  static  版本的  free 在你的  DLL  中 , 使用  run-time 版本的  malloc 全部放在  DLL 中處理 VOID EXEFunc() { PVOID pv = DLLFunc(); // Access the storage pointed to by pv... // Assumes that pv is in EXE's C/C++ run-time heap DLLFreeFunc(pv); } PVOID DLLFunc() { // Allocate block from DLL's C/C++ run-time heap return(malloc(100)); } BOOL DLLFreeFunc(PVOID pv) { // Free block from DLL's C/C++ run-time heap return(free(pv));  } 比較好的策略 在  DLL  配置記憶體 , 在  DLL  釋放他
The Overall Picture ,[object Object],[object Object],[object Object],DLL modules  也可以  importing  其他人的  function  與變數使用 使用  DLL  的  Module
DLL  是 如何被建立 的   以及如何被應用程式  implicitly link export  函式原型 , 結構 Symbol  的  header file 函式的實作 產生  obj  檔 組合成  DLL  檔並且 產生  lib  檔 DLL export  的部分 使用  DLL  的部分 Import  你要使用的函式 原型 呼叫  DLL  函式 產生  obj  結合  lib  解析出呼叫 的函式  symbol  執行的部分 Loader  建立 address space  給  .exe  Loader  載入必要的  DLL  到 process 的  address space  Lib  檔 dll  檔 exe  檔 COFF  格式 COFF  格式 =Common Object File Format
建立一個最簡單的  DLL Module ,[object Object],[object Object],選擇  DLL 選擇  Empty Project 1 2 3 把後面的   MyDLL.h   與  MyDLL.cpp   加到  project  中 全區域變數 :  影響抽象性 ,  且不易維護 必須使用相同的編譯器 才能  reference class
Export Symbol  ,[object Object],#ifdef MYLIBExport #define MYLIBAPI  extern  "C" __declspec( dllexport ) #else #define  MYLIBAPI  extern  "C" __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); MyLib.h 定義  exported  函式及 變數的地方 當你要  export symbol 時 , 使用這個 當你要  import symbol 時 , 使用這個
實作  export  函式 #include <windows.h> #define MYLIBExport   #include &quot;..MyLib.h&quot; int g_nResult; int Add(int nLeft, int nRight) { g_nResult = nLeft + nRight; return(g_nResult); } Include  標準的  windows  及  C++ run-time Library  標頭檔 定義目前是  export Function  的  prototype 實作函式及 變數的地方 MyLibFile1.cpp #ifdef MYLIBExport #define MYLIBAPI  extern  &quot;C&quot; __declspec( dllexport ) #else #define  MYLIBAPI  extern  &quot;C&quot; __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); MyLib.h
為甚麼要加  extern “C” ?  ,[object Object],[object Object],[object Object],[object Object],extern  &quot;C&quot; __declspec( dllimport ) void myfunction() { … } void foo(int ,double); Comeau C++  會把  foo  改成  foo 例如 : void foo(int ,double); Comeau C++  會把  foo  改成  foo_Fid F    function name I     第一個參數是  int;  d     第二個參數是  double  各家編譯器有不同的作法 參考資料 : http://www.comeaucomputing.com/techtalk/#externc
我們來看看差別在哪裡 ! #ifdef MYLIBExport #define MYLIBAPI  extern  “C” __declspec( dllexport ) #else #define  MYLIBAPI  extern  &quot;C&quot; __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); 如果你的  MyLib.h  的內容如下 使用 “ C”  的方式命名 利用  dumpbin /exports  察看  MyDll.lib  結果 看到了嗎 Symbol  沒有加上  mangle 那請問一下 : 加上  extern “C”  後 ,  你的  DLL  是否還支援  function overloading ?
如果你的  MyLib.h  的內容如下 #ifdef MYLIBExport #define MYLIBAPI  extern  __declspec( dllexport ) #else #define  MYLIBAPI  extern  __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); 沒有使用 “ C” 利用  dumpbin  察看  MyDll.lib 結果 看到了嗎 Symbol  後面加了一些料
#include “MyLib.h” void main(){ int Sum=Add(1,2); } 好了 , 接下來 , 就是直接使用  function 若你是由  VC  幫你建立  DLL export symbol 骨架 ,  那麼 預設值是不加上” C” 要注意 :  別弄錯了 , 否則會發生  link  錯誤
使用  MyDLL.dll ,[object Object],選擇  Linker Input 1 設定  MyDLL.lib 2 #include “MyLib.h” void main(){ int Sum=Add(1,2); } 呼叫  MyDLL.dll export  的  function 3 你也可以直接在  Source code  中加入這行 #pragma comment( lib, &quot;c:kkMyDll.lib&quot; )
__declspec( dllexport )  的意義 ,[object Object],[object Object],[object Object],[object Object],extern  &quot;C&quot; __declspec( dllexport ) void myfunction() { … } Microsoft VC  發現 __declspec( dllexport ) 會在  .obj  中加入額外的鏈結資訊 亦即  lib  檔中包含 DLL  所 輸出的  symbol  以及 相對的位址  (Relative Virtual Address)
查閱   DLL  檔所  export  的  symbol  –  使用  DumpBin 公用程式 ,[object Object],[object Object],[object Object],Export  的  symbol 相對位址
讓其他的 IDE  也能使用  Visual C++ 的 DLL ,[object Object],[object Object],__declspec(dllexport) LONG  __stdcall  MyFunc(int a, int b); [email_address] 後面有  8  個  bytes __stdcall:   1.  最右邊 的參數先推入堆疊 2.  函式返回前 , 自行 移除堆疊中的參數 *Win32 API  使用  __stdcall 因為  C/C++  不定參數的關係 所以必須要在  function  後面加 上需要  pop  的數字
讓其他的 IDE  也能使用  Visual C++ 的 DLL ,[object Object],[object Object],[object Object],EXPORTS MyFunc #pragma comment(linker, &quot;/export : MyFunc=_MyFunc@8 &quot;)
沒有加入  .DEF  檔 加入  .DEF  檔 LIBRARY MYDll EXPORTS Add dumpbin /exports MyDLL.dll dumpbin /exports MyDLL.dll 補充
Import  的實際意義 ,[object Object],extern  &quot;C&quot; __declspec( dllimport ) void myfunction() { … } extern  &quot;C“  extern  void myfunction() { … } 但是 ,  如果你想產生比較 有效率的程式碼 .  請使用  __declspec(dllimport) 因為  __declspec(dllimport)  可以讓編譯器知道 這個  symbol  確定是由  DLL export  出來的
Import  的實際意義 ,[object Object],[object Object],使用  MyDLL.dll  的部分 使用  Kernel32.dll  的部分 DumpBin –imports UseDLL.exe
執行  Executable Module Step1:  Loader  會為該  Process  建立 virtual address Step2:   將  executable module  map   到 virtual address Step3:   檢查  import section  並且  map  需要的  DLL 到  virtual space  中 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Step4:   檢查 每個 DLL  的  import section   並且  map  需要的  DLL 系統會紀錄每個  DLL  載入的次數 使得 DLL  不會重複載入
當系統找不到  DLL  時 , 所發出的錯誤訊息 Step5:   檢查 所有模組的  import symbol  與 載入 DLL 的  export  是否一樣  Step6:   計算每個  symbol  的實際位址 將其放到  executable module  的 import section DLL base addr + RVA 事實上 , 是  update import section  中的  Imported Address Table (ITA) Reference: ms-help://MS.MSDNQTR.2004JAN.1033/dnmsj00/html/hood0200.htm
產生你的執行程式 ,[object Object],[object Object],[object Object],[object Object],[object Object],由於  DLL  的  base address 可能會改變 ,  所以必須在  loading time  時 , 才能決定  import function  真實所在位址
[object Object]
附錄 使用  Borland’s IMPLIB.exe  可直接將  COFF  的  library format  轉成  OMF  格式 IMPLIB (destination lib name) (source dll)  例如 :  IMPLIB mydll.lib mydll.dll  OMF: Object Module Format  (BCB  使用 ) COFF: Common Object File Format  (VC  使用 )

Mais conteúdo relacionado

Mais procurados

Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharDreamtech Labs
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13Chris Ohk
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010Satish Verma
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpen Gurukul
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++oggyrao
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 

Mais procurados (20)

Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Deep C
Deep CDeep C
Deep C
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
# And ## operators in c
# And ## operators in c# And ## operators in c
# And ## operators in c
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
C Programming
C ProgrammingC Programming
C Programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Oop l2
Oop l2Oop l2
Oop l2
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 

Semelhante a 01 dll basics

DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building BlocksMax Kleiner
 
P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#Rainer Stropek
 
DLL Tutor maXbox starter28
DLL Tutor maXbox starter28DLL Tutor maXbox starter28
DLL Tutor maXbox starter28Max Kleiner
 
DEFCON 27 - ALEXANDRE BORGES - dot net malware threats
DEFCON 27 - ALEXANDRE BORGES - dot net malware threatsDEFCON 27 - ALEXANDRE BORGES - dot net malware threats
DEFCON 27 - ALEXANDRE BORGES - dot net malware threatsFelipe Prado
 
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!Synack
 
Formbook - In-depth malware analysis (Botconf 2018)
Formbook - In-depth malware analysis (Botconf 2018)Formbook - In-depth malware analysis (Botconf 2018)
Formbook - In-depth malware analysis (Botconf 2018)Rémi Jullian
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxsangeetaSS
 
01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programmingmaznabili
 
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019Alexandre Borges
 
AmiBroker AFL to DLL Conversion
AmiBroker  AFL to DLL ConversionAmiBroker  AFL to DLL Conversion
AmiBroker AFL to DLL Conversionafl2dll
 

Semelhante a 01 dll basics (20)

DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building Blocks
 
P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#
 
Visual programming
Visual programmingVisual programming
Visual programming
 
DLL Tutor maXbox starter28
DLL Tutor maXbox starter28DLL Tutor maXbox starter28
DLL Tutor maXbox starter28
 
Embedded C.pptx
Embedded C.pptxEmbedded C.pptx
Embedded C.pptx
 
CFInterop
CFInteropCFInterop
CFInterop
 
DEFCON 27 - ALEXANDRE BORGES - dot net malware threats
DEFCON 27 - ALEXANDRE BORGES - dot net malware threatsDEFCON 27 - ALEXANDRE BORGES - dot net malware threats
DEFCON 27 - ALEXANDRE BORGES - dot net malware threats
 
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!
DEF CON 23: 'DLL Hijacking' on OS X? #@%& Yeah!
 
Formbook - In-depth malware analysis (Botconf 2018)
Formbook - In-depth malware analysis (Botconf 2018)Formbook - In-depth malware analysis (Botconf 2018)
Formbook - In-depth malware analysis (Botconf 2018)
 
C structure
C structureC structure
C structure
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Srgoc dotnet_new
Srgoc dotnet_newSrgoc dotnet_new
Srgoc dotnet_new
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
 
01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programming
 
Introduction to Programming Lesson 01
Introduction to Programming Lesson 01Introduction to Programming Lesson 01
Introduction to Programming Lesson 01
 
Basics of building a blackfin application
Basics of building a blackfin applicationBasics of building a blackfin application
Basics of building a blackfin application
 
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019
.NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019
 
AmiBroker AFL to DLL Conversion
AmiBroker  AFL to DLL ConversionAmiBroker  AFL to DLL Conversion
AmiBroker AFL to DLL Conversion
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
 

Último

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Último (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

01 dll basics

  • 1. DLL Basics Reference: Programming Applications for Microsoft Windows Fourth Edition p.p. 675
  • 2.
  • 3.
  • 4.
  • 5. 要注意的事情 VOID EXEFunc() { PVOID pv = DLLFunc(); // Access the storage pointed to by pv... // Assumes that pv is in EXE's C/C++ run-time heap free(pv); } PVOID DLLFunc() { // Allocate block from DLL's // C/C++ run-time heap return(malloc(100)); } 使用你 DLL 的人可能 使用的是 static 版本的 free 在你的 DLL 中 , 使用 run-time 版本的 malloc 全部放在 DLL 中處理 VOID EXEFunc() { PVOID pv = DLLFunc(); // Access the storage pointed to by pv... // Assumes that pv is in EXE's C/C++ run-time heap DLLFreeFunc(pv); } PVOID DLLFunc() { // Allocate block from DLL's C/C++ run-time heap return(malloc(100)); } BOOL DLLFreeFunc(PVOID pv) { // Free block from DLL's C/C++ run-time heap return(free(pv)); } 比較好的策略 在 DLL 配置記憶體 , 在 DLL 釋放他
  • 6.
  • 7. DLL 是 如何被建立 的 以及如何被應用程式 implicitly link export 函式原型 , 結構 Symbol 的 header file 函式的實作 產生 obj 檔 組合成 DLL 檔並且 產生 lib 檔 DLL export 的部分 使用 DLL 的部分 Import 你要使用的函式 原型 呼叫 DLL 函式 產生 obj 結合 lib 解析出呼叫 的函式 symbol 執行的部分 Loader 建立 address space 給 .exe Loader 載入必要的 DLL 到 process 的 address space Lib 檔 dll 檔 exe 檔 COFF 格式 COFF 格式 =Common Object File Format
  • 8.
  • 9.
  • 10. 實作 export 函式 #include <windows.h> #define MYLIBExport #include &quot;..MyLib.h&quot; int g_nResult; int Add(int nLeft, int nRight) { g_nResult = nLeft + nRight; return(g_nResult); } Include 標準的 windows 及 C++ run-time Library 標頭檔 定義目前是 export Function 的 prototype 實作函式及 變數的地方 MyLibFile1.cpp #ifdef MYLIBExport #define MYLIBAPI extern &quot;C&quot; __declspec( dllexport ) #else #define MYLIBAPI extern &quot;C&quot; __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); MyLib.h
  • 11.
  • 12. 我們來看看差別在哪裡 ! #ifdef MYLIBExport #define MYLIBAPI extern “C” __declspec( dllexport ) #else #define MYLIBAPI extern &quot;C&quot; __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); 如果你的 MyLib.h 的內容如下 使用 “ C” 的方式命名 利用 dumpbin /exports 察看 MyDll.lib 結果 看到了嗎 Symbol 沒有加上 mangle 那請問一下 : 加上 extern “C” 後 , 你的 DLL 是否還支援 function overloading ?
  • 13. 如果你的 MyLib.h 的內容如下 #ifdef MYLIBExport #define MYLIBAPI extern __declspec( dllexport ) #else #define MYLIBAPI extern __declspec( dllimport ) #endif MYLIBAPI int g_nResult; MYLIBAPI int Add(int nLeft, int nRight); 沒有使用 “ C” 利用 dumpbin 察看 MyDll.lib 結果 看到了嗎 Symbol 後面加了一些料
  • 14. #include “MyLib.h” void main(){ int Sum=Add(1,2); } 好了 , 接下來 , 就是直接使用 function 若你是由 VC 幫你建立 DLL export symbol 骨架 , 那麼 預設值是不加上” C” 要注意 : 別弄錯了 , 否則會發生 link 錯誤
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20. 沒有加入 .DEF 檔 加入 .DEF 檔 LIBRARY MYDll EXPORTS Add dumpbin /exports MyDLL.dll dumpbin /exports MyDLL.dll 補充
  • 21.
  • 22.
  • 23.
  • 24. 當系統找不到 DLL 時 , 所發出的錯誤訊息 Step5: 檢查 所有模組的 import symbol 與 載入 DLL 的 export 是否一樣 Step6: 計算每個 symbol 的實際位址 將其放到 executable module 的 import section DLL base addr + RVA 事實上 , 是 update import section 中的 Imported Address Table (ITA) Reference: ms-help://MS.MSDNQTR.2004JAN.1033/dnmsj00/html/hood0200.htm
  • 25.
  • 26.
  • 27. 附錄 使用 Borland’s IMPLIB.exe 可直接將 COFF 的 library format 轉成 OMF 格式 IMPLIB (destination lib name) (source dll) 例如 : IMPLIB mydll.lib mydll.dll OMF: Object Module Format (BCB 使用 ) COFF: Common Object File Format (VC 使用 )