200分咨询有关DLL的几个问题(分两次给分)
下面是kataboy给我的“调用DLL”的示范程序代码:
1。DLL.DLL的代码:
#include "YesNoUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TYesNofrm *YesNofrm;
//---------------------------------------------------------------------------
__fastcall TYesNofrm::TYesNofrm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
bool __stdcall YesNo()
{
bool bRet;
TYesNofrm *YesNofrm=new TYesNofrm(NULL);
YesNofrm->ShowModal();
bRet=YesNofrm->GetReturnValue();
delete YesNofrm;
return bRet;
}
bool TYesNofrm::GetReturnValue()
{
//TODO: Add your source code here
return returnvalue;
}
void __fastcall TYesNofrm::Button1Click(TObject *Sender)
{
returnvalue=true;
}
//---------------------------------------------------------------------------
void __fastcall TYesNofrm::Button2Click(TObject *Sender)
{
returnvalue=false;
}
2。动态调用的代码:
.h
typedef bool (dllYesNo)(void);
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *TESTButton;//★★可以被Dll.cpp用吗?
...........
.cpp
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE hdll;
bool bRet;
dllYesNo *YesNo;
hdll=LoadLibrary("dll.dll");
if (!hdll)
{
ShowMessage("没有找到动态连接库");
return;
}
YesNo=(dllYesNo*)GetProcAddress(hdll,"YesNo");
if (!YesNo)
{
ShowMessage("没找到函数!");
return;
}
-------------------------------------------------------
3。静态调用的代码:
TForm1 *Form1;
extern "C" __declspec(dllimport) bool __stdcall YesNo();
void __fastcall TForm1::Button1Click(TObject *Sender)
{
bool bRet;
bRet=YesNo();
if (bRet)
ShowMessage("Yes");
else
ShowMessage("No");
}
问题是:1.动态调用中的typedef bool (dllYesNo)(void);是什么?如果有参数是否可以这样写typedef bool (dllYesNo)(TObject * Sender, int num);?
2.静态调用的工程中要哪些设置?如何与DLL.DLL联系?
读了http://expert.csdn.net/Expert/topic/1171/1171316.xml?temp=.9920618
lingbin(林斌) :“你把.h文件#include到你的工程中,还有把.lib也Add to Project到你的工程中”
.h是否只要加"include path"即可?.lib如何Add to ?
3.如果在DLL.DLL的代码中要用主程序中的VCL或数据可以吗?如用TForm1->TESTButton;怎么用法?