南山南古筝伴奏曲:如何向含有窗口的DLL传递参数并在窗口上显示?

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 00:44:04
Exe文件要调用含有窗口的Dll文件,此时Exe文件如何向该DLL文件传递一个参数,并在窗口上显示出来?
最好能给个代码示范一下。正确的话即可得50分!

用C++ Builder写DLL的一个例子:
------------------------------------------
//-***-Form: Unit2.h--
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
TLabel *lbl1;
TButton *btn1;
void __fastcall btn1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------

//***===Unit2.cpp==
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------
void __fastcall TForm2::btn1Click(TObject *Sender)
{
this->Close();
}
//---------
//==*****==DllMain: Unit1.cpp===
#include <vcl.h>
#include <windows.h>
#include "unit2.h"

extern "C" _declspec(dllexport) void __stdcall DlgTest(const char*); //Dll导出函数声明
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//----------------------------------------------
void __stdcall DlgTest(const char* s) { //Dll导出函数
TForm2 *form = new TForm2(NULL);
form->lbl1->Caption = s ; //设置Label控件的Caption,即窗体上的文字。
form->ShowModal() ;
delete form;
}

////////////////////////////////////////////
DLL 测试部分:(Dll文件名为:Project1.dll
//-------------
#include "windows.h"

int main() {
HINSTANCE h = LoadLibrary("Project1.dll");
typedef void (__stdcall MyFunc)(const char*);
MyFunc* Func = (MyFunc*)::GetProcAddress(h,"DlgTest");
Func("Hello,World"); // 调用Dll函数!!
FreeLibrary(h);
return 0;
}

///////////////////////////////////////////
这个可以实现你要的功能
用VC++也可以实现,原理一样,不过用C++Builder写的较简单些。
//////////////
要完整源代码的加我QQ: 309928903
//////////////////////////////////////
呵呵

dll中写一个函数,通过函数传递参数和打开窗体

不用那么麻烦吧
好象你只要调用dll时,看你调用的函数有没提供型参
然后把实参代入拿型参就可以传递过去了吧

Delphi4.0 & Visual FoxPro6.0
Windows98
我在Delphi中编制一DLL,想在FOXPRO中调用:
在Delphi中定义两个export函数:
setcaption(S:PChar);
begin
Form1.label1.Caption:=String(S);
end;

getcaption:PChar;
begin
Result:=PChar(Form1.label1Caption)
end;
在FoxPro中调时用getcaption完全可获得其返回的值,而用setcaption时传递过去的却是空值,不知为何?在FoxPro中我是这样注册两函数的:
DECLARE setcaption IN xxx.dll String S
DECLARE String getcaption IN xxx.dll