BCB实现DLL简单demo

                                    

       主要分为两个部分:创建DLL文件,调用DLL文件。

Note: BCB运行环境:2010 C++ Builder XE

      

  1. 创建DLL文件

Step1 创建DLL Project

a.1双击BCB软件,接着依次选择:File→New→Other,跳出如下dialoguebox:

a.2或者在右侧点击,add new project,跳出如下dialoguebox:

b. 选择“Dynamic link Library”,跳出如下dialoguebox:

c. 点击“OK”(如果只是在BCB中用的话,按default选择Use VCL即可,否则可选择Multi Treaded),点击“OK”;

d. 可得到如下Project , 实现代码可在“File1.cpp”添加

      

eg:// 添加相加,相乘功能 如下实现代码:

      

// @ 调用的函数格式: int __stdcall Summary (int a, int b)

// @ duedate:20211009

extern "C" __declspec(dllexport) int __stdcall Summary(int a, int b) //导出函数

{

              return a+b;

}

// @ 调用的函数格式: int __stdcall Multiplication (int a, int b)

// @ duedate:20211009

extern "C" __declspec(dllexport) long __stdcall Multiplication(int a, int b) //导出函数

{

              return a*b;

}

f. 选择: File→Save all , 选择相应的path(此处project name: DLL_Demo)

g. 编译,跳出以下dialoguebox,点击“OK”忽略,在Debug/Win32下则生成DLL,结束。

       此处的DLL_Demo.dll即为实现(相加、相乘)的DLL,

  1. 动态调用DLL 文件

实现效果如下,输入X/Y值,Summary(相加)、Multiplication(相乘)

代码实现:

HINSTANCE Hdll;

// @ brief :实现相加功能

// @ call by: DLL_Demo.dll

// @ para : none

// @ duedate: 20211009

// @ other : LoadLibrary/FreeLibrary/GetProcAddress

void __fastcall TForm2::Button1Click(TObject *Sender)

{

        int a=0,b=0;

        int __stdcall (*Calc)(int,int);  //定义函数原型

        // 加载动态库

        try

        {

              Hdll = LoadLibrary(L"DLL_Demo.dll");// 实现summary计算

        }

        catch(Exception &Ex)

        {

              ShowMessage(Ex.Message);

              return ;

        }

        a = StrToInt(Edit_x->Text.Trim());

        b = StrToInt(Edit_y->Text.Trim());

        if(Hdll != NULL)

        {

                 Calc = (int __stdcall (*)(int,int))::GetProcAddress(Hdll,"Summary"); //获取函数入口地址

              if(Calc != NULL)

              {

                     Edit_summary->Text = Calc(a,b);

               }

              else

              {

                     ShowMessage("不能找到函数入口!");

              }

               FreeLibrary(Hdll); //一定不要忘记调用完毕后释放DLL

        }

        else

       {

              ShowMessage("不能载入DLL!");

       }

}

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

// @ brief :实现相乘功能

// @ call by: DLL_Demo.dll

// @ para : none

// @ duedate: 20211009

// @ other : LoadLibrary/FreeLibrary/GetProcAddress

void __fastcall TForm2::Button_MultiplicationClick(TObject *Sender)

{

       // Edit_Multiplication

      

        int a=0,b=0;

        long __stdcall (*Calc)(int,int);  //定义函数原型

        // 加载动态库

        try

        {

              Hdll = LoadLibrary(L"DLL_Demo.dll");// 实现summary计算

        }

        catch(Exception &Ex)

        {

              ShowMessage(Ex.Message);

              return ;

        }

        a = StrToInt(Edit_x->Text.Trim());

        b = StrToInt(Edit_y->Text.Trim());

        if(Hdll != NULL)

        {

                 Calc = (long __stdcall (*)(int,int))::GetProcAddress(Hdll,"Multiplication"); //获取函数入口地址

              if(Calc != NULL)

              {

                     Edit_Multiplication->Text = Calc(a,b); // 实际调用函数

               }

              else

              {

                     ShowMessage("不能找到函数入口!");

              }

               FreeLibrary(Hdll); //一定不要忘记调用完毕后释放DLL

        }

        else

       {

              ShowMessage("不能载入DLL!");

       }

}

//-------------------------end--------------------------------------------

// File1.cpp             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;
}
//---------------------------------------------------------------------------

// @ 调用的函数格式: int __stdcall Summary(int a, int b)
// @ duedate:20211009
extern "C" __declspec(dllexport) int __stdcall Summary(int a, int b) //导出函数
{
		return a+b;
}

// @ 调用的函数格式: int __stdcall Multiplication(int a, int b)
// @ duedate:20211009
extern "C" __declspec(dllexport) long __stdcall Multiplication(int a, int b) //导出函数
{
		return a*b;




// Call_summary.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Call_summary.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;

HINSTANCE Hdll;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// @ brief :实现相加功能
// @ call by: DLL_Demo.dll
// @ para : none
// @ duedate: 20211009
// @ other : LoadLibrary/FreeLibrary/GetProcAddress
void __fastcall TForm2::Button1Click(TObject *Sender)
{
	 int a=0,b=0;

	 int __stdcall (*Calc)(int,int);  //定义函数原型

	 // 加载动态库
	 try
	 {
		Hdll = LoadLibrary(L"DLL_Demo.dll");// 实现summary计算
	 }
	 catch(Exception &Ex)
	 {
		ShowMessage(Ex.Message);
		return ;
	 }

	 a = StrToInt(Edit_x->Text.Trim());
	 b = StrToInt(Edit_y->Text.Trim());

	 if(Hdll != NULL)
	 {
		   Calc = (int __stdcall (*)(int,int))::GetProcAddress(Hdll,"Summary"); //获取函数入口地址
		if(Calc != NULL)
		{
			Edit_summary->Text = Calc(a,b);
		 }
		else
		{
			ShowMessage("不能找到函数入口!");
		}

		 FreeLibrary(Hdll); //一定不要忘记调用完毕后释放DLL
	 }
	 else
	{
		ShowMessage("不能载入DLL!");
	}

}
//---------------------------------------------------------------------------
// @ brief :实现相乘功能
// @ call by: DLL_Demo.dll
// @ para : none
// @ duedate: 20211009
// @ other : LoadLibrary/FreeLibrary/GetProcAddress
void __fastcall TForm2::Button_MultiplicationClick(TObject *Sender)
{
	// Edit_Multiplication
	
	 int a=0,b=0;

	 long __stdcall (*Calc)(int,int);  //定义函数原型

	 // 加载动态库
	 try
	 {
		Hdll = LoadLibrary(L"DLL_Demo.dll");// 实现summary计算
	 }
	 catch(Exception &Ex)
	 {
		ShowMessage(Ex.Message);
		return ;
	 }

	 a = StrToInt(Edit_x->Text.Trim());
	 b = StrToInt(Edit_y->Text.Trim());

	 if(Hdll != NULL)
	 {
		   Calc = (long __stdcall (*)(int,int))::GetProcAddress(Hdll,"Multiplication"); //获取函数入口地址
		if(Calc != NULL)
		{
			Edit_Multiplication->Text = Calc(a,b); // 实际调用函数
		 }
		else
		{
			ShowMessage("不能找到函数入口!");
		}

		 FreeLibrary(Hdll); //一定不要忘记调用完毕后释放DLL
	 }
	 else
	{
		ShowMessage("不能载入DLL!");
	}
}
//---------------------------------------------------------------------------


//  Call_summary.h
//---------------------------------------------------------------------------

#ifndef Call_summaryH
#define Call_summaryH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:	// IDE-managed Components
	TEdit *Edit_x;
	TEdit *Edit_y;
	TLabel *Label1;
	TLabel *Label2;
	TEdit *Edit_summary;
	TButton *Button1;
	TButton *Button_Multiplication;
	TEdit *Edit_Multiplication;
	void __fastcall Button1Click(TObject *Sender);
	void __fastcall Button_MultiplicationClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
	__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论

打赏作者

weixin_42682768

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值