如何动态调用DLL?
DLL 只有一个函数 大致是这样 int P(int X); 我想要动态调用它该怎么声明?
我只知道静态是 extern "C" .....
在主函数中我又怎么样才能动态调用它呢?
最好把三个函数都用上 LoadLibrary GetProcAddress FreeLibrary
问题点数:20、回复次数:4Top
1 楼Persistent8813(固执的,坚持的:山东诸城)回复于 2004-09-04 07:59:34 得分 7
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit7_8.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
extern "C" __declspec(dllimport) void __fastcall CustomerForm(char *st);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
void (*CustomerForm)(char *);
HINSTANCE hInst=LoadLibrary("pDelphiDLL.dll");
(FARPROC &)CustomerForm=GetProcAddress(hInst,"CustomerForm");
CustomerForm(Edit1->Text.c_str());
FreeLibrary(hInst);
}
//---------------------------------------------------------------------------
Top
2 楼sunliwen780502(孙立文)回复于 2004-09-04 09:04:00 得分 7
typedef __declspec(dllimport) void __stdcall ExportType(TSaveDialog* dia, TQuickRep *Rep);
ExportType *LoadFunction;
HINSTANCE Dll = LoadLibrary("ExportDataDLL.dll");
if (Dll)
{
LoadFunction = (ExportType*)GetProcAddress(Dll, "ExportData");
if (LoadFunction)
LoadFunction(SaveDialog1, Form2->QuickRep1);
else
ShowMessage(SysErrorMessage(GetLastError()));
//free Library
FreeLibrary(Dll);
}
else
{
ShowMessage(SysErrorMessage(GetLastError()));
ShowMessage("Unable to load Dll");
}Top
3 楼weixing979(★★★闪电侠★★★)回复于 2004-09-04 09:49:25 得分 5
楼上两位的代码就可以了。不明白的话可以看下下面的文章
http://blog.csdn.net/behard/archive/2003/06/04/8681.aspx
Top
4 楼binbin(破坏分子)回复于 2004-09-04 13:32:16 得分 1
同意.Top




