关闭

如何将动态库(DLL)中的类导出(二)

499人阅读 评论(0) 收藏 举报

EXE文件: FMakeDll.cpp

 

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

//使用静态调用,别忘了Project->Add to project...添加MakeDll.lib

 

#include <vcl.h>

#pragma hdrstop

#include "FTestDll.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

    : TForm(Owner)

{

    hMyDll = NULL;

}

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

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

    TestA(10);    

}

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

 

void __fastcall TForm1::Button2Click(TObject *Sender)

{

    if(hMyDll != NULL)

    {

     ShowMessage("已经为对象分配资源!");

     return;

    }

 

    hMyDll = InitMakeDll();

 

    if(hMyDll != NULL)

    {

     ShowMessage("获得对象句柄成功!");

    }

}

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

 

void __fastcall TForm1::Button3Click(TObject *Sender)

{

    int irtn;

 

    irtn = TestB(hMyDll,20);

 

    if(errOk == irtn)

    {

     ShowMessage("使用句柄执行测试函数B成功!");

    }

}

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

 

void __fastcall TForm1::Button4Click(TObject *Sender)

{

    int irtn;

 

    //这种情况下需要使用者在程序中自己控制

    if(hMyDll == NULL)

    {

     ShowMessage("对象还没有被创建或已经释放");

     return;

    }

 

    irtn = ReleaseMakeDll(hMyDll);

 

    if(errOk == irtn)

    {

     ShowMessage("使用句柄释放对象成功!");

    }

 

    hMyDll = NULL;

}

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

 

EXE文件: FTestDll.h

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

#ifndef FTestDllH

#define FTestDllH

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

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

//DLL的导出函数文件(上面已经介绍过)

#include "MakeDll.h"

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

class TForm1 : public TForm

{

__published:  // IDE-managed Components

    TButton *Button1;

    TButton *Button2;

    TButton *Button3;

    TButton *Button4;             

    void __fastcall Button2Click(TObject *Sender);

    void __fastcall Button1Click(TObject *Sender);

    void __fastcall Button3Click(TObject *Sender);

    void __fastcall Button4Click(TObject *Sender);

private: // User declarations

    //因为在整个窗体中需要使用hTestDll,定义在这里好了

    //从这里的声明根本不会看到有关原来的类的影子

    HANDLE hMyDll;

public:       // User declarations

    __fastcall TForm1(TComponent* Owner);

};

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

extern PACKAGE TForm1 *Form1;

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

#endif 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1342315次
    • 积分:20834
    • 等级:
    • 排名:第365名
    • 原创:810篇
    • 转载:12篇
    • 译文:1篇
    • 评论:227条
    最新评论
    ATL/WTL
    Big Deal
    Book