静态使用DLL动态连接库

一、首先创建一个DLL工程。

1)  bcb集成环境中,通过菜单File | new | other打开new Items对话框,选中DLL Wizar项,选中C++,选中Use VCL,确定。系统会自动创建一个DLL工程,内容如下:

    //---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#include <vcl.h>

#include <windows.h>

#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

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)

{

        return 1;

}

//---------------------------------------------------------------------------

 

2)  现在可以通过菜单Project | Build Project1来编译和连接当前的DLL工程。尽管还没有添加任何一句代码,DLL文件仍然可以顺利创建。但这里我们要继续进行下去。新建一个目录: D:/samples/mydll,保存资源文件为mydllprj.cpp,保存工程文件为mydllprj.bpr

3)  新建的DLL中包含两个函数,分别实现了两个整数的加法运算和减法运算,具体代码如下:

//---------------------------------------------------------------------------

 

#include <vcl.h>

#include <windows.h>

#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   int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)

{

        return 1;

}

//往下为新添加代码

extern "C" __declspec(dllexport) int PlusNum(int X,int Y)

{

  return X+Y;

}

extern "C" __declspec(dllexport) int MinusNum(int X,int Y)

{

  return X-Y;

}

//---------------------------------------------------------------------------

4)  开始编译和连接工程,编译和连接成功后在D:/samples/mydll中可见到mydllprj.dllmydllprj.lib

二、建立客户应用程序,调用mydllprj.dll

1)  通过菜单File | Close all 关闭所有工程和文件,然后通过菜单File |new Application 打开一个新的工程,将资源和工程文件保存在D:/samples,分别命名为mysmp.cppmysmpdll.bpr

2)  设计窗体界面如下:

   调整好后如下:

3)  ComboBox1Items属性中依次加入:两数相加两数相减,并将Style属性设置为csDropDownList

4)  mysmp.h中的窗体类Tform1声明之前添加下面的代码:

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

//新加代码

extern "C" __declspec(dllimport) int PlusNum(int X,int Y);

extern "C" __declspec(dllimport) int MinusNum(int X,int Y);

//extern "C" __declspec(dllimport) int MultiplyNum(int X,int Y);

//新加代码结束

//---------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:    // IDE-managed Components

        TLabel *Label1;

        TLabel *Label2;

        TLabel *Label3;

5)  添加Button1控件的OnClick事件的处理代码如下:

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 if(ComboBox1->Text=="两数相加")

   Edit3->Text=IntToStr(PlusNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

 else if(ComboBox1->Text=="两数相减")

   Edit3->Text=IntToStr(MinusNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

// else if(ComboBox1->Text=="两数相乘")

  // Edit3->Text=IntToStr(MultiplyNum(StrToInt(Edit1->Text),StrToInt(Edit2->Text)));

 else

   MessageBox(Form1->Handle,"请指定一个计算方法。","警告",MB_OK|MB_ICONWARNING);

}

//---------------------------------------------------------------------------

6)  现在编译连接和运行程序,程序会提示错误信息,错误信息如下:

  [Linker Error] Unresolved external '_PlusNum' referenced from D:/SAMPLES/MYSMP.OBJ

  [Linker Error] Unresolved external '_MinusNum' referenced from D:/SAMPLES/MYSMP.OBJ

这是因为编译器没有找到开始生成的 mydllprj.dll 这个连接库文件,需要在 bcb 集成开发环境中加入,通过菜单 Project | add to Project ,把 d:/samples/mydll/mydllprj.lib 添加进来。然后编译连接并运行,这时系统会提示 找不到 mydllprj.dll 运行库 ,这是因为 mydllprj.dll d:/samples/mydll/ ,系统按指定的路径找不到这个文件,我们这里可以把 mydllprj.dll 复制到 d:/samples 下,然后编译运行,这是你就可以看到期待已久的界面了。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值