dll初学者求教
拜托一定要帮我看看。:)
////////////////////////引入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最好,这里先谢谢大家了!