动态调用dll不成功
想学着动态调用dll,写了个简单的例子 ,可是GetProcAddress出错了 具体代码如下,请帮我看看有什么错,
dll中
extern "C" __declspec(dllexport) AnsiString DealPassword(AnsiString mm);
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
AnsiString DealPassword(AnsiString mm) //想试写一个简单字符串加密,还没有测试
{ //结果GetProcAddress一直不成功 汗
AnsiString new_mm = "";
//char s = '';
for(int i = 1; i <= mm.Length() ; i++)
{
new_mm += (mm[i] + 9) * 234;
}
return new_mm;
}
typedef AnsiString (*DealPassword)(AnsiString mm);
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm3::rzbtbtn1Click(TObject *Sender)
{
AnsiString s,r;
HINSTANCE dd;
AnsiString aa = Form2->FileLoc + "\\DealUserData.dll" ;
dd = LoadLibrary("DealUserData.dll");
if(dd)
{
DealPassword deal = (DealPassword)GetProcAddress(dd,"DealPassword");
if(deal) //这是deal始终是Null
{
s = deal(edt2->Text);
ShowMessage(s);
}
else
{
ShowMessage("GetProcAddress错误");
}
}
else
{
ShowMessage("LoadLibrary错误");
}
}
extern "C" __declspec(dllexport) void __stdcall DealPassword(LPSTR mm);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
void __stdcall DealPassword(LPSTR mm)
{
size_t uLen = strlen(mm);
for (size_t i = 0; i < uLen; i++)
{
// 算法代码略过
// ...
}
}
typedef void __stdcall (*DEALPASSWORD)(LPSTR mm);
void __fastcall TForm3::rzbtbtn1Click(TObject *Sender)
{
AnsiString strDllFile = Form2->FileLoc + "\\DealUserData.dll" ;
if (!FileExists(strDllFile ))
{
ShowMessage("DLL文件没找到,请检查文件路径名!");
return;
}
HINSTANCE h = ::LoadLibraryA(strDllFile .c_str());
if (h)
{
DEALPASSWORD DealPassword = (DEALPASSWORD)
::GetProcAddress(h, "DealPassword");
if (DealPassword )
{
char *lp = new char[edt2->Text.Length() + 1];
strcpy(lp, edt2->Text.c_str());
lp[edt2->Text.Length()] = 0x0;
deal(lp);
ShowMessage(lp);
delete []lp;
::FreeLibrary(h);
}
else
{
ShowMessage("GetProcAddress错误");
}
}
else
{
ShowMessage("LoadLibrary错误");
}
}