dll初学者求教

olivertree 2003-02-27 10:07:56
拜托一定要帮我看看。:)
////////////////////////引入dll的两个文件
//Unit_successful.h文件
//---------------------------------------------------------------------------

#ifndef Unit_successfulH
#define Unit_successfulH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class Tfrm_suc : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall Tfrm_suc(TComponent* Owner);
};
__declspec(dllimport) bool InvokeYesNoDialog();
//---------------------------------------------------------------------------
extern PACKAGE Tfrm_suc *frm_suc;
//---------------------------------------------------------------------------
#endif






//Unit_successful.cpp文件
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit_successful.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tfrm_suc *frm_suc;

//---------------------------------------------------------------------------
__fastcall Tfrm_suc::Tfrm_suc(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall Tfrm_suc::Button1Click(TObject *Sender)
{
HINSTANCE DllInst = LoadLibrary("frm_dll.dll");
int _stdcall (*ddd)(void);
ddd=GetProcAddress(DllInst,"InvokeYesNoDialog()");
InvokeYesNoDialog();
FreeLibrary(DllInst);
//int a=test(9);
}
//---------------------------------------------------------------------------






///////////////创建dll的三个文件
//Unit_dll.cpp
//---------------------------------------------------------------------------

#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
//extern "C" __declspec(dllexport) int test(int a);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
extern "C" __declspec(dllexport) bool InvokeYesNoDialog();
//int test(int a)
//{
// return a;
//}

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







//DLLMAIN.h
//---------------------------------------------------------------------------

#ifndef DLLMAINH
#define DLLMAINH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TYesNoDialog : public TForm
{
__published: // IDE-managed Components
TButton *YesButton;
TButton *NoButton;
TLabel *LabelText;
void __fastcall YesButtonClick(TObject *Sender);
void __fastcall NoButtonClick(TObject *Sender);
private: // User declarations
bool returnValue;
public: // User declarations
__fastcall TYesNoDialog(TComponent* Owner);
bool __fastcall GetReturnValue();
};
//---------------------------------------------------------------------------
extern PACKAGE TYesNoDialog *YesNoDialog;
//---------------------------------------------------------------------------
#endif










//DLLMAIN.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "DLLMAIN.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TYesNoDialog *YesNoDialog;
//---------------------------------------------------------------------------
__fastcall TYesNoDialog::TYesNoDialog(TComponent* Owner)
: TForm(Owner)
{
returnValue = false;
}
//---------------------------------------------------------------------------
void __fastcall TYesNoDialog::YesButtonClick(TObject *Sender)
{
//
returnValue = true;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TYesNoDialog::NoButtonClick(TObject *Sender)
{
//
returnValue = false;
Close();
}
//---------------------------------------------------------------------------
bool __fastcall TYesNoDialog::GetReturnValue()
{
return returnValue;
}
//---------------------------------------------------------------------------
bool InvokeYesNoDialog()
{
bool returnValue;
TYesNoDialog *YesNoDialog = new TYesNoDialog(NULL);

YesNoDialog->ShowModal();
returnValue = YesNoDialog->GetReturnValue();
delete YesNoDialog;
return returnValue;
}
//---------------------------------------------------------------------------



另外我是在同一路径下建的。环境变量中(library)已经包含了他们所在的文件夹路径,但是仍然不能使用,除非我将DLLMAIN.cpp引入到调用的文件中.可是这样的话,跟普通的包含引用不是一样了吗?为什么我动态调用还是找不到呢?
我是在cbc的例子中找到这个的,初学dll。其实例子中只有DLLMAIN.cpp、DLLMAIN.h。创建dll时是没有窗体的,所以我单击右键建了一个frm。请问这样做对吗?听朋友说可以在dll中使用任何东西,就很向往,可是也有一点迷茫,因为不知道怎么做。我现在用的最多的就是TreeView,我不知道这些东西有哪些可以放在dll。总之,请指点一些在什么地方、何种情况下使用dll最好,这里先谢谢大家了!





...全文
48 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
猎人66 2003-02-27
  • 打赏
  • 举报
回复
ddd=GetProcAddress(DllInst,"InvokeYesNoDialog()");
估计应该是
ddd=(int (__stdcall *)())GetProcAddress(DllInst,"InvokeYesNoDialog");
?
watercelery 2003-02-27
  • 打赏
  • 举报
回复
dll,其实没有什么的,就象自己写的外部函数一样,事先写好,要用的时候调用么!
dragonhux 2003-02-27
  • 打赏
  • 举报
回复
还有,我没看懂
Test和InvokeYesNoDialog的意思,
我估计Test是你做测试用的!

在是,DLL一般先查找文件的当前路径,
其次就是System目录啦!
dragonhux 2003-02-27
  • 打赏
  • 举报
回复
修改后的程序:

////////////////////////引入dll的两个文件
//Unit_successful.h文件
//---------------------------------------------------------------------------

#ifndef Unit_successfulH
#define Unit_successfulH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class Tfrm_suc : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall Tfrm_suc(TComponent* Owner);
};
//__declspec(dllimport) long _stdcall InvokeYesNoDialog();
//---------------------------------------------------------------------------
extern PACKAGE Tfrm_suc *frm_suc;
//---------------------------------------------------------------------------
#endif






//Unit_successful.cpp文件
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit_successful.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tfrm_suc *frm_suc;

//---------------------------------------------------------------------------
__fastcall Tfrm_suc::Tfrm_suc(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall Tfrm_suc::Button1Click(TObject *Sender)
{
long __stdcall (*ddd)(void); //在这说明,与名称无关,只要保证类型相同

HINSTANCE DllInst = LoadLibrary("frm_dll.dll");
ddd=GetProcAddress(DllInst,"InvokeYesNoDialog()"); //修改部分
(*ddd)();
FreeLibrary(DllInst);
}
//---------------------------------------------------------------------------






///////////////创建dll的三个文件
//Unit_dll.cpp
//---------------------------------------------------------------------------

#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) long __stdcall InvokeYesNoDialog();

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







//DLLMAIN.h
//---------------------------------------------------------------------------

#ifndef DLLMAINH
#define DLLMAINH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TYesNoDialog : public TForm
{
__published: // IDE-managed Components
TButton *YesButton;
TButton *NoButton;
TLabel *LabelText;
void __fastcall YesButtonClick(TObject *Sender);
void __fastcall NoButtonClick(TObject *Sender);
private: // User declarations
bool returnValue;
public: // User declarations
__fastcall TYesNoDialog(TComponent* Owner);
bool __fastcall GetReturnValue();
};
//---------------------------------------------------------------------------
extern PACKAGE TYesNoDialog *YesNoDialog;
//---------------------------------------------------------------------------
#endif










//DLLMAIN.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "DLLMAIN.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TYesNoDialog *YesNoDialog;
//---------------------------------------------------------------------------
__fastcall TYesNoDialog::TYesNoDialog(TComponent* Owner)
: TForm(Owner)
{
returnValue = false;
}
//---------------------------------------------------------------------------
void __fastcall TYesNoDialog::YesButtonClick(TObject *Sender)
{
//
returnValue = true;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TYesNoDialog::NoButtonClick(TObject *Sender)
{
//
returnValue = false;
Close();
}
//---------------------------------------------------------------------------
bool __fastcall TYesNoDialog::GetReturnValue()
{
return returnValue;
}
//---------------------------------------------------------------------------
long __stdcall InvokeYesNoDialog()
{
long returnValue;
TYesNoDialog *YesNoDialog = new TYesNoDialog(NULL);

YesNoDialog->ShowModal();
returnValue = YesNoDialog->GetReturnValue();
delete YesNoDialog;
return returnValue;
}
//---------------------------------------------------------------------------

几点建议:
1、在程序中不要使用BOOL,使用long;
2、最好不要使用类,要用的话就最好做成COM;
3、你这使用是动态装入,还有静态装入的方式。

gfh21cn 2003-02-27
  • 打赏
  • 举报
回复
还有建议你使用__stdcall

extern "C" __declspec(dllexport) bool __stdcall InvokeYesNoDialog();
gfh21cn 2003-02-27
  • 打赏
  • 举报
回复
如果你想静态调用,你就要把头文件和LIB文件加到工程中,
便可以使用了
gfh21cn 2003-02-27
  • 打赏
  • 举报
回复
DLL中可以做任何普通程序可以做的事情
关键是这些窗体对象不是自动创建的
譬如说你在DLL中有一个窗体TForm
你要显示,你就必须动态创建
TForm *dlg=new TForm(NULL);
.....
delete dlg;

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧