Use DLL in PHP?
Asked Answered
I

4

8

I'm not going to lie. I'm not all the familiar with Windows and COM objects. That's why i'm here. First of all is it possible to access a DLL from within a PHP script running out of Apache? In my journey around the internets i believe that i have 2 options:

  1. compile the dll as an extension for PHP. (i didn't make this dll)
  2. access the DLL as a COM object which is sort of what it's designed for anyways.

So i'm taking the COM approach.

try{
  $com = new COM('WHAT_GOES_HERE');
} catch(Exception $e){
    echo 'error: ' . $e->getMessage(), "\n";
}

How do i go about finding out what would go into the initialization string? is there a com viewer type program i could/should be using to find this out? the documentation associated with this DLL doesn't seem to specify what strings i should be using to initialize but gets very in-depth into what streams are available, and all sorts of fun stuff. just gotta past this initial hump. Please help!

Impermissible answered 7/7, 2009 at 23:9 Comment(1)
Does this answer your question? Can I use a dll written in C with PHP?Gelya
C
5

WHAT_GOES_HERE is the ProgID, Class ID or Moniker registered on the Operating System.

Each of these can change for the same DLL registered on different machines. There are several ways to find what's the ProgID/CLSID/Moniker of a registered dll. You can search the web for "dll debugger", "dll export", "dll inspect" and you'll see several solutions, and also ways to show what functions the dll export so you can use them.

The easiest way, you can just register the dll with Regsvr32.exe and search Window's register with regedit.exe for the dll's name, you might need to search several times until you find the key under \HKEY_CLASSES_ROOT\, which is the ProgID.

The command dcomcnfg.exe shows a lot of information about COM objects.

If you have Visual Studio, the OLE/COM Object Viewer (oleview.exe) might be useful.

Castile answered 8/7, 2009 at 3:13 Comment(0)
A
4

You can run dll functions (from dlls which are not php extensions) with winbinder. http://winbinder.org/ Using it is simple. You have to download php_winbinder.dll and include it in php.ini as an extension. In the php script you have to use something similar:

function callDll($func, $param = "")
{
    static $dll = null;
    static $funcAddr = null;
    if ($dll === null)
    {
        $dll = wb_load_library(<DLL PATH AND FILENAME>);
    }
    $funcAddr = wb_get_function_address($func, $dll);
    if ($param != "")
    {
        return wb_call_function($funcAddr,array(mb_convert_encoding($param,"UTF-16LE")));
    }
    else
    {
        return wb_call_function($funcAddr);
    }
}
Animated answered 21/7, 2010 at 11:16 Comment(2)
Winbinder hasn't been updated since 2010 and appears to be a dead OSS project sadly. :(Heavyweight
Not sure what happened with the site I have mentioned, but it looks like the project is still maintained here: github.com/Darksynx/WinBinder-PHP7Animated
S
2

You can simply develop a wrapper around your main dll and use this wrapper as an extension in your PHP. Some free tools like SWIG can generate this wrapper for you automatically by getting the header of your dll functions. I myself use this approach and it was easy and reliable.

Spiker answered 29/4, 2014 at 6:2 Comment(0)
G
1

With PHP>=7.4.0's new FFI/Foreign Function Interface (which didn't exist yet when this question was posted), this is now easier than ever before! For example, to call the GetCurrentProcessId(); function from kernel32.dll:

<?php

declare(strict_types=1);
$ffi = FFI::cdef(
    'unsigned long GetCurrentProcessId(void);',
    "C:\\windows\\system32\\kernel32.dll"
);
var_dump($ffi->GetCurrentProcessId());

outputs

C:\PHP>php test.php
int(24200)

:)

Gelya answered 5/9, 2022 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.