转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VBCOM TUTORIAL(3)         ★★★★

VBCOM TUTORIAL(3)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2001 更新时间:2009/4/23 18:58:29
;

At this point, it is very important to observe the following: no special programming was required to use COM with VB!  The only differences from the original "App.exe" were configuration-oriented --- the use of a different project type in the server and setting a project reference in the client.  On the surface, COM programming in VB is that easy.

For completeness, let''''s build the progress indicator into an out-of-process COM server.   Once again, the only changes will be the use of a different project type for the server, and a different project reference in the client:

  1. Repeat steps 1 .. 6 above, except create an ActiveX EXE project, set the project name "ProgressEXE" and the description to "_Progress Indicator Out-of-Process Server (VBCOM)".  After you make your project, you should end up with a COM server in the file "ProgressEXE.exe".
  2. Reopen the "Client.vbp" project, and under project references uncheck the in-process COM server and check your new "_Progress Indicator Out-of-Process Server (VBCOM)".  Now run your client (F5), and it should function as before!
  3. To convince yourself that the COM server actually runs as a separate executable, insert a Stop statement in the client immediately after the instantiation of CProgress:

  Set progress = New CProgress ''''** instantiate COM component   Stop   progress.Show ''''** tell COM component to show itself
    Now run the client from inside VB, and click the command button to execute the above code.  VB should enter break mode with the Stop statement highlighted.  Bring up the Task Manager on your computer, and you will see that ProgressEXE is running!  Switch back to VB, continue execution, and let the client run (i.e. let the processing of cmdProgress''''s Click event run to completion).  Return back to the Task Manager, and observe that ProgressEXE is no longer running.  Why not?  Because the client no longer has a reference to a CProgress object, and when the last object in a VB COM server is destroyed the server is stopped.
  1. If you want to play...  To keep the COM server running, we need to keep at least one object alive in the server.  This means we need to keep the object''''s reference count > 0.  One way to do this in our clients is to keep object references in global variables, which never go out of scope.  In the client app, first move the declaration of the progress variable from the Click event to the Declarations section (top) of the main form.  Then, instead of instantiating CProgress in the Click event, now do this in the form''''s Load event.  Finally, move the "Set progress = Nothing " statement from the Click event to the form''''s Unload event.  Now when you run the client, the COM server will be started as well, and this same process will continue to run until the client terminates.  Confirm this behavior using the Task Manager.

That''''s enough lab work for now, good job!


Registering a VB COM Server

As mentioned in the introduction, a COM server must be registered before a client can instantiate any of the coclasses in that server.  However, in the above lab the client app (e.g. "Client.exe") ran just fine without any kind of explicit "registration" step.  How can this be?  Who registered the progress indicator COM server?  And what does it mean for a COM component to be registered?

In short, registering a COM server means updating the Windows registry database on that computer.  The registry enables COM to locate coclasses in response to client instantiation requests.  To ease component development, VB automatically registers --- on your development machine --- any COM server you make (i.e. File >> Make).  Thus, each time you do make, VB unregisters the previous version (if any) and registers the new version. 

As evidence, let''''s break the "Client.exe" application by simply unregistering the COM server on which it depends.  First, in preparation, locate the file \VBCOM\Misc\regsvr.reg and double-click to execute; this file adds some entries to your registry database that makes server registration and unregistration easier.  Next, run your "Client.exe" in the lab directory \VBCOM\Labs\COM Servers\ and convince yourself that it shows progress without error.  Now right-click on the file "ProgressDLL.dll" --- the in-process progress indicator COM server --- and select "Unregister COM Server" from the pop-up menu.  Finally, re-run "Client.exe" and click the command button to show progress:  this should yield "Run-time error 429:  ActiveX component can''''t create object."   In other words, the requested COM server is not registered on this machine.  [ Note: if you did not receive an error, perhaps your client is instantiating the out-of-process version of the progress indicator.  Unregister "ProgressEXE.exe" and try again. ]  To fix the application, simply re-register the COM server by right-clicking on the appropriate file.  

How can you find out what COM servers are registered on your machine?  One way is to view project references.  Startup VB, select Project >> References, and you should see something similar to:

coclasses-references.gif

This list is produced by VB using information culled from the machine''''s registry database; recall we used this dialog in the lab to configure the client. 

COM servers can also be registered via the command-line.  To register / unregister an in-process COM server, use the REGSVR32 utility:

  REGSVR32 ProgressDLL.dll   REGSVR32 /u ProgressDLL.dll

In the case of out-of-process COM servers, run the server itself:

  ProgressEXE.exe /RegServer   ProgressEXE.exe /UnregServer

These approaches make it easy to automate the registration process, in particular during application installation on a client machine.  This task is typically performed by the install program itself.

For now, we can safely ignore the registration problem since VB will automatically register COM servers for us.  However, you need to keep this in mind when it comes time for deployment.  We will revisit this issue in more detail when we present COM activation.


In-Process vs. Out-of-Process?

When developing a COM component, you are faced with an immediate decision:  create an in-process server, or an out-of-process server?  The short answer is that in-process COM servers offer better performance while out-of-process COM servers offer greater flexibility.  The former are more efficient since the server objects live in the same address space as the client, enabling a much cheaper communication mechanism (a simple subroutine call).  The latter are more flexible since they can be deployed remotely on server machines, without having to recompile any of the code. 

At first glance, it would appear that multi-tier applications are best designed using out-of-process COM servers.  However, it is becoming quite common to develop in-process COM servers and then deploy these components using surrogate processes.  A surrogate process acts as a host for an in-process COM server, allowing it to run in an address space separate from the client --- just like an out-of-process COM server.  However, the advantage is that the surrogate can provide services to its server objects, such as support for sharing state (i.e. "global variables").   The most important example of a surrogate process is Microsoft Transaction Server (MTS), which among other things provides support for distributed transactions.  Thus, if you want to build COM components that work with MTS, you must create in-process COM servers.

What''''s next?  A discussion of client-side COM programming. 

上一页  [1] [2] 


[聊天工具]QQ2005 Beta3 珊瑚虫3.1.6 版发布__天极Yesky  [常用软件]绕过WGA安装IE 7 Beta3 5450
[常用软件][软件推荐]将MP3 压得更好  [常用软件]Expression3 与intuos2应用实例
[VB.NET程序]几个 WMI 的例子(初级) - 3  [VB.NET程序]VBCOM TUTORIAL(2)
[VB.NET程序]VBCOM TUTORIAL(1)  [VB.NET程序]VB程序员眼中的C# 3
[VB.NET程序]Microsoft Agent Tutorial Chapter 2  [VB.NET程序]Microsoft Agent Tutorial Chapter 1
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台