Jaslabs: High performance Software

High Performance Software

5 cool things you can do with windows and php

by Justin Silverton

Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache).

1) Eject the CD-ROM

//create an instance of Windows Media Player
$mp = new COM("WMPlayer.OCX");
//ejects the first cd-rom on the drive list
$mp->cdromcollection->item(0)->eject();

2) Read and write from/to the registry

function registry_read($folder, $key)
{
$WshShell = new COM("WScript.Shell");

$registry = "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\" . $key;
$result = $WshShell->RegRead($registry);

return($result);
}

$key = registry_read("RegisteredApplications","Firefox");

parameters:

  • Folder name - (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
  • key - the key name to read from

function registry_write($folder, $key, $value,$type="REG_SZ")
{
$WshShell = new COM("WScript.Shell");

$registry = "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\" . $key;
$result = $WshShell->RegRead($registry);
$result = $WshShell->RegWrite($registry,$value, $type);

return($result);
}

parameters:

  • Folder name - (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
  • key - the key name to write to
  • value - value that will be written to the key
  • type - key type (default: REG_SZ)

3) register and un-register phpscripts as a windows service

# registering a service

win32_create_service(array(
’service’ => ‘myservice’, # the name of your service
‘display’ => ’sample dummy PHP service’, # description
‘params’ => ‘c:\path\to\script.php run’, # path to the script and parameters
));

# un-registering a service

win32_delete_service(’myservice’);

# code run as a service

if ($argv[1] == 'run') {
win32_start_service_ctrl_dispatcher('myservice');

while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
# write script here
# as a general rule, keep it below 30 seconds through each loop iteration
}
}

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net) here
  • extract php_win32service.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_win32service.dll

4) print pages/data

#this is an example function that will format a host/printer name, for printing to shared printers over the network

function getPrinter($host,$SharedPrinterName) {
return “\\\\”.$host.”\\”.$SharedPrinterName;
}

#this opens the printer
$handle = printer_open(getPrinter(”my computer 2″,”my printer”));

An extensive list of functions for printing can be found here

#this is possible in *nix as well. Here is some example code

function lpr($string,$printer) {
$prn=(isset($printer) && strlen($printer))?”$printer”:C_DEFAULTPRN ;
$CMDLINE=”lpr -P $printer “;
$pipe=popen(”$CMDLINE” , ‘w’ );
if (!$pipe) {print “pipe failed.”; return “”; }
fputs($pipe,$string);
pclose($pipe);
}

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net) here
  • extract php_printer.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_printer.dll

5) List the current system processes

# list all the current processes running on the system

print_r(win32_ps_list_procs());

other related commands:

# Retrieves statistics about the global memory utilization
print_r(win32_ps_stat_mem());

# Retrieves statistics about the process with the process id pid (if no process id is given, the current process will be used)
print_r(win32_ps_stat_proc(int processid));

This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:

  • Download the main library (it’s included in the main PECL extension download from php.net) here
  • extract php_win32ps.dll to your ext directory (where your php extension .dlls are located)
  • add the following line to your php.ini: extension=php_win32ps.dll
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

12 Comments so far

  1. Bill June 4th, 2007 1:00 pm

    Thanks for thinking of us IIS/PHP users - we are out there!

  2. […] On the JSLabs website, Justin Silverton has posted five cool things that you can do with PHP in a Windows environment: Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache). […]

  3. Dougal Matthews June 6th, 2007 10:49 am

    I was hoping to see an example of using Microsoft office features via the com. Spell checker and so on.

    But a nice list, I’d like to see more.

  4. Justin Silverton June 6th, 2007 7:10 pm

    thanks for the feedback.

    I will start looking into microsoft+com+php. You may just see it in an upcoming article.

  5. TJ June 8th, 2007 5:05 pm

    Very cool stuff. Keep it up. Lots of our users want more PHP + Windows tips. We’ll keep track of yours. Bookmarked http://tektag.com/bookmark.php?bid=6894_5_cool_things_you_can_do_with_Windows_and_PHP

  6. brain90 June 18th, 2007 9:16 pm

    i have try the CD-Rom Opener Code..
    And it works
    but for the second chance, the code doesnt work !
    the browser just view long loading time…
    hope you can help me to clear it.

    salute for you

  7. Justin Silverton June 18th, 2007 10:53 pm

    “have try the CD-Rom Opener Code..
    And it works
    but for the second chance, the code doesnt work !
    the browser just view long loading time…
    hope you can help me to clear it.”

    are you saying that it works the first time you tried it, but locks up after the second try?

  8. N350CA Designer June 24th, 2007 9:19 pm

    Cool Scripts Man, Thanks Alot I Didnt Know You Could Read And Write To The Registry!! Hope No Hackers See This! Anyways Thanks The CD Open Thing Is Cool And Funny Cause Guys Visit The Site And Boom The CD Opens They Dont Know What Happened Lol ;) Thanks Again! Hope You Change The Title To 10 cool things you can do with windows and php And Add Some More JK $)

  9. Sherf August 28th, 2007 6:02 am

    Nice stuff, but I too got the same issue the second time it is not working. browser keep on loading.

  10. N350CA October 25th, 2007 6:37 pm

    lolz. stick the CD open code in a while {} statement. it keeps opening and closing even when you close the script!

  11. […] Kamis, 31 Januari 2008 oleh amin888 Disadur dari sini. Kebanyakan contoh-contoh PHP dibuat untuk sistem operasi linux/unix. Berikut adalah contoh-contoh menarik kegunaannya tapi hanya untuk php yang jalan di windows (IIS atau Apache). […]

  12. […] Di-sadur dari sini. Kebanyakan contoh-contoh PHP dibuat untuk sistem operasi linux/unix. Berikut adalah contoh-contoh menarik kegunaannya tapi hanya untuk php yang jalan di windows (IIS atau Apache). […]

Leave a reply