请教 c++builder xe2 动态调用dll方法
dll文件名是clntTransfer.dll
#include <vcl.h>
#include <windows.h>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <IdAntiFreezeBase.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdConnectThroughHttpProxy.hpp>
#include <IdCustomTransparentProxy.hpp>
#include <IdIOHandler.hpp>
#include <IdIOHandlerSocket.hpp>
#include <IdIOHandlerStack.hpp>
#include <IdSocks.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
#include <Vcl.ComCtrls.hpp>
#include <Vcl.IdAntiFreeze.hpp>
#include <Vcl.ExtCtrls.hpp>
#pragma hdrstop
extern "C" __declspec(dllexport) __stdcall int MyTest(int,int);
#pragma argsused
TIdTCPClient *UploadClient;
TIdIOHandlerStack *IdIOHandlerStack1;
TIdAntiFreeze *IdAntiFreeze1;
TIdConnectThroughHttpProxy *IdConnectThroughHttpProxy1;
TIdSocksInfo *IdSocksInfo1;
UnicodeString srvPort,srvIP;
TProgressBar *ProgressBar1;
//-----------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
__declspec(dllexport) __stdcall int MyTest(int n1,int n2)
{
int t;
t= n1*n2;
return t;
}
//==========================================================
dll调用;
int __stdcall (*MyTest)(int,int);
void __fastcall TfrClient::Button2Click(TObject *Sender)
{
HINSTANCE hdll;
AnsiString proname;
FARPROC pr;
UnicodeString dllflName=ExtractFilePath(Application->ExeName)+"clntTransfer.dll";
try {
if (hdll==NULL) {
hdll=LoadLibrary(L"clntTransfer.dll");
}
if (hdll!=NULL) {
proname="MyTest";
pr=GetProcAddress(hdll,"MyTest");
if (pr!=NULL) {
MyTest=(int __stdcall (__cdecl *)(int,int))pr;
ShowMessage(MyTest(10,20));
} else {
ShowMessage("打开函数错误。");
}
}else {
ShowMessage("打开动态链接库错误。");
}
} __finally {
FreeLibrary(hdll);
}
}
我在c++builder xe2 上做的。
在动态导入dll时候是成功的但是取函数地址时取不到出现“打开函数错误。”信息。
我在网上找了很多都是这种方法,难道xe2 例外吗?
请高手帮忙看看。
[解决办法]
DLL中:
函数声明:
extern "C" __declspec(dllexport) int __stdcall MyTest(int, int);