Automation in Windows, has over the years has been known by a number of different names as it has evolved, including Object Linking and Embedding (OLE), ActiveX, COM, COM+, DCOM and so on. Conceptionally, Automation is a set of objects, similar to C++ objects, that can be called from a program to perform various tasks, as required. In our case, they are called from a batch file or the command prompt itself. Typically, these objects are commonly referred to as COM objects or ActiveX objects and they control other software packages that maybe installed on your operating system, for example, internet explorer, word, excel, access and many others. For the sake of simplicity, this documentation will refer to all object as COM object only.
COM objects are made up of both Methods and Properties. Methods are simply functions included in the COM object and Properties are the variables had can be set or retrieved from a COM object.
The WinOne® Command Prompt supports Automation using three simple batch commands :-
- COM - Call a COM Object Method
- COG - Get a COM Object Property value
- COP - Put a value into a COM Object Property
All COM objects have the following three default methods :-
- .AddRef() - increment the reference count for an object
- .QueryInterface() - get the handle of another object
- .Release() - decrement the reference count for an object
When an object is first created, the WinOne® Command Prompt internally calls the .AddRef() method automatically and there is usually no need to call this method directly. On the other hand, it is the responsibility of the programmer to call the .Release() method from the batch file in order to release the object (and all the resources it uses) from memory. failure to release an object could result in a memory leak. The .QueryInterface() method is only used internally by the WinOne® Command Prompt when needed and can not be called directly from with in a batch file.
The best way to understand how to use a COM object is with some examples. Consider the following simple batch file which can be used to create a shortcut for a program called MyProgram.EXE :-
COM obj=CreateObject("WScript.Shell") COM lnk=obj.CreateShortcut("C:\MyShortcut.LNK") COP lnk.TargetPath="C:\Program Files\MyApp\MyProgram.EXE" COP lnk.Arguments="" COP lnk.Description="MyProgram" COP lnk.HotKey="ALT+CTRL+F" COP lnk.IconLocation="C:\Program Files\MyApp\MyProgram.EXE, 2" COP lnk.WindowStyle="1" COP lnk.WorkingDirectory="C:\Program Files\MyApp" COM lnk.Save() COM obj.Release()
Firstly, the batch file creates the COM object called "WScript.Shell" and places the handle for the COM Object in the environment variable called obj :-
Then the COM Object method .CreateShortcut() is called, passing a string (ie. "C:\MyShorcut.LNK") to the method. This specifies the name of the shortcut that the COM Object will create. this method returns a handle that is stored in the environment variable lnk :-
Next, all the properties for the shortcut that belong to the handle stored in the environment variable lnk are set to various values. This includes the properties .TargetPath, .Arguments, .Description, .HotKey, .IconLocation, .WindowStyle and .WorkingDirectory :-
COP lnk.TargetPath="C:\Program Files\MyApp\MyProgram.EXE" COP lnk.Arguments="" COP lnk.Description="MyProgram" COP lnk.HotKey="ALT+CTRL+F" COP lnk.IconLocation="C:\Program Files\MyApp\MyProgram.EXE, 2" COP lnk.WindowStyle="1" COP lnk.WorkingDirectory="C:\Program Files\MyApp"
Next, the .Save() method is invoked to save the shortcut to C:\MyShorcut.LNK :-
Note: when running this batch file on Windows Vista and above, the WinOne® Command Prompt will need to be run with Administrator Privileges in order to write to the folder c:\.
Finally, the COM objects .Release() method is called to release the memory used by the COM object, as it is no longer needed :-
The following sections provide a number of different examples that serve as a brief introduction into using COM objects from with in a batch file. There is many more tasks that COM objects can do, that is beyond the scope of this documentation. All COM object are well documented on the internet. Simply use a search engine to search Microsoft's MSDN for more information relating to the COM objects of interest.