Calling a DLL with PHP

So here’s a quick post on calling dll’s in Windows using php. I have a dll that encrypts data in a certain format that we need for another process. So I need to pass the dll a string and it returns the encrypted string back.

I tried calling the dll using the COM class in code and was having issues until I realized I have to register the dll in windows first before I can call it using the COM class. To register a dll in windows you do the following in your command line:

REGSVR32 MyStuff.dll

Now that the dll is registered you can do the following to start accessing the dll:

$my_dll = new COM('MyStuff.Functions');

MyStuff is the dll name an/or id and Functions is the object inside the dll that we want to use. Now I call the method I need and pass the parameters:

$encrypted_text = null;
$input = 'This needs to be encrypted.';
$my_dll->EncryptString($input, $encrypted_text );

This is pretty much it. We instantiate the COM class with the dll and function I want. Then I call the method in the dll passing my text and it returns into my $encrypted_text var the encrypted text. I can now do my next process with the encrypted text like:

print $encrypted_text;

19 thoughts on “Calling a DLL with PHP”

  1. i am a newbie on php, i wanted to ask if this sample could access the dll installed in the client machine or the server

    1. Hi Alfred,

      PHP can only access what is in the server and not the client so if you would need to make sure the dll is available to PHP on the server side where the PHP code relies.

    1. Hey Eric, when I tried this I had to register the dll. To be honest, I don’t remember how much time I spent trying without registering but it was how I was able to get it to work. I don’t remember what the documentation says but I would assume as long as you pass a correct path to the file it should have worked. I don’t know if this helps you but there seems to be a .net class object that may help you but it also uses com I believe: http://us.php.net/manual/en/class.dotnet.php

  2. Hi, I was wondering if you can do the following:

    I have the dll of an application, this application reads two-dimensional bar codes using a webcam and decodes them to show a result, when I download the dll that came with examples of its use in vb.net, c++ y c #, but not in php . Now I need to use this application and return me the number that stores the bar code.

    Hope some advisement from you… the application is QuickMark

    1. Jenny,

      I’m not sure I’ll be much help. If I remember correctly, this only worked with some dll’s I tried and not all but I could have done something wrong. Also, I think if the dll couldn’t get registered with the system then I was not able to use them in PHP.

    2. Move all the dll files from the application installation folder to wherever you are calling the COM object.( In my case it is c:\\Xampp\htdocs\My_project). Then register the dll files as said above. If you are using 64 bit operating system, then navigate to c:\\windows\SysWOW64 in cmd and run regsvr32 command. Hope it helps.

  3. iam trying to call c++ dll in php.
    while creating a COM object iam getting error,can’t create COM object..like this…

    Is it mandatory to register the dll into system to use that dll in php.
    and how to write wrapper for that dll to use those functions in php..
    if anybody knows ,please clarify this.
    Thanks..

  4. I think we have to rememer that all DLLs cannot be registered or use .NET. I’m just now looking to do the same. I’ve called dlls from other scripting languages in the past. I’m sure I’ll find an answer.

    I just wanted to mention that when working with third party dlls that all cannot be registered and developers should either code a wrapper that can be registered of document how they can be used otherwise.

  5. We are developing a LMS using PHP and we need to integrate a USB Hardware lock to for security, can anybody tell how to use / call a dll file to check whether the valid lock is there or not on usb port

    Thanks in advance

  6. sir do you have a tutorials creating .net dll in visual studio coz im not familiar with .net only in php.. thank bcoz we are we integrating biometric to php…

  7. I’m trying real hard to learn about COM objects and calling them from PHP; this example was one of the first I came upon when I started looking for information. From what I’ve read I gather that the DLL must contain an interface in order to be used as a COM object – not just any DLL will do; is this correct?

    1. Hey Bo, when I was playing around with this a few years back only certain types of DLL’s would work. Unfortunately I don’t remember why some worked and others didn’t but you are correct – not just any DLL will do.

  8. Hi Joey/All

    Anyone have any feedback on whether using a DLL in a PHP website would be scalable? I presume it loads an instance of the DLL for each call so was thinking on a busy site that used the DLL for all its database access this might end up being slow.

    I’m being asked to use a Delphi DLL within a new site inplace of accessing the database directly.

  9. Andy, I think you have destruct COM objects carefully after use. It solves your problem? It doesnot solve situation, when there are a significant numner of quazi-simultaneous calls, but is a rare case.

  10. Hi Joey,

    I am going to try this method to access DLL in which I need to pass 2 values. But can you tell me if I need to configure any other thing as well. I checked in phpinfo that COM and DCOM are enabled but .Net says “not present in this build”. Would it be effect?

  11. Hi All,

    I wanted to call a function inside .NET dll from PHP. Have enabled php_com_dotnet extension. Can anyone please help me how to proceed further?

Leave a Reply to Sivaram Cancel reply

Your email address will not be published. Required fields are marked *