3

I'm developing a web app using CakePHP on the server side, and I need to use a dll provided by a 3rd party to access their platform, so I don't have the source code of the dll and I can't modify it. I've searched about this and I haven't found a simple way to do this.

What is the best way to load a dll in php?

Should I build an php extension that acts as a wrapper to the 3rd party dll?

It's there other method available?

Thanks in advance.

UPDATE

I've found that I can use the dll as a COM object but I would need to register the dll on Windows every time the client needs to deploy the server, and I would like a solution that does not involve registering the dll.

UPDATE

I tried registering the dll, but i got an error ("the entry-point DllRegisterServer was not found"), which means it probably is not compiled to be used as a COM object. I think the only options are to create a C/C++ php extension that wraps the dll or create a C++/C# COM object that wraps the dll and use it using the DOTNET php extension.

  • You are running Windows right? You will need a mix of PHP's [`exec()`](http://php.net/manual/en/function.exec.php) and http://stackoverflow.com/questions/3044395/how-do-i-execute-a-dll-file – MonkeyZeus May 16 '16 at 18:21
  • I don't think that will do, because I need to call a method inside the dll that will return an Object and then call some methods from that object. I've found that I can use the dll as a COM object but I would need to register the dll on Windows every time the client needs to deploy the server. – Iván José Mestre Fernández May 16 '16 at 18:55
  • Couldn't your deployment script register the DLL automatically? – CherryDT May 17 '16 at 11:24
  • Also check out http://stackoverflow.com/questions/2152584/is-there-anything-like-pythons-ctype-for-php-accessing-libraries-without-the-n – CherryDT May 17 '16 at 13:22
  • I tried registering the dll, but i got an error ("the entry-point DllRegisterServer was not found"), which means it probably is not compiled to be used as a COM object. I think the only options are to create a C/C++ php extension that wraps the dll or create a C++/C# COM object that wraps the dll and use it using the DOTNET php extension. – Iván José Mestre Fernández May 17 '16 at 14:31

1 Answers1

2

I ended up implementing a .NET COM visible Wrapper for the dll, I imported the dlls functions like this:

[DllImport("dapi.dll", EntryPoint = "#100", PreserveSig = false,
         CallingConvention = CallingConvention.StdCall)]
    public static extern Idapi CreateDapi();

I registered the Wrapper as a COM object using the regasm.exe utility in cmd:

regasm /codebase dapiWrapper.dll

And in php I instanciated the COM Object like this:

$this->dapiCom = new COM("DapiWrapper.DapiWrapper") or die("Error");
  • I know this is old, but i need to know, if this approach can work for importing on linux Env (ubuntu or debian) specialy on containers. – Samuel Bié Jul 19 '21 at 23:16
  • Is it a .dll or .so library? If it's a .dll, I'm not sure you can access COM objects from PHP on Linux using Wine, so your best bet is to use a VM or PC running Windows or Wine/Mono, wrap load the .dll on a .NET project and expose its functions via a web service using SOAP or JSON, and then consume the web service from PHP. If it's a .so, then you'll probably have to create a PHP extension wrapping the .so library and add it to you're PHP installation. – Iván José Mestre Fernández Jul 20 '21 at 14:36
  • Check this 2 SO answers: https://stackoverflow.com/questions/374157/using-windows-dll-from-linux https://stackoverflow.com/questions/13806550/calling-a-so-shared-library-object-from-php – Iván José Mestre Fernández Jul 20 '21 at 14:42
  • Its a .dll, i am tring to into a C shared object using https://github.com/taviso/loadlibrary – Samuel Bié Jul 23 '21 at 13:52