W32api Functions

Table of Contents

add a note add a note

User Contributed Notes 10 notes

up
1
wobble at gmx dot de
11 years ago
I played a bit around with the W32API. Here is some code that actually works with the current release of W32API.
The interface changed completely, so all documentation about this extension is out-dated. While the old release
just implemented "plain" functions, the current version offers a class to handle all the API-related operations.
Additionally, functions are now registered using a SQL-like language with a single string.

<?php

$api
= new win32;

/*
     BOOL GetUserName(
        LPTSTR lpBuffer,    // address of name buffer
        LPDWORD nSize       // address of size of name buffer
     );
    Returns the current thread's username
    "&" passes argument as "refrence" not as "copy"
*/
$api->registerfunction("long GetUserName (string &a, int &b) From advapi32.dll");

/*
    DWORD GetTickCount(VOID)
    Returns the ms the OS is running
*/
$api->registerfunction("long GetTickCount () From Kernel32.dll");

$len = 255;                   // set the length your variable should have
$name = str_repeat("\0", $len); // prepare an empty string
if ($api->GetUserName($name, $len) == 0)
{
    die(
"failed");
}

if (!(
$time = $api->GetTickCount()))
{
    die(
"failed");
}

echo
"Username: $name<br>\nSystemtime: $time<br>\n";

?>

GOOD LUCK !!

Jan Kleinsorge
up
0
sk89q
7 years ago
The only way I've been able to call Win32 API functions with PHP5 has been through COM and DynamicWrapper.

Get DynamicWrapper at:
http://ourworld.compuserve.com/homepages/Guenter_Born/
WSHBazaar/WSHDynaCall.htm
(remove new line)

Here's an example to play a beep sound with your computer speaker:
<?php
$com
= new COM("DynamicWrapper");
$com->Register("KERNEL32.DLL", "Beep", "i=ll", "f=s", "r=l");
$com->Beep(5000, 100);
?>
up
0
MagicalTux at FF dot st
9 years ago
Answer to the note posted by me at nullflux dot com (see below)

The Win32 API *does* work in other versions of PHP than between 4.2.0 and 4.2.3. However the documented functions are not available. You will have to use the "win32" class to access functions. For an example, read Philip Soeberg's post just below.
up
0
chris at ramsoft dot biz
10 years ago
The win32 support appears to be pretty flaky in php. This of course is to be expected with an experimental extension. That being said, if you are having trouble using the win32 functionality you may want to look into creating a PHP extension instead.

We were having some problems interfacing PHP with a 3rd party dll. As such we created an extension which wraps the interface with the aforementioned dll. The solution was surprisingly quick and painless.
Because we use Delphi primarily we found the following extremely useful and easy to use. http://users.chello.be/ws36637/php4delphi.html#download

more info here:
http://php.us.themoes.org/manual/en/zend.creating.php
up
0
me at nullflux dot com
10 years ago
The w32api extension -does not- work on versions of php that are not between 4.2.0 and 4.2.3. This is mentioned on the individual function's manual pages, but not on the extension's main page. Perhaps I'm just missing something, but if you want to interact with the Win32 API via PHP you have to use an outdated version, or just not bother with it at all.
up
0
Philip Soeberg
10 years ago
In response to post by Jan Kleinsorge.

To get the proper windows system uptime, one should use the Performance Monitoring system... GetTickCount is limited to 49.7 days, as the result is a dword.

<?php
if (!dl("php_w32api.dll")) {
  echo
"Unable to load php_w32api.dll";
  exit;
}

$api = new win32;
$api->registerfunction("bool QueryPerformanceCounter (float &lpPerformanceCount) From kernel32.dll");
$api->registerfunction("bool QueryPerformanceFrequency (float &lpPerformanceFrequency) From kernel32.dll");

$api->QueryPerformanceCounter($a);
$api->QueryPerformanceFrequency($b);
$c = $a/$b;
$days = floor($c / 86400);
echo
gmstrftime("System uptime is $days Days, %H Hr, %M Min, %S Sec<br>", $c);
?>

The above snippet will return the true uptime for an unlimited time for a windows OS. (Disregarding the fact that windows needs reboot every 10 days or so :))
A tiny dange though is the lack of 64bit variable types in php.. (the "float &lpPerformanceCount" should be "long long &lpPerformanceCount") .. Nevertheless.. It works :)

Philip S.
up
0
Jan Kleinsorge
10 years ago
Or google for "win32.hlp". Almost every API-function is listed in there.
up
0
Anonymous
11 years ago
The small, unofficial documentation is now available here http://wobster.mynnga.de/w32api.txt .
The old link from the post above is dead.

Note that the W32API doesn't seem to be under development anymore. E-Mails to the author stayed unanswered.
If you really have to use it, just some hints here: There are often problems concerning Apache webservers. This extension doesn't seem to work with mod_php.
I used the API on IIS5 with PHP as CGI. Additionally the IUSR_<hostname> (that's the user IIS runs php.exe by default) might need additional rights to be able to call certain API-functions.
up
0
silver_dragoon77 at hotmail dot com
11 years ago
hi

for phpgtk users, who might want to add some sound to their apps, here is the code, assuming ding.wav is in the script's directory

$api = new win32;
$api->registerfunction("long sndPlaySound (string a, int b) From winmm.dll");
$api->sndPlaySound("ding.wav", 0);

you can use the big win32.hlp file containing the win32 api reference to get some other multimedia functions

regards
up
0
arunasphp at anm dot org
12 years ago
In order to use most (perhaps all?) of the win32 API while running with a web server you have to give the server service permission to interact with the desktop.  This is especially noticeable with the given example, where the script will try to display message boxes on the server's display.

Keep in mind, however, that you should think hard about the consequences of letting a web server interact with your desktop, especially if you're not the only one using the web server.
To Top