• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 19次 | 进入软件测试论坛讨论

领测软件测试网

 

n         方法2:

它是将类导出,但必须要有LIB文件,这种方法是静态调用!简单

 

DLL文件:MainDll.cpp

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

 

#include <vcl.h>

#include <windows.h>

#pragma hdrstop

#pragma argsused

 

#include "MainDllTest.h"

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

 

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)

{

  return 1;

}

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

//下面是类的代码

TStaticClass::TStaticClass()

{

    FTimesCalled    = 0;

    m_AlreadySaidHi = false;

}

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

TStaticClass::~TStaticClass()

{

 

}

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

void __fastcall TStaticClass::SayHi(void)

{

    ShowMessage("调用DLL中类成功!");

    FTimesCalled++;

    m_AlreadySaidHi = true;

    ShowMessage("再试一次");

}

 

//下面是导出函数

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

void __stdcall Say(char *WhatToSay)

{

    ShowMessage("来自EXE调用DLL中的函数\n" + (String)WhatToSay);

}

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

 

DLL文件:MainDll.h

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

 

#ifndef MainDllTestH

#define MainDllTestH

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

//写个导出类

__declspec(dllexport) class TStaticClass

{

private:

  int FTimesCalled;

public:

  TStaticClass();

  ~TStaticClass();

  bool m_AlreadySaidHi;

  void __fastcall SayHi(void);

  __property int TimesCalled = {read = FTimesCalled};

};

 

//写个导出函数

extern "C"

{

  __declspec(dllexport) void __stdcall Say(char *WhatToSay);

}

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

#endif

 

EXE文件:CallingForm.cpp

 

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

 

#include <vcl.h>

#pragma hdrstop

 

#include "CallingForm.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

    : TForm(Owner)

{

    //新建个类,此类在DLL中定义

    StaticClass = new TStaticClass;

}

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

__fastcall TForm1::~TForm1()

{

    //释放

    delete StaticClass;

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

    //调用DLL的函数

    Say("静态加载函数成功");

}

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

void __fastcall TForm1::Button2Click(TObject *Sender)

{

    //加载DLL,得到DLL文件句柄

    HINSTANCE hInst = LoadLibrary("SimpleDll.dll");

 

    //加载成功?

    if (hInst)

    {

        //获得DLL函数的指针

        (FARPROC &)Say = GetProcAddress(hInst,"Say");

 

        //获得成功?

        if (Say)

        {

            Say("动态加载函数成功");

        }

        else

        {

            ShowMessage(SysErrorMessage(GetLastError()));

        }

 

        //释放DLL句柄

        FreeLibrary(hInst);

    }

    else

    {

        ShowMessage(SysErrorMessage(GetLastError()));

        ShowMessage("加载DLL失败");

    } 

}

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

void __fastcall TForm1::Button3Click(TObject *Sender)

{

    if (!StaticClass->m_AlreadySaidHi)

    {

        StaticClass->SayHi();

    }

    else

    {

        ShowMessage("你已经调用DLL中类第 " +

                   (AnsiString)StaticClass->TimesCalled + " 次.");

        StaticClass->SayHi();

    }

}

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

void __fastcall TForm1::Button4Click(TObject *Sender)

{

    StaticClass->SayHi() ;

}

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

 

EXE文件:CallingForm.h

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

 

#ifndef CallingFormH

#define CallingFormH

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

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

//注意加上DLL导出文件

#include "MainDllTest.h"

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

class TForm1 : public TForm

{

__published:  // IDE-managed Components

    TButton *Button1;

    TButton *Button2;

    TButton *Button3;

    TButton *Button4;

    void __fastcall Button1Click(TObject *Sender);

    void __fastcall Button2Click(TObject *Sender);

    void __fastcall Button3Click(TObject *Sender);

    void __fastcall Button4Click(TObject *Sender);

private: // User declarations

    //函数指针,指向DLL的导出函数

    void __stdcall (*Say)(char *WhatToSay);

 

    //类指针,此类在DLL中定义

    TStaticClass *StaticClass;

public:       // User declarations

    __fastcall TForm1(TComponent* Owner);

    __fastcall TForm1::~TForm1(); 

};

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

extern PACKAGE TForm1 *Form1;

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

#endif

 


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网