星期四, 三月 15, 2007

怎样在C++ Builder中创建使用DLL

动态链接库(DLL)是Windows编程常遇到的编程方法,下面我就介绍一下在BCB (C++ Builder下简称BCB) 中如何创建使用DLL和一些技巧。

  一、创建:
  使用BCB File|NEW建立一个新的DLL工程,并保存好文件BCB,生成一个DLL的程序框架。
  1.DllEntryPoint函数为一个入口方法,如果使用者在DLL被系统初始化或者注销时被调用,用来写入对DLL的初始化程序和卸载程序;参数:hinst用来指示DLL的基地址;reason用来指示DLL的调用方式,用于区别多线程单线程对DLL的调用、创建、卸载DLL;
  2.在程序中加入自己所要创建的DLL过程、函数;
  3.用dllimport描述出口;
  例程序如下:
Enter in the Conditionals defines field: __BUILDING_THE_DLL.
file.h

#ifdef __BUILDING_THE_DLL
#define _EXPORT_DLL __declspec(dllexport)
#else
#define _EXPORT_DLL __declspec(dllimport)
#endif

extern "C" _EXPORT_DLL bool __stdcall test(int n);


  unit.cpp
//---------------------------------------------------------------------------

#include
#include
#pragma hdrstop
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
#include "File1.h"
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
bool __stdcall test(int n)
{
if(n==1)
{
ShowMessage("ok");
return true;
}
else
ShowMessage("not ok");
return false;
}
  注意:动态链接库中调用过程、函数时有不同的CALL方式 __cdecl、__pascal, __fastcall、__stdcall,BCB中默认的方式为__cdecl(可不写),如果考虑兼容性可用时__stdcall声明方法为:
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=106&d=r3n13t
  extern "C" __declspec(dllexport) int __stdcall test();
  对于其中过程、函数也改为:
  int __stdcall test()

  二、使用DLL
  在BCB中使用DLL有两种方法:

  1.用静态调用法
  首先需要在BCB的项目中加入输入接口库(import library),打开工程项目,使用BCB View|Project Manager打开项目列表,向项目中加入接口库(*.lib)。
  其次在头文件中加入接口声明。
  例程序如下:
   //define in include file
   extern "C" __declspec(dllimport) int __cdecl test();
  //use function in main program
  int I;
  I=test();
  注意:
  (1)动态链接库调用过程、函数时CALL方式 与创建时方式一样不写为__cdecl,其它需要声明。
  (2)BCB创建的DLL有对应的输入接口库(import library),如只有DLL而无库时,可用BCB的implib工具产生:implib xxx.lib xxx.dll;另外可用:tlibxxx.lib,xxx.lst 产生DLL的内部函数列表,许多Windows的未公开技术就是用这种方法发现的。

  2.动态调用法
  动态调用法要用Windows API 中的LoadLibrary()和GetProcAddress()来调入DLL库,指出库中函数位置,这种方法较常见。
  例程序如下:
.h
typedef bool __stdcall (*test)(int n);

.cpp
test tDll;
HINSTANCE hins;
hins = LoadLibrary("Project2.dll");
if(hins != Null)
{
tDll = (test)GetProcAddress(hins,(LPCSTR)"test");
if(tDll)
{
if(tDll(1))
{
ShowMessage("work");
}
else
ShowMessage("work too");
}
else
ShowMessage("not work");
}
FreeLibrary(hins);


  三、注意: (not necessary)
  创建DLL时编译链接时注意设置Project Options。
  Packages标签:去除Builder with runtime packages检查框。
  Linker标签:去除Use dynamic RTL检查框。
  否则创建的DLL需要Runtime packages or Runtime library。

没有评论: