The possibilities of COM

The two COM scripts presented so far should have given you a fair idea of the power of COM - we have managed to run programs, load and control web browsers, and grab user and PC information, all by re-using existing code.

However, in order to demonstrate quite how much flexibility COM gives you as a Windows-based PHP developer, we are going to do something as yet not done in the world of PHP: we are going to make PHP work with VBScript. If you just gasped and had to take a deep breath before continuing, you clearly did not migrate to PHP from ASP! Microsoft's Active Server Pages (ASP) technology lets you choose the language you write your scripts in on a script-by-script basis. Now, while I dislike ASP, and dislike VBScript even more, quite a few of you probably came from an ASP background.

ASP programmers traditionally make very heavy use of COM objects, from ADO to their own custom components; giving people the functionality to literally write and execute VBScript inside their PHP code adds a new layer of functionality. Granted, many will see absolutely no reason why VBScript and PHP should be mixed, but the hacker's answer to that is "because we can"!

Take a look at this script:

<?php
    $scripter = new COM("MSScriptControl.ScriptControl");
    $scripter->Language = "vbscript";

    $i = 5;
    $j = 2;
    $k = $scripter->eval( $i . " + " . $j);
    print "Result: $k\n";

    $scripter->ExecuteStatement("MsgBox \"The result is $k\"");
    $scripter = null;
?>

This time we use the component MSScriptControl.ScriptControl, the Microsoft Script Control, which is a control designed to let application designers execute scripts from within their programs. In this example, we want to execute VBScript from our PHP "application", so this control is perfect for our needs.

Line two of the PHP script tells the MS script control that we want to use VBScript. This could also have been JScript, Microsoft's extension of JavaScript, or it also could have been any other supported scripting system that was installed. We then use the eval() function to pass in the code we want to execute - in this case, it is just simply adding two variables together, and the result is stored in the PHP variable $k.

Next, the ExecuteStatement() function is called, which also takes the block of code to execute as its parameter. In this case, the MsgBox VBScript function is called, which pops up a message box on the screen with the parameter supplied to it, "The result is $k". Naturally PHP will replace the $k with the value received back from the earlier call to eval(), and MsgBox will output "The result is 7". Just so you can see that VBScript and PHP do work together fluently, the value is also printed out to the command line where you launched PHP.

Finally, the $scripter variable is set to null, to ensure that things are cleaned up properly.

So now we have a script where we call another scripting language - are you confused yet? Hopefully this should have given you ideas for your own scripts - do not be afraid to experiment with wild ideas!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: DCOM >>

Previous chapter: Advanced COM

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.