编写支持VCL的DLL的实例

时间:2007-10-30 09:01:46   来源:中国IT者收集整理   作者:佚名  编辑:gaopoadmin

学习一个实例,在C++中编写支持vlc的DLL程序,不是很难,大家跟着一步一步做就可以了.

第一步:建立dll

建立dll工程。如下图选择

确定后,保存工程名为usevcldll,新增一个about窗体,一个usevcl单元:usevcl.cpp/usevcl.h,

在usevcl.h中的代码如下:

 

#include <vcl.h>
#include <StdCtrls.hpp>
#include "about.h"
extern "C" __declspec(dllexport) int __stdcall GetAKSControlType();
extern "C" __declspec(dllexport) const char * __stdcall GetAKSControlName();
extern "C" __declspec(dllexport) void __stdcall TestSetEditValue(TEdit * aEdit);
extern "C" __declspec(dllexport) TForm * __stdcall GetAboutForm(TForm * aOwner);

在usevcl.cpp中的代码如下:

int __stdcall GetAKSControlType()
{
    return 1;
}
const char * __stdcall GetAKSControlName()
{
    return "AKSControl_Demo";
}

void __stdcall TestSetEditValue(TEdit * aEdit)
{
 aEdit->Text = "This is Set Edit Value!";
}
TForm * __stdcall GetAboutForm(TForm * aOwner)
{
    TAboutBox * frm = new TAboutBox(aOwner);
    return frm;
}

然后,编译即可。这时会生成usevcldll.dll和usevcldll.lib等文件。在这里我们使用的是动态装载dll,usevcldll.lib就暂时不用了。

第二步,动态调用dll

我们再新建一个vcl Forms Application程序,保存为testdll

然后在生入如下界面窗体

窗口定义如下:主界面头文件:unit2.h

class TForm2 : public TForm
{
__published: // IDE-managed Components
 TButton *Button1;
 TEdit *Edit1;
 TEdit *Edit2;
 TLabel *Label1;
 TLabel *Label2;
 TEdit *Edit3;
 void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public:  // User declarations
 __fastcall TForm2(TComponent* Owner);
};
 

代码文件:unit2.cpp

 

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

#include <vcl.h>
#pragma hdrstop

#include "XFrmMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
typedef int __stdcall (* FGetAKSControlType )();
typedef const char * __stdcall (*FGetASKControlName)();
typedef void __stdcall (*FTestSetEditValue)(TEdit * aEdit);
typedef TForm * __stdcall(*FGetAbout)(TForm * aOwner);

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
 : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)

关键字:编写,支持VCL,DLL的实例

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面