本人用vs2010根据网上教程编写了一个调用DLL的小程序,但是出现了0x00a163b8 处有未经处理的异常: 0xC0000005: Access violation的异常。调试模式的位置是 mainret = main(argc, argv, envp);请各位指出问题所在。代码如下:
#include
#include
using namespace std;
int main(){
typedef int (*OpenComPort)(int,unsigned char *,unsigned char,int *);
typedef int (*GetReaderInformation)(unsigned char *, unsigned char *, unsigned char *, unsigned char *,unsigned char *, unsigned char *, unsigned char *,unsigned char *, int);
typedef int (*SetWorkMode)(unsigned char *, unsigned char * , int );
typedef int (*SetRelay)(unsigned char *,unsigned char *, unsigned char *,int );
typedef int (*Inventory_G2)(unsigned char *, unsigned char , unsigned char , unsigned char , unsigned char ,unsigned char ,unsigned char *, int * , int *,int);
typedef int (*CloseComPort)(void);
typedef int (*CloseSpecComPort)(int);
int Port = 3;
unsigned char ComAdr = 0xFF;
unsigned char Baud = 5;//对应的波特率见说明文件
int handle = 0;
unsigned char VersionInfo[2] = {0,0};
unsigned char ReaderType = 0;
unsigned char TrType = 0;
unsigned char dmaxfre = 0;
unsigned char dminfre = 0;
unsigned char powerdBm = 0;
unsigned char ScanTime = 0;
unsigned char Parameter[6] = {0x01,0x0a,0x04,0x00,0x10,0x00};
unsigned char Qvalue = 0;
unsigned char Session = 0;
unsigned char AdrTID = 0x00;
unsigned char LenTID = 0x03;
unsigned char TIDFlag = 1;
unsigned char EPClenandEPC[1000];
int Totallen = 0,CardNum = 0;
HMODULE hDLL = LoadLibrary("UHFReader188.dll"); //加载dll文件
if(hDLL != NULL){
OpenComPort fp1 = OpenComPort(GetProcAddress(hDLL,"OpenComPort")); //得到dll中的第一个函数
GetReaderInformation fp2 = GetReaderInformation(GetProcAddress(hDLL,"GetReaderInformation"));
SetWorkMode fp3 = SetWorkMode(GetProcAddress(hDLL,"SetWorkMode"));
SetRelay fp4 = SetRelay(GetProcAddress(hDLL,"SetRelay"));
Inventory_G2 fp5 = Inventory_G2(GetProcAddress(hDLL,"Inventory_G2"));
CloseComPort fp6 = CloseComPort(GetProcAddress(hDLL,"CloseComPort"));
if(fp1 != NULL){
cout << "it can run the dll." << endl;
int a = fp1(Port,&ComAdr,Baud,&handle);
cout << a << " " << handle << endl;
cout << int(ComAdr) << endl;
}
else{
cout<<"Cannot Find Function "<<"OpenComPort"<<endl;
}
//ComAdr = 0xFF;
if (fp2 != NULL){
cout << "it can run the function2 of dll." <<endl;
cout << fp2(&ComAdr,VersionInfo,&ReaderType,&TrType,&dmaxfre,&dminfre,&powerdBm,&ScanTime,handle) << endl; //代码执行到此处,出现异常。
cout << handle << endl;
cout << int(ComAdr) <<endl;
}
else{
cout<<"Cannot Find Function2 "<<"GetReaderInformation"<<endl;
}
if (fp3 != NULL){
cout << "it can run the function3 of dll." <<endl;
cout << fp3(&ComAdr,Parameter,handle) <<endl;
}
else{
cout << "Cannot Find Function3 " << "SetWorkMode" << endl;
}
if (fp4 != NULL){
cout << "it can run the function4 of dll." << endl;
cout << fp4(&ComAdr,&Qvalue,&Session,handle) << endl;
}
else{
cout<<"Cannot Find Function4 "<<"SetRelay"<<endl;
}
if (fp5 != NULL){
cout << "it can run the function5 of dll." <<endl;
cout << fp5(&ComAdr,Qvalue,Session,AdrTID,LenTID,TIDFlag,EPClenandEPC,&Totallen,&CardNum,handle) << endl;
cout << "all is ok!!" <<endl;
}
else{
cout<<"Cannot Find Function5"<<"Inventory_G2"<<endl;
}
if (fp6 != NULL){
cout << "it can run the function6 of dll." <<endl;
cout << fp6() <<endl;
}
else{
cout<<"Cannot Find Function6"<<"CloseComPort"<<endl;
}
}
else{
std::cout<<"Cannot Find "<<"dll"<<std::endl;
}
FreeLibrary(hDLL);
return 1;
}