zoukankan      html  css  js  c++  java
  • vc++ 调用dll应用与案例

    静态库直接链接,#pragma command(lib,"rw.lib"); 
    然后在系统可以搜索的路径下放入你的rw.dll文件 

    动态库的链接 
    typedef int (WINAPI *MYFUNC)(int nPort, WORD wSID, BYTE bDT, WORD  bDID,struct _RWStatus *) ; 
    MYFUNC ProcAdd=NULL; 
    HINSTANCE hInst=LoadLibrary("rw.dll"); 
    if(hInst!=NULL) 
      ProcAdd=(MYFUNC)GetProcessAddress(hInst,"Init_RW");//注意你dll导出的函数名 
      if(ProcAdd!=NULL) 
      { 
        ProcAdd(...); 
      } 
    .... 
    FreeLibrary(hInst);



    比如dll中有加减两面个函数   
        
      #include   "..\\DLL1.h" //加入头文件   
      void   CUseDllDlg::OnOK()     
      {   
      HMODULE   hDll=::LoadLibrary("D:\\VC\\Lesson1\\DLL1\\Debug\\dll1.dll"); //定义一个窗口句柄,用来加载动态库   
      if(hDll==NULL){AfxMessageBox("not   found");return;} //如果加载失败,反回错误   
        
      try //试着运行一段代码   
      {   
      DLLAdd   funAdd=(DLLAdd)::GetProcAddress(hDll,"dllAdd"); //定义一个函数指针来指向调用的函数   
      if(funAdd==NULL)   throw("not   found");       //如果加载失败,反回错误   
      int   nResult=funAdd(3,6); //调用加法函数   
        
      CString   str; //输出调用结果   
      str.Format("%d",nResult);   
      AfxMessageBox(str);   
        
        
      //减法   
      DLLSub   funSub=(DLLSub)::GetProcAddress(hDll,"dllSub");   
      if(funAdd==NULL)   throw("not   found");   
        
      nResult=funSub(3,6);   
        
        
      str.Format("%d",nResult);   
      AfxMessageBox(str);   
        
      }   
        
      catch(LPCSTR   lpErr) //用来接住throw抛出的错误,有字符串显现   
      {   
      AfxMessageBox(lpErr);   
      }   
      catch(...) //如果不是throw抛出的错误,是其它错误,弹出错误对话框   
      {   
      AfxMessageBox("Uoknow   error");   
      }   
        
      ::FreeLibrary(hDll); //释放库文件   
        
      }   
     
  • 相关阅读:
    RHEL 5.7 Yum配置本地源[Errno 2] No such file or directory
    SQL SERVER中什么情况会导致索引查找变成索引扫描
    Vmware扩展磁盘如何不需重启系统
    Vmware虚拟机进入BIOS方法
    Linux LVM学习总结——删除物理卷
    Linux系统检查查看桌面环境
    spring面试问题与答案集锦
    solr查询特殊字符的处理
    阅读源代码的一点小技巧
    solr查询工作原理深入内幕
  • 原文地址:https://www.cnblogs.com/luhuan860/p/newtype.html
Copyright © 2011-2022 走看看