正文

VC调用DLL问题2007-08-16 09:54:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/28545.html

分享到:

VC调用DLL问题

 提问:

在vc里编译成功了一个dll,怎么在另一个程序里调用它.

网友回答:

发表者:chunhai12

hmodule   loadlibrary(  
      lpctstr   lpfilename       //   file   name   of   module  
  );  
   
  例如  
   
  hinstance   hlibrary;  
  hlibrary=loadlibrary("mydll.dll");  
  //……  
  freelibrary(hlibrary);

发表者:healer_kx

加入lib和头文件,   把dll放在exe的目录。

发表者:zengwujun

下面通过例子介绍如何在动态链接库中定义函数、资源、和类以及如何在工程中使用动态链接库中已定义的函数、资源、  
   
  类。  
   
  一、   函数的定义和使用方法:  
   
    第一步:  
   
     运行appwizard,定义项目名为mydll,选择mfc   appwizard(dll),而不是mfc   appwizards(exe)。  
   
    第二步:  
   
     在这个例子中,只有一个appwizard屏幕出现,选择mfc扩展dll(mfc   extension   dll   (using   shared   mfc   dll),点击  
   
  finish生成工程。  
   
    第三步:  
   
     点击file中的new,选择c/c++   header   file,file   name中输入dll,点击ok,创建dll.h。输入extern   "c"   __declspec(dllexport)    
   
  int   fun(int   x1,int   x2,int   x3);   ,保存。  
   
    第四步:  
   
     点击file中的new,选择c++   source   file,file   name中输入dll,点击ok,创建dll.cpp。输入  
   
    #include   "stdafx.h"  
    #include   "dll.h"  
    extern   "c"   __declspec(dllexport)   int   fun(int   x1,int   x2,int   x3)  
     {  
      return   x1+x2+x3;  
     }  
   
    编译生成mydll.dll和mydll.lib。  
   
    第五步:  
   
    选择project   中add   to   project   中的new   ,   重新生成一个工程,选择mfc   appwizards(exe),项目名为mydlltest   ,   选择single    
   
  document   ,点击finish,生成一个新的工程。选择菜单中的project   àset   active   project   àmydlltest   ,将mydlltest设为当前活动工程  
   
  。  
   
    第六步:  
   
    拷贝…\mydll\debug\mydll.dll   到   ..\mydlltest\debug\下,拷贝…\mydll\debug\mydll.lib到…\mydlltest\目录下。  
   
    第七步:  
   
    在mydlltestview.cpp中的#endif下添加  
   
    extern   "c"   __declspec(dllimport)   int   fun(int   x1,int   x2,int   x3);  
   
    在void   cmydlltestview::ondraw(cdc*   pdc)中添加代码如下:  
   
    void   cmydlltestview::ondraw(cdc*   pdc)  
     {  
      cmydlltestdoc*   pdoc   =   getdocument();  
      assert_valid(pdoc);  
      //   todo:   add   draw   code   for   native   data   here  
      int   x=fun(1,2,3);  
      cstring   str;  
      str.format("%d",str);  
      pdc->textout(10,10,x);  
     }  
   
    第八步:  
   
    在workspace中的mydlltest   files上点击右键,选择add   files   to   project   ,将mydll.dll添加到工程。  
  好了,我们的工作做完了,运行一下看看吧!  
   
 

发表者:oyljerry

包含h和dll到project  
  然后loadlibrary加载dll文件,getprocaddress获得函数地址,调用函数

发表者:hxblvc


 

发表者:liuduofu

如果已经生成了一个dll,那么直接在工程里的:project下的settings里的link下加入dll的*.lib.注意了,把动态库拷进你的工程目录下呀  
 

发表者:hardeggwang

如果要是在vb下生成的activex   dll该怎么调用?

发表者:xxqqzz

我只作过在dephi中调用c++的dll

发表者:wuyupu

loadlibrary     具体的可以看csdn

.

阅读(15245) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册