Search code examples
phpasp.net.netdllcom

Running a function in DLL(.net) in PHP - Nothing seems to work


Ok, I have gone through almost every question, PHP Manual and post around this issue all over the internet. Nothing seems to help.

I'm working on a project that needs me to authenticate (or at least be able to lookup the UID for) different users in my organisation on an Active Directory. For connecting and authenticating, they have provided me with a dll file which has the functions necessary to authenticate. I need to use this dll file and the functions within to get my application working.

Below are the different things I have tried so far along with errors for each.

  1. Use dl() to load the DLL.

I tried using

dl('filename.dll');

and got the error

Call to undefined function dl() in C:\xampp\htdocs\OOP_curater\index.php on line 16

After looking around I came across this and this I realized it's no more supported.

I then tried placing the dll in C:\xampp\php\ext and adding an extention line to php.ini. (Just for the heck of it) restarting php gave me an error message saying it's not a valid PHP extention.

  1. Tried using COM()

I then tried using

$action = new COM("ProjectName.FunctionName") or die ("Could not initialise MS Word object."); 

and got the following error

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `ProjectName.FunctionName': Invalid syntax ' in C:\xampp\htdocs\OOP_curater\index.php:3 Stack trace: #0 C:\xampp\htdocs\OOP_curater\index.php(3): com->com('FBA_Provider1.a...') #1 {main} thrown in C:\xampp\htdocs\OOP_curater\index.php on line 3

Looking around got me to links similar to this. I realized I need to register the DLL so tried

regsvr32 C:\xampp\htdocs\OOP_curater\filename.dll

and got the error

DllRegisterServer entry point was not found

I then tried

regsvr32 /i /n C:\xampp\htdocs\OOP_curater\filename.dll

and got a "DLL Install was not found" error.

I must admit, I'm still not sure if I got the (Projectname.FunctionName) part right, i really could not understand most of the documentation around this topic.

  1. Tried using DOTNET()

I then tried the following code

$comobj = new DOTNET("projectname", "projectname.function");

and got this error

Fatal error: Uncaught exception 'com_exception' with message 'Failed to instantiate .Net object [CreateInstance] [0x80070002] The system cannot find the file specified. ' in C:\xampp\htdocs\OOP_curater\index.php:21 Stack trace: #0 C:\xampp\htdocs\OOP_curater\index.php(21): dotnet->dotnet('ProjectName', 'Function') #1 {main} thrown in C:\xampp\htdocs\OOP_curater\index.php on line 21

I now have the following questions with regards to loading a DLL for PHP

What is the best way to load a DLL where:

a. It is not really a PHP function

b. The source is not available

c. It cannot be registered (assuming due to the errors above)

d. It cannot be recompiled to be a PHP Extension / Register able

Few Things to Note

  1. The setup is currently on XXAMP for development and testing.
  2. I do not have admin rights, but can arrange for it if necessary.
  3. Production environment would be on a windows server running IIS.

I have very little knowledge in .net and Active Directory which is why I find this even tougher to handle. Also given the length of the question, hope I didn't miss out on anything important. Do let me know if you need more info.

EDIT

I was able to decompile the DLL using Reflector, and now have the source code and access to Microsoft Visual Studio for Applications 2.0. Incase you need me to make changed to the DLL itself.

Also I tried building it with the "make visible to com" option checked but still got the same error with regsvr 32 and regasm.


Solution

  • Based on your description of your requirements I'm not convinced you're going about this the right way. I suspect you've been led astray by 'they' providing you with a DLL file, as if you are building a desktop (or ASP.NET?) application.

    As I understand it you simply need to authenticate users of your PHP application via Active Directory (presumably to allow for authorisation checking in your application). With IIS this is a standard feature and is simple to activate. See my answer to another question here on SO about how to do this.

    If you need to do any querying against the AD (like retrieving the users UID, email, etc.) you can use the built-in LDAP functions.

    A quick and dirty example:

    $server ='ldaps://server_hostname'; // could also use 'ldap://' for non-encrypted connection.
    $username = 'username'; // should be a restricted 'service account' to be used only by this app.
    $password = '';
    $base_dn = '';
    $search_filter = 'cn=' . $_SERVER['AUTH_USER']; // search based on username provided by IIS single-sign on.
    $attributes = ['mail', 'sn', 'cn']; // array of fields you want AD to provide.
    
    $ldap = ldap_connect($server);
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); // generally needed with AD.
    ldap_bind($ldap, $username, $password); 
    $search = ldap_search($ldap, $base_dn, $search_filter, $attributes);
    $data = ldap_get_entries($ldap, $search);
    

    If a match is found $data will be an array of the specified attributes.