DLL动态调用碰倒问题!各位大侠,帮忙看看呀!!
贴一下代码:
[code=C/C++][/code]
[code=C/C++][/code]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i=0;
hInst = LoadLibrary("Project1.dll");
if(hInst!=NULL)
{
FARPROC P;
P=GetProcAddress(hInst,"fac");
if(NULL != P)
{
int __fastcall (*fac)(int a,int b);
fac = (int __fastcall (__cdecl*)(int a,int b))P;
i = fac(StrToInt(Edit1->Text),StrToInt(Edit2->Text));
ShowMessage("平方和是:" +AnsiString(i));
}
else
{
ShowMessage("Could not obtain function pointer!");
}
}
FreeLibrary(hInst);
}
DLL文档里只有一个函数:
[code=C/C++][/code]
#pragma argsused
extern "C" __declspec(dllexport) int fac(int a,int b);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------
int fac(int a,int b)
{
return pow(a,2)+pow(b,2);
}
为什么P的返回值是NULL呢?搞不懂呢!!
[解决办法]
既然你调用的时候指定的约定是fastcall,那么DLL中函数的声明和定义也指定成fastcall,这样才能匹配。如:
extern "C" __declspec(dllexport) int __fastcall fac(int a,int b);int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved){ return 1;}//---------------------------------------int __fastcall fac(int a,int b){ return pow(a,2)+pow(b,2);}