1 静态调用 DLL
使用 $BCB path\Bin\implibexe 生成 Lib 文件,加入到工程文件中
将该文件拷贝到当前目录,使用 implib MyDlllib MyDlldll 生成
// Unit1h // TForm1 定义
#include "DllFormh" // TDllFrm 定义
//---------------------------------------------------------------------------
__declspec(dllimport) class __stdcall MyDllClass {
public:
MyDllClass();
void CreateAForm();
TDllFrm DllMyForm;
};
extern "C" __declspec(dllimport) __stdcall void CreateFromFunct();
class TForm1 : public TForm{}
// Unit1cpp // TForm1 实现
void __fastcall TForm1::Button1Click(TObject Sender)
{ // 导出类实现,导出类只能使用静态方式调用
DllClass = new MyDllClass();
DllClass->CreateAForm()
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject Sender)
{ // 导出函数实现
CreateFromFunct();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject Sender, TCloseAction &Action)
{
delete DllClass;
}
2 动态调用 DLL
// Unit1h
class TForm1 : public TForm
{
private: // User declarations
void (__stdcall CreateFromFunct)();
}
// Unit1cpp // TForm1
HINSTANCE DLLInst = NULL;
void __fastcall TForm1::Button2Click(TObject Sender)
{
if( NULL == DLLInst ) DLLInst = LoadLibrary("DLLdll"); //上面的 Dll
if (DLLInst) {
CreateFromFunct = (void (__stdcall)()) GetProcAddress(DLLInst,
"CreateFromFunct");
if (CreateFromFunct) CreateFromFunct();
else ShowMessage("Could not obtain function pointer");
}
else ShowMessage("Could not load DLLdll");
}
void __fastcall TForm1::FormClose(TObject Sender, TCloseAction &Action)
{
if ( DLLInst ) FreeLibrary (DLLInst);
}
3 DLL 作为 MDIChild (子窗体) 只编写动态调用的例子
实际上,调用子窗体的 DLL 时,系统只是检查应用程序的 MainForm 是否为 fsMDIForm 的窗体,这样只要把调用程序的 Application 的 Handle 传递给 DLL 的 Application 即可;同时退出 DLL 时也要恢复
// MDIChildProcpp // Dll 实现 CPP
#include "unit1h" // TForm1 定义
TApplication SaveApp = NULL;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void)
{
if ( (reason==DLL_PROCESS_DETACH) && SaveApp )
Application = SaveApp ; // 恢复 Application
return 1;
}
extern "C" __declspec(dllexport) __stdcall void TestMDIChild(
//1024X768
TApplication mainApp,
LPSTR lpCaption)
{
if ( NULL == SaveApp ) // 保存 Application,传递 Application
{
SaveApp = Application;
Application = mainApp;
}
// lpCaption 为子窗体的 Caption
TForm1 Form1 = new TForm1 ( Application, lpCaption );
Form1->Show();
}
注:上面的程序使用 BCB 30 编译成功
4 BCB 调用 VC 编写的 DLL
1 名字分解:
没有名字分解的函数
TestFunction1 // __cdecl calling convention
@TestFunction2 // __fastcall calling convention
TESTFUNCTION3 // __pascal calling convention
TestFunction4 // __stdcall calling convention
有名字分解的函数
@TestFunction1$QV // __cdecl calling convention
@TestFunction2$qv // __fastcall calling convention
TESTFUNCTION3$qqrv // __apscal calling convention
@TestFunction4$qqrv // __stdcall calling convention<br
你试一下:环境配置问题,Tools->Debugger Options->Event Log页选中Windows Messages的缘故。某些杀毒软件(瑞星主动防御)也可引起这种问题。希望对你有用,如果解决了问题请采纳。
以上就是关于win7 下BCB调用 BCB编写的DLL全部的内容,包括:win7 下BCB调用 BCB编写的DLL、BCB中,程序复位不到,说无法创建文件InitCC32.exe、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)