VBA, VBScript, JavaScript and DCOM
VBA, VBScript, JavaScript, are programming scripts to write and automate tasks in Office and Windows operating systems. These have component support for doing all sorts of works provided by Windows and Office. Some services which are not available or some services which are custom needed by a user cannot be fulfilled. These types of situations call for a custom made COM server. The user requires to implement the routine in the server part and thus that function can be called by a script file.
We will be building a custom made COM/DCOM server using ATL C++ framework. We will be using this ATL framework since this is much easy compared to implementing in raw C++ files.
These are prerequisite knowledge-base and required software tools you must possess before reading this article.
- OOP programming in C++
- Knowledge of component software (COM/DCOM)
- Knowledge of VBA, VBScript, JavaScript (any one)
- Visual Studio 6.0 or VS community (free) installed
DCOM server DLL using ATL C++
We will open Visual C++ and start with a new project. Visual Studio provides wizard-based project configuration and creates all the necessary files automatically as needed by the project. It takes less effort and time so it is a better choice while creating a project. We should select "ATL COM App wizard" and provide the name of our project.
Our component will be a small calculator so we have given the name "MyATLCalc". We are creating a DCOM server as a dynamic link library executable.
The wizard will finish creating all the necessary files. Our output executable file will be MyATLCalc.dll. MyATLCalc.cpp is our C++ implementation file for our DLL. MyATLCalc.IDL is the interface definition file where we will define the functions/interfaces of the calculator which will be used by our scripting language(client). MyATLCalc.mk will generate stub/proxy files for DCOM operations.
At this point, VC++ editor will generate all the files and necessary functions of the DCOM DLL export function.
Our DCOM component is ready but it does not have any interface. We will add our Calculator interface using the menu option New ATL Object.
We will select the type as Simple Object and continue.
We have given a short name "MyCalc" as the name of the object. This will add an interface in IDL file and the editor will create a C++ class file as "CMyCalc". MyCalc is the interface and CMyCalc C++ class is the implementation of this interface.
Now we add our first interface function as AddInt(). This function takes two integer x, y as inputs and returns the value as an integer.
Interface definition file
IMyCalc::AddInt will be implemented in C++ and it will accessed by script.
C++ header prototype
CMyCalc is the C++ class file of the interface IMyCalc
C++ implementation
CMyCalc::AddInt() function body implemented here. The implementation of this function is just to add these two inputs and return the value.
C++ implementation of DCOM DLL
Deploy DCOM DLL
VC++ make (.mk) file automatically register the component after a successful build. So an automatic installation will be done on the development system.
Component DLL is different from the normal DLL executables. Component DLL file cannot be used until it is registered with the operating system's registry. We need to copy the C runtime dynamic library file i.e. msvcrtxx.dll to path location. It is appropriate to copy this file to \Windows\System32.
The installation or registration of the DLL can be done using regsvr32.exe. We need to copy this component DLL to a path and execute regsvr32.exe along with this dll as an argument(example regsvr32 MyATLCalc.dll).
The uninstallation or deregistration of the DLL can be done using regsvr32.exe with /u argument. (example regsvr32 /u MyATLCalc.dll).
There are installer softwares available to do this and Installshield is one such software that comes with VS. This entire installation and uninstallation process can be done with Installshield.
DCOM Client using VB/VBA, VB Script, Javascript
Our deployment system will be ready to use once the installation is complete. Now we can use any of your desired scripting language to access this C++ DCOM server. We are calling this AddInt C++ interface function and printing the result in a message box.
DCOM Client using Javascript Script
DCOM Client using VBA/VB Script
DCOM Client using Visual Basic
What is next?
What is variant type in script? How arrays are implemented? How enum types are supported? What is callback event to script? How to implement completion event for long task?
About our authors: Team EQA
You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.