如何将C/C++程序转译成Delphi(十四)

原创 2001年07月24日 13:45:00

7.2. Dynamic Linking

Dynamic Linking is used to link the DLL at runtime.

Handling Static and Dynamic Linking in the Jedi Environment

Because both static and dynamic linking have their strengths, we must be prepared to support both techniques in the Jedi units. However, static linking is the default.

To support multiple methods compiler symbols are used. The two compiler symbols that symbolize dynamic linking are as follows:

If Xxxx_DYNLINK is defined the DLL must be linked dynamically at startup (in the initialization section).

If Xxxx_LINKONREQUEST is defined the DLL is linked dynamically, not at startup, but as needed by the user. Xxxx is the name of the API, e.g. MAPI_DYNLINK or MAPI_LINKONREQUEST.

If neither symbol is defined, static linking is used.

Each import unit should implement the following functions:

Function XxxxInitAPI: Boolean;

Procedure XxxxFreeAPI;

Function XxxxCheckAPI: Boolean;

The functions XxxxInitAPI and XxxxFreeAPI are available for the user if the symbol Xxxx_LINKONREQUEST is defined. The User can call this function to load or free the DLL. XxxxInitAPI returns TRUE, if the DLL has been loaded successfully.

If Xxxx_DYNLINK is defined, but not Xxxx_LINKONREQUEST, these functions are used internally to load or unload the DLL in the initialization section, but they are not available for the user.

The function XxxxCheckAPI returnes TRUE if the API is available, so the return value is TRUE if the DLL has previously been loaded successfully using XxxxInitAPI. When static linking is used, the return value is always TRUE.

Here is an example of how to handle the symbols in a translation:

unit apisample;
interface
USES Windows,
     JediUtil;
// There are two conditional defines. One to use dynamic
// run time linking with linking on startup, the other
// to link upon request. In fact the only difference is
// that the library is not loaded in the initialization
// section if the upon-request symbol is defined.
// Both symbols start with the name of the api, e.g. TAPI.
// In this example it is APISAMPLE.
// APISAMPLE_DYNLINK signals that the API should be linked
// via LoadLibrary and GetProcAddress at startup.
// APISAMPLE_LINKONREQUEST signals that the libary should
// not be linked at startup, but via an initialization procedure.
// Since most of the stuff is the same for both we use
// APISAMPLE_DYNLINK as common symbol.
{$IFDEF APISAMPLE_LINKONREQUEST}
  {$DEFINE APISAMPLE_DYNLINK}
{$ENDIF}
{$IFDEF APISAMPLE_DYNLINK}
// Define function types and variables for dynamic linking
TYPE
  TApiSampleFunc1 = Function (lParam: LongInt): LongInt; stdcall;
  TApiSampleFunc2 = Function (wParam: Word): LongInt; stdcall;

VAR
  ApiSampleFunc1 : TApiSampleFunc1 = NIL;
  ApiSampleFunc2 : TApiSampleFunc2 = NIL;
{$ELSE}
// We don't use dynamic linking so we implement static linking
Function ApiSampleFunc1 (lParam: LongInt): LongInt; stdcall;
Function ApiSampleFunc2 (wParam: Word): LongInt; stdcall;
{$ENDIF}
//
// Linking Control functions
//
// The XxxxInitAPI function follows <ApiName>InitAPI
// naming convention and is only visible if
// Xxxxx_LINKONREQUEST is defined. The same is true
// for the XxxxxFreeAPI function which frees the
// library
{$IFDEF APISAMPLE_LINKONREQUEST}

Function ApiSampleInitAPI: Boolean;
Procedure ApiSampleFreeAPI;
{$ENDIF}

// The XxxxxCheckAPI function returns true if
// the API is available. With static linking
// the function always returns TRUE
Function ApiSampleCheckAPI: Boolean;
implementation
CONST APISampleDLL = 'APISAMPLE.DLL'; // Name of the DLL
{$IFDEF APISAMPLE_DYNLINK}
VAR hDLL : THandle = 0; // Handle to the lib. Only req. for
                          dyn.link.
Function ApiSampleInitAPI: Boolean;
begin
  Result := FALSE;
  // Load library if necessary
  If hDLL = 0 then hDLL := LoadLibrary (APISampleDLL);
  If JediCheckInstanceHandle (hDLL) then
 begin
    // Set pointers to functions
    @ApiSampleFunc1 := GetProcAddress (hDLL, 'ApiSampleFunc1');
  @ApiSampleFunc2 := GetProcAddress (hDLL, 'ApiSampleFunc2');
    // Everything ok, return true
    Result := TRUE;
  end
end;
Procedure ApiSampleFreeAPI;
begin
  If hDLL <> 0 then
    FreeLibrary (hDLL);
  hDLL := 0;
end;
{$ELSE}
Function ApiSampleFunc1; external APISampleDLL name 'ApiSampleFunc1';
Function ApiSampleFunc2; external APISampleDLL name 'ApiSampleFunc1';
{$ENDIF}
Function ApiSampleCheckAPI: Boolean;
begin
  {$IFDEF APISAMPLE_DYNLINK}
  Result := hDLL <> 0;
  {$ELSE}
  Result := TRUE;
  {$ENDIF}
end;
initialization
begin
  {$IFDEF APISAMPLE_DYNLINK}
    {$IFNDEF APISAMPLE_LINKONREQUEST}
  // Call Init if dynamic linking and not link on request 
  ApiSampleInitAPI
    {$ENDIF}
  {$ENDIF}
end;
finalization
begin
 {$IFDEF APISAMPLE_DYNLINK}
  ApiSampleInitAPI;  // Call free if dynamic linking
  {$ENDIF}
end;
end.

You can use the the JediCheckInstanceHandle function from the common Jedi-support unit to check an instance handle if necessary.


Back to contents

8. The Jedi Common Support Unit

如何将C/C++程序转译成Delphi(五)

如何将C/C++程序转译成Delphi(五) 发表日期:2006-2-4 1. Anatomy of a C Header Back to content...
  • autumn20080101
  • autumn20080101
  • 2013年05月05日 14:54
  • 790

Delphi 与 C/C++ 数据类型对照表(转)

 原文地址:http://www.cnblogs.com/del/archive/2007/12/03/981563.html Delphi 数据类型 C/C++ ...
  • edisator
  • edisator
  • 2015年01月17日 11:15
  • 946

Delphi与C语言类型转换对照

 When converting C function prototypes to Pascal equivalent declarations, it's important to subst...
  • MaxWoods
  • MaxWoods
  • 2014年10月10日 14:31
  • 2073

UTC转换本地时间

//===================================================================== //TITLE: //    UTC转换本地时间 ...
  • nui111
  • nui111
  • 2015年05月11日 16:37
  • 708

Delphi 与 C/C++ 数据类型对照表

Delphi 数据类型 C/C++ ShorInt 8位有符号整数 char Byte 8位无符号整数 BYTE,unsigned sh...
  • rocklee
  • rocklee
  • 2015年09月24日 11:53
  • 3193

Delphi与C/C++ 数据类型的对应

原文地址:Delphi与C/C++ 数据类型对照关系 Delphi 数据类型 C/C++ ShorInt 8位有符号整数 char Byte 8位无符号整数...
  • shuiyuanfengxing
  • shuiyuanfengxing
  • 2013年01月05日 16:13
  • 3028

C/C++调用Delphi制作的dll时发现的一些问题

做了一个网络业务逻辑实体,是用C++编写的dll,内部有一个全局的业务实体,外部准备用Delphi编写的界面程序来控制并显示状态...... 1. C++的dll中不能用C++风格的导出方式:_dec...
  • gyymen
  • gyymen
  • 2016年12月09日 08:31
  • 1063

【整理】C/C++中字符串与整数之间的相互转换

C/C++中字符串与整数之间的相互转换 一、用C标准库中的sprintf()和sscanf()转换  二、用C++中的stringstream类转换 三、atoi、atof、atol、itoa等 总...
  • sherry_zs
  • sherry_zs
  • 2016年01月13日 20:23
  • 2561

Matlab转c与c++代码

作为一个强大的科学计算软件,matlab广泛运用于较多领域,以其简单的编程风格,便利的调试环境等等众多优点,在编写算法与测试的时候通常用到。然而很多的实际运用上matlab毕竟还不是很普及,相反还是c...
  • on2way
  • on2way
  • 2015年10月17日 15:36
  • 19056

将Python代码嵌入到C++程序

这篇文章主要介绍了将Python代码嵌入C++程序进行编写的实例,尽管通常还是Python代码中调用C++程序的情况较多...需要的朋友可以参考下 把python嵌入的C++里面需要做一些步...
  • u011854789
  • u011854789
  • 2016年06月19日 00:17
  • 1774
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何将C/C++程序转译成Delphi(十四)
举报原因:
原因补充:

(最多只允许输入30个字)