Automation Server with mfc support

Introduction:

OLE Automation simplifies the task of automating applications. On one hand it enables an application to manipulate objects implemented in another application and on the other hand, it helps an application to expose objects that can be manipulated by other applications.

OLE automation is built on top of COM and has become quite popular in recent years as most of the Microsoft applications like Excel, DevStudio, Access and Word have started supporting it. The stage is set where customers expect every application to support OLE automation. As VC++ programmers, it is therefore in our interest to open our COM component and applications to outside world via OLE automation. [VC++, COM and Beyond.]

Here we would build an automation server that can verify the user ID and password, allow us to add new user, remove existing user and edit user information. The automation component uses marshalling/DCOM to communicate to the client located on the same/remote machine. We would use MS-Access as our database holder. This example also shows us a method to access remote database, although this may me not the best method to do so.

Prerequisite:

This tutorial assumes that you are familiar with Visual C++/MFC, COM, Automation and other technologies used in this tutorial like ADO, etc. Though there is no need to understand their underpinnings in great detail, you should at least have a working knowledge of what these underpinnings mean.

Implementation

DevStudio provides a lot of support to build an automation component, but unfortunately it assumes the application to be an SDI/MDI or a dialog based application. We would build a dialog based application and then remove all the dialog based support.

  • Create a dialog based MFC project selecting 'Automation' in step 2 of 4, like you have done previously.
  • Delete the dialog based resources, dialog based files [$ProjectName]Dlg.cpp/hDlgProxy.cpp/hand remove all the occurrences of that dialog from the [$ProjectName].cpp file.
  • Add the following statement after the call to COleTemplateServer::RegisterALL() in theC[$ProjectName]App::InitInstance() function.
    return TRUE;
  • Launch the class wizard, it will prompt us to remove the dialog related files from its database. Comply with its wishes until it is happy.
  • Compile the project now, it should compile without any error.
  • Launch the class wizard again and add a new class to the project derived from the CCmdTargetbase class. Choose the automation support "Createable by type ID" and specifyLogin.CRemoteLogin as the type id.

    Add New Class.

  • Use the automation property sheet of the class wizard to add new methods to the newly created class.

    Add New Method.

  • Compile the application and run it, the server will immediately terminate after adding the necessary registry entries.

Automation Client

There is also a sample client application located here.

Conclusion

Here we have successfully built an automation server that can verify the user ID and password, allow us to add new user, remove existing user and edit user information. This server can be easily extended to DCOM (Distributed Component Object Model) for a networked environment to exploit the benefits of distributed computing with remote database management. Though this may not be the correct method for using and managing a remote database, there may be more better methods too. But the purpose of this article is just to help you to understand the mysteries of automation better. Your valuable suggestions are always welcome, to make this article better.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值