tags:

views:

191

answers:

3

1) How to call winapi functions from PHP?
2) How to load any dll file and call functions from it?

Platform: ms windows, php5

php_w32api extension is not avalaible.

Maybe there is solution using COM objects?

A: 

Check the COM extension. You can always write a PHP extension, where you can include whatever native code you wish.

Artefacto
+1  A: 

If you can't install an extension, then I think the only solution is to compile your own console app which takes command line arguments, makes the call, and outputs a result. You can then execute it from your php script. Not terribly efficient!

Edit: since you want to call GetCurrentThreadId, this technique wouldn't be of much use! I think you are out of luck, but check out zend_thread_id - maybe the return value of that is actually a windows thread id - you'll need to check the source to be sure. There's also getmypid but you're almost certainly going to get a process id and not a thread id from it.

Paul Dixon
I want to call GetCurrentThreadId() from kernel32.dll. I want this because using WMI I can get all stats (cpu usage, memory usage, etc) information about every Thread of every Process. I want to identify thread in which current PHP script runs. So running external app wont help.
codez
@Pau Yes, it calls GetCurrentThreadId. Not that one should rely on undocumented behavior...
Artefacto
php show that zend_thread_id() is undefined. From documentations - "his function is only available if PHP has been built with ZTS (Zend Thread Safety) support". Is this means that I have to compile php by myself?
codez
@cod No, it means that if PHP is compiled without thread support, it doesn't make sense to ask for the thread id. http://windows.php.net/ has both ZTS and non ZTS builds.
Artefacto
+1  A: 

You mentioned stats. try...


$wmi_call = "wmic process where \"name like '%php%'\" list statistics";
system($wmi_call, $output);
var_dump($output);

My answer for alternatives to win api may be disheartening, but here it goes...

Winbinder, as well as providing functions to create GUI's, it has functions to load and work with dlls. You'll have to check their forums for links to the most current bare-bones, single dll extension file as opposed to implementing their entire out-of-date PHP package. Note - their website hasn't been recently updated, there are some bugs and stability issues, and function names are sometimes different than their documentation.

COM() will get you closer, but still not far enough. See this tuxradar.com article on working with PHP/COM. Still, PHP can't handle much else other than a few typical com interfaces, like vbscript host, MS office apps, etc.

DOTNET() will get you even further. See this peachpit.com article on the topic. Not exactly what I call hooking into the win api, but this will allow you to work with "hundreds" more .net classes and methods. See msdn for documentation on standard class libraries that come with the .net framework. Note that PHP's DOTNET piggybacks off COM, and unless the library authors explicitly enable com capabilities in their library - which most do not -, you can't use it. Also, this DOTNET class seems very limited and not mature. Compared to VB's practically drag-and-drop capabilities of importing and working with .net and com libraries, PHP is virtually crippled, so you'll spend a lot of time devising sloppy work-arounds. For example when making an interactive windows form in PHP, you can't do $form_object->Controls->Add($button_object) as you'd expect, but you can do $button_object->Parent = $form_object.

I've personally tried implementing several com and .net libraries using COM() and DOTNET(), and only a handful worked... barely. IMHO, I'd recommend building, compiling, and registering as a .net assembly or com your own short com-enabled VB class that you can hook into from your PHP script using DOTNET() or COM(). The PHP manual pages and the the peachpit.com article linked above will explain. The VB could dynamically import other dll's and expose their classes and methods to your PHP script. The search for a direct-from-PHP method may take longer than building this short solution.

bob-the-destroyer