长师两刃刀 2018-05-04 03:04 采纳率: 0%
浏览 1451
已结题

调用DLL中函数时出现异常,求大神指教!

本人用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;  

}

  • 写回答

5条回答

  • threenewbee 2018-05-04 03:16
    关注

    可能原因有很多,比如你的函数原型(参数列表、调用约定)和dll里函数的不一致
    或者你分配内存有问题,比如dll要修改一个指针,你传了常量
    再可能dll本身丢出了异常,比如说你这个是一个阅读器吧,你的硬件本身没工作,或者dll有bug
    你最好先拿厂家的程序调试,然后再整合到你的程序里。

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名