CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

VB做成的DLL如何在VC中使用?参与者有分

楼主zhuomao(拙猫)2001-12-10 16:22:04 在 VB / 基础类 提问

VB做成的DLL如何在VC中使用?参与者有分 问题点数:66、回复次数:2Top

1 楼uguess(天地间,有我在行走!)回复于 2001-12-10 16:43:43 得分 66

 
      呵呵,我来告诉你吧!(因为我刚刚做了一个)  
   
      由于VB不能生成标准的DLL和OBJ文件,所以只能通过Automation来实现VC对VB的调用。首先VB必须加入一个类,把需要VC调用的函数和方法放在这个类中。将VB程序编译为EXE和DLL(当然也可以生成.ocx在VC中调用)。   然后在VC中选择ClassWizard中的Automation,然后选择Import   class   from   typelib,选择EXE和DLL,此时ClassWizard会让你选择VB的类,然后生成C++代码。  
       
        下面是MSDN上的例子,按步骤作吧!      
   
  HOWTO:   Access   a   Visual   Basic   ActiveX   DLL   from   Visual   C++    
   
  --------------------------------------------------------------------------------  
  The   information   in   this   article   applies   to:  
   
  Microsoft   Visual   C++,   32-bit   Enterprise   Edition,   versions   5.0,   6.0    
  Microsoft   Visual   C++,   32-bit   Professional   Edition,   versions   5.0,   6.0    
  Microsoft   Visual   C++,   32-bit   Learning   Edition,   version   6.0  
   
  --------------------------------------------------------------------------------  
   
   
  SUMMARY  
  This   article   shows   three   ways   to   access   a   Visual   Basic   ActiveX   DLL   from   a   Visual   C++   6.0   executable.    
   
   
   
  MORE   INFORMATION  
  This   article   represents   an   introduction   to   creating   Visual   C++   clients   for   Visual   Basic   ActiveX   components.   If   you   are   already   proficient   with   Visual   C++,   you   may   wish   to   refer   to   the   reference   section   for   a   list   of   more   advanced   topics.    
   
   
   
  Steps   to   Create   the   Visual   Basic   Server  
   
   
   
  Create   a   Visual   Basic   ActiveX   DLL   project.   Class1   is   created   by   default.    
   
   
  Add   the   following   code   to   Class1:    
              Public   Function   MyVBFunction(x   As   Integer)   As   Integer  
                    MsgBox   x  
              End   Function    
   
   
   
  Compile   the   DLL   as   c:\Project1.dll   and   exit   Visual   Basic.    
   
   
  Method   1   -   CreateDispatch:    
   
   
  Start   Visual   C++   and   select   New   on   the   File   menu.   Choose   MFC   AppWizard   (Exe)   and   a   project   name,   and   Click   OK.   When   the   MFC   AppWizard   dialog   box   appears,   click   Finish.   Click   OK   on   the   next   dialog   box.    
   
   
  Select   ClassWizard   on   the   View   menu,   pick   Ctst1App   in   the   Class   Name   box,   and   double-click   InitInstance   in   the   Messages   box.   Click   Edit   Code   to   bring   up   the   code   for   BOOL   CTst1App::InitInstance(),   find   the   line   AfxEnableControlContainer();,   and   add   the   following   line   before   it:    
              AfxOleInit();    
   
   
   
  Select   ClassWizard   from   the   View   menu   and   click   the   Automation   tab.   Click   AddClass   and   select   "from   a   TypeLibrary".   Specify   Project1.dll,   the   Visual   Basic   DLL   which   was   created   in   step   3.   When   the   Confirm   Classes   dialog   box   appears,   click   OK.   Click   OK   again   to   close   the   MFC   ClassWizard   dialog   box.    
   
   
  Open   your   <App   Name>.cpp   file   and   add   the   line   #include   "Project1.h".   You   need   to   include   Project1.h   wherever   you   have   code   that   accesses   project1.dll.    
   
   
  Open   the   ClassWizard   again.   On   the   Message   Maps   tab,   select   CAboutDlg   in   the   Class   Name   box   and   IDOK   in   the   Object   IDs   box,   and   then   double-click   BN_CLICKED.   Click   OK   in   response   to   the   dialog   box   and   OK   again   to   close   the   ClassWizard.    
   
   
  Open   <App   Name>.cpp,   scroll   to   the   bottom   to   theCAboutDlg::OnOK(),   and   replace   it   with   the   following   code:    
              void   CAboutDlg::OnOK()  
              {  
   
              short   st   =   2;  
              short   st1;  
              _Class1   p;  
              p.CreateDispatch("Project1.Class1");  
              st1   =   p.MyVBFunction(&amp;st);  
              CDialog::OnOK();  
              }    
   
   
   
  Compile   your   .exe   file   (F7).    
   
   
  Run   the   .exe   file   and   select   About   on   the   Help   menu.   Click   OK   on   the   About   box   and   the   message   box   that   was   specified   in   project1.dll   appears.   Click   the   Close   button   to   dismiss   the   dialog   box.    
   
   
  Method   2   -   #IMPORT:    
   
   
  Start   Visual   C++   6.0   and   create   a   Win32   Console   Application.   Select   "An   Empty   Project"   and   click   Finish.    
   
   
  Point   to   Add   to   Project   on   the   Project   menu   and   click   New   to   add   a   new   C++   source   file   to   the   project.   Paste   the   following   code   in   the   new   source   file   and   save   it:    
              #include   <stdio.h>  
   
              //   This   is   the   path   for   your   DLL.  
              //   Make   sure   that   you   specify   the   exact   path.  
   
              #import   "c:\project1.dll"     no_namespace  
   
              void   main()  
              {  
                BSTR   bstrDesc;  
   
              try  
              {  
              CoInitialize(NULL);  
              short   st   =   2;  
                short   st1;  
              //   Declare   the   Interface   Pointer   for   your   Visual   Basic   object.   Here,  
              //   _Class1Ptr   is   the   Smart   pointer   wrapper   class   representing   the  
              //   default   interface   of   the   Visual   Basic   object.  
   
              _Class1Ptr   ptr;  
              //   Create   an   instance   of   your   Visual   Basic   object,   here  
              //   __uuidof(Class1)   gets   the   CLSID   of   your   Visual   Basic   object.  
   
                ptr.CreateInstance(__uuidof(Class1));  
                st1   =   ptr->MyVBFunction(&st);  
              }  
              catch(_com_error   &e)  
              {  
                bstrDesc   =   e.Description();  
   
              }  
              CoUninitialize();  
              }    
   
   
   
  Compile   and   run   your   project.   The   message   box   from   Project1.DLL   should   appear.    
   
   
  The   #import   method   can   be   used   in   a   Win32   Application,   a   Console   Application,   or   in   MFC   as   well.    
   
  Method   3   -   Pure   COM   Interface    
   
   
  Start   Visual   C++   and   select   New   from   the   File   menu.   Choose   MFC   AppWizard   (Exe),   name   the   project   tst1,   and   click   OK.   When   the   MFC   Appwizard   dialog   box   appears,   select   Dialog   Based   and   click   Finish.   Click   OK   when   the   next   dialog   box   appears.    
   
   
  The   Resource   Editor   is   started   by   default.   Delete   all   the   controls   on   the   dialog   box   and   add   a   Command   button   on   it,   retaining   the   default   caption   "Button1".    
   
   
  Double-click   Button1   to   display   the   Add   Member   Function   dialog   box.   Click   OK   to   accept   the   name   OnButton1.    
   
   
  Click   OLE/COM   Object   Viewer   on   the   Tools   menu.   Select   View   Typelib   from   the   File   menu   and   choose   the   Project1.dll   you   created   earlier.   Click   Open   to   display   the   ITypeLib   Viewer,   which   contains   the   .idl   file   for   your   DLL.    
   
   
  Copy   the   contents   of   your   .idl   file   (contents   of   the   right   pane)   to   the   Clipboard.   Hold   the   SHIFT   key   down   while   paging   or   scrolling   from   the   first   character   to   the   end   of   the   text   in   the   pane.   Press   Ctrl+C   to   copy   the   marked   text   to   the   Clipboard.    
   
   
  Click   New   on   Visual   C++   File   menu.   Select   Text   File   on   the   New   dialog   box,   name   the   file   test1.idl,   and   click   OK.    
   
   
  A   blank   text   file   appears.   Paste   the   data   from   the   Clipboard   into   it   and   save   the   file.    
   
   
  Select   Settings   from   the   Project   menu,   expand   the   tst1   and   Source   Files   nodes   of   the   tree   view,   and   select   test1.idl.   Click   the   MIDL   tab,   enter   test1.h   in   the   "Output   header   file   name"   box,   and   click   OK.    
   
   
  Open   tst1Dlg.cpp   and   add   the   following   files   to   the   includes   section:    
              #include   <initguid.h>  
              #include   "test1.h"    
   
   
   
  Click   the   ClassWizard   on   the   View   menu,   select   Ctst1App   in   the   Class   Name   box,   and   double-click   InitInstance   in   the   Messages   box.   Click   Edit   Code   to   bring   up   the   code   for:    
              BOOL   CTst1App::InitInstance()    
  Find   the   line:    
              AfxEnableControlContainer();    
  Add   the   following   line   before   it:    
              AfxOleInit();    
   
   
   
  Open   the   ClassWizard   again.   On   the   Message   Maps   tab   select   CTst1Dlg   in   the   Class   Name   box   and   IDC_BUTTON1   in   the   Object   IDs   box.   Double-click   BN_CLICKED   in   the   Messages   box,   and   click   Edit   Code   to   bring   up   the   code   for   void   CTst1Dlg::OnButton1().   Replace   the   OnButton1()   function   with   the   following   code:    
              void   CTst1Dlg::OnButton1()  
              {  
                    //   TODO:   Add   your   control   notification   handler   code   here.  
   
                    _Class1   *pClass   =   NULL;  
                    IUnknown   *pUnk   =   NULL;  
   
              //       HRESULT   hr   =   CoCreateInstance(CLSID_Class1,NULL,  
              //       CLSCTX_INPROC_SERVER,IID__Class1,(void   **)&pClass);  
              //       You   can   directly   get   the   Interface   ID   as   in   the   previous   line   or  
              //       you   can   do   a   QueryInterface   on   IUnknown   to   get   the   IID  
              //       as   in   the   following   three   lines:  
   
              HRESULT   hr   =   CoCreateInstance(CLSID_Class1,NULL,CLSCTX_INPROC_SERVER,  
              IID_IUnknown,(void   **)&pUnk);  
              hr   =   pUnk->QueryInterface(IID__Class1,(void   **)&pClass);  
              pUnk->Release();  
   
              //   Once   you   have   the   IID,   you   can   make   use   of   the   interface   pointer  
              //   to   access   our   Visual   Basic   DLL.  
   
              short   st   =   2;  
              short   st1;  
              hr   =   pClass->MyVBFunction(&st,&st1);  
              pClass->Release();  
   
              }    
   
   
   
  Compile   your   .exe   file   (F7)   and   run   your   application   (F5).   Click   Button1   in   the   dialog   box.   The   message   box   from   the   Visual   Basic   DLL   appears.  
   
   
   
   
   
    Top

2 楼zhuomao(拙猫)回复于 2001-12-10 17:05:53 得分 0

gei   fen   leTop

相关问题

  • PB与VB和Dll的问题,参与者都有分,谢谢……
  • VB与PB和Dll的问题,参与者都有分,谢谢……
  • ¥¥¥难题请教VB高手,参与者有分!!!
  • 为什么VB不能开发游戏的外挂程序?参与者有分~!
  • 【讨论】关于.dll文件的编译的问题!参与者有分!
  • how pass String from vb to VC DLL
  • vb调用vc++的dll
  • vc调用vb的dll
  • 参与者有分
  • 如何区别vb dll和vc dll

关键词

  • 32-bit
  • c++
  • visual c++
  • win32
  • dll
  • vc
  • visual
  • vb
  • ctst
  • box

得分解答快速导航

  • 帖主:zhuomao
  • uguess

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo