⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 149.htm

📁 水木清华的BBS文章
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://bbs.tsinghua.edu.cn"><font face="黑体"><big><big>水木清华★</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">         Delphi编程                            (BM: strayli FlyingBoy)          </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="454"> <p align="center">[<a href="index.htm">回到开始</a>][<a href="7.htm">上一层</a>][<a href="150.htm">下一篇</a>]
<hr><p align="left"><small>发信人: strayli (stray), 信区: Delphi <br>

标  题: Importing or "Wrapping" DLL Function Calls <br>

发信站: BBS 水木清华站 (Tue Sep 22 18:22:58 1998) <br>

  <br>

Importing or &quot;wrapping&quot; dll function calls <br>

  <br>

Two methods exist for importing or loading functions <br>

from a Dynamic Link Library(DLL). The first method <br>

(which is discussed extensively in this document) is <br>

called &quot;implicit&quot; loading. Implicit loading involves <br>

loading the DLL statically at startup and accessing <br>

the functions through an Object PASCAL interface. <br>

This method should be used when an application is totally <br>

dependent upon the loading of a DLL for proper functioning. <br>

The other method available is referred to as &quot;explicit&quot; <br>

loading because the DLL is loaded dynamically on demand. <br>

This method requires a little more coding and should be <br>

used if the application needs to run even if the DLL fails <br>

to load properly. <br>

  <br>

What is a &quot;wrapped&quot; function call? <br>

  <br>

A wrapped function or set of functions consists <br>



of an entry into the interface section and an <br>

entry into the implementation section (along <br>

with associated constants or types) which corresponds <br>

to a function or set of functions to be imported from <br>

a DLL.  A wrapper is simply a declaration in a PASCAL <br>

unit that provides an entry Point into a DLL. With Delphi, <br>

this wrapper is represented by a unit file containing <br>

object pascal code. The Delphi development team has <br>

already conveniently wrapped many standard Windows <br>

controls and functions for you. On occasion it may become <br>

necessary to create wrappers for dll function calls <br>

that are not already wrapped in Delphi. <br>

  <br>

The first and often the most difficult step in this <br>

process is locating information about the function(s). <br>

One of the best sources for locating documentation of <br>

the function(s) in question is the World Wide Web. <br>

An extensive search beginning with the MSDN and <br>

extending to the numerous available search engines will <br>

often reveal some pertinent information.Search C++ header <br>

files in a product like Borland C++ or MS Visual C++ to <br>

get the structure of the function calls. Type conversion <br>

get the structure of the function calls. Type conversion <br>

and calling conventions are generally able to be resolved <br>

when converting between C++ and PASCAL. A good resource <br>

for compatibility between Delphi and C++ can be found <br>

on the Borland web site at: <br>

&quot;http://www.borland.com/delphi/papers/brick.html&quot;. <br>

  <br>

After locating an example or documentation of the DLL in <br>

question the next step is to create a new unit file. The <br>

interface of the unit will contain the types and constants <br>

that are specific to the function calls of the DLL along <br>

with the function headers. These function headers are the <br>

Object PASCAL interface that is being provided for other <br>

Delphi applications to call the DLL function. Once <br>

the interface section of the unit is complete the next <br>

section is the implementation. The implementation section <br>

of the unit contains declarations of the imported external <br>

functions. These headers are not identical to those found in <br>

the interface section of the unit (these contain the actual <br>

function identifiers plus other important implementation <br>

information).  For a more thorough treatment of this subject <br>

see the help topic &quot;DLLs:accessing procedures and functions&quot; <br>

in the Delphi 3 help file. <br>



  <br>

For example, say there exists a function called BOB in a DLL <br>

called 'BLODGE.DLL'.  (detailed below are the steps required to <br>

implicitly load the DLL) <br>

  <br>

1) WWW Research on the function BOB reveals that it returns a <br>

boolean and takes a word and a boolean as it's arguments. <br>

  <br>

2) Create a new unit file named 'UseBob.pas' via Delphi <br>

(File|New and choose unit) <br>

  <br>

3) The following line of code would go in the interface section <br>

of the new unit: <br>

  <br>

function BOB(Fire: Word; Dances: Boolean): Boolean; stdcall; <br>

  <br>

4) The following line of code would go in the implementation <br>

section of the new unit: <br>

  <br>

function BOB; external 'BLODGE'; <br>

  <br>

5) Save the unit, name it 'UseBob.pas'. <br>

5) Save the unit, name it 'UseBob.pas'. <br>

  <br>

6) It is also necessary to make sure that UseBob.pas <br>

is in the same directory of the current project or that it <br>

resides in a directory that is in the Delphi search path. <br>

  <br>

7) Add the 'UseBob' unit to a new project's uses clause. The <br>

function BOB can be called from this new project just like any <br>

other standard function. <br>

  <br>

8)  At runtime  BLODGE.DLL must be in the path of the current <br>

process's environment. <br>

  <br>

The steps required to explicitly load the 'BLODGE.DLL' are <br>

slightly different and involve a bit of coding.  As before a <br>

knowledge of the function/procedure arguments is necessary <br>

(and a result type if it's a function). <br>

  <br>

What follows is a unit that implements the BOB function call <br>

on a button click event: <br>

  <br>

unit UDLLTest; <br>

  <br>

  <br>

interface <br>

  <br>

uses <br>

  Windows, Messages, SysUtils, Classes, Graphics, Controls, <br>

  Forms, Dialogs, StdCtrls; <br>

  <br>

type <br>

  TForm1 = class(TForm) <br>

    Button1: TButton; <br>

    procedure Button1Click(Sender: TObject); <br>

  private <br>

    { Private declarations } <br>

  public <br>

    { Public declarations } <br>

  end; <br>

  <br>

{ Here's a type which points to our bob function } <br>

  TBOB = function(Fire: Word; Dances: Boolean): Boolean; stdcall; <br>

  <br>

var <br>

  Form1: TForm1; <br>

  <br>

  <br>

implementation <br>

  <br>

{$R *.DFM} <br>

  <br>

procedure TForm1.Button1Click(Sender: TObject); <br>

var <br>

  BOB: TBOB; <br>

  hDLLInst: THandle; <br>

  IsAlive,IsDancing: Boolean; <br>

  Years: Word; <br>

  <br>

begin <br>

  { Load a handle to our BLODGE.DLL } <br>

  hDLLInst:= LoadLibrary('BLODGE.DLL'); <br>

  { If the load was unsuccessful raise a custom exception } <br>

  if (hDLLInst &lt;= 0) then <br>

    raise exception.create('[LoadLibrary Fail]'); <br>

  { Try to load the address of the BOB function } <br>

  try <br>

    @BOB:= GetProcAddress(hDLLInst,'BOB'); <br>

    if not assigned(BOB) then <br>

      raise exception.Create('[GetProcAddress Fail]'); <br>



    Years:= 25; <br>

    IsDancing:= True; <br>

  { Now we can execute the BOB function } <br>

    IsAlive:= BOB(Years,IsDancing); <br>

  finally <br>

  { Free the handle to the DLL } <br>

    FreeLibrary(hDLLInst); <br>

  end; <br>

end; <br>

end. <br>

  <br>

-- <br>

※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 210.45.208.4] <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="7.htm">上一层</a>][<a href="150.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -