Colorizing Windows command line output from PHP

Download dynwrap.dll from : http://www.script-coding.com/dynwrap95.zip

Then extract it to %systemroot%\system32 directory and then run following command in command line:

regsvr32.exe "%systemroot%\system32\dynwrap.dll"

You’ll get a success message which means dynwrap.dll is registered.

Then you can use it this way :

$com = new COM('DynamicWrapper');

// register needed features
$com->Register('kernel32.dll', 'GetStdHandle', 'i=h', 'f=s', 'r=l');
$com->Register('kernel32.dll', 'SetConsoleTextAttribute', 'i=hl', 'f=s', 'r=t');

// get console handle
$ch = $com->GetStdHandle(-11);

some example:

$com->SetConsoleTextAttribute($ch, 4);
echo 'This is a red text!';
$com->SetConsoleTextAttribute($ch, 7);
echo 'Back to normal color!';

colors codes:
7 => default
0 => black
1 => blue
2 => green
3 => aqua
4 => red
5 => purple
6 => yellow
7 => light gray
8 => gray
9 => light blue
10 => light green
11 => light aqua
12 => light red
13 => light purple
14 => light yellow
15 => white

Leave a Comment