调用VC .dll的问题,求大大们帮看看
void __fastcall TForm1::Button1Click(TObject *Sender)
{
string a,b;
dllpath=ExtractFilePath(ParamStr(0))+"NEWDLL.dll";//路径dllpath头文件已定义String类
if(!FileExists(dllpath))//判断dll不存在
{Application->MessageBoxA("NEWDLL.dll不存在","提示",MB_OK);}
if(FileExists(dllpath))//判断dll存在
{
hInst=LoadLibrary(dllpath.c_str());//加载dll
if(hInst!=NULL)
{(FARPROC &)transfer2Pin=GetProcAddress(hInst,"transfer2Pin");
//取得dll中函数的地址
Edit1->Text=(AnsiString)a.c_str();
b=transfer2Pin(a);//调用这个转换函数
Edit2->Text=(AnsiString)b.c_str();}
}
FreeLibrary(hInst);
}
点RUN不报错,但是程序运行的时候会‘Access violation at address 00000000,read of address 00000000’ 调试了一下,发现这句
(FARPROC &)transfer2Pin=GetProcAddress(hInst,"transfer2Pin");
transfer2Pin的值是NULL,也就是这句出问题了,请大大们看看是怎么回事
附上dll文件的.h:
extern "C" _declspec(dllexport) string __stdcall transfer2Pin(string input);
dll文件的.cpp:
#include <string>
using namespace std;
#include "NEWDLL.h"
string __stdcall transfer2Pin(string input)
{...//太多省略,没有问题,编译通过运行也通过
}
[解决办法]
看看是不是确认相关的环境,缺少vc的dll之类文件。
你用vc写个调用的demo,看可否成功
[解决办法]
#include <windows.h>
#include <strsafe.h>
{
......
transfer2Pin=GetProcAddress(hInst,"transfer2Pin");
if (transfer2Pin == NULL )
ShowErrorMsg("Button1Click");
.....
}
void ShowErrorMsg(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER
[解决办法]
FORMAT_MESSAGE_FROM_SYSTEM
[解决办法]
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}