5

I have a C# .net dll script that calls a SQL stored procedure that SELECTs data in order to do run relevant methods.

I need to run the dll using PHP as my entire application is built in PHP.

What is the best way of doing this?

I'm not experienced at all with C#.

EDIT

I successfully registered the .net dll using:

RegAsm.exe DllName.dll /tlb:DllName.tlb

I should now be able to use PHP's COM() method as described below to call the dll's functions/methods.

But will these functions still be accessible through the COM() method as the .net dll was registered as an assembly? Does it make a difference?

EDIT

After registering the .net dll assembly I get an error message when I try to call the method using COM():

"PHP Fatal error:  Uncaught exception 'com_exception' with message 'Failed
 to create COM object `DllName.ClassName': Invalid syntax"

EDIT

Tried using:

new DOTNET('DllName, Version=4.0.30319.33440, Culture=neutral,
PublicTokenKey=14843e0419858c21', 'ClassName');

got an internal server 500 error

Is this because PHP doesn't communicate with .net 4 assemblies?

proPhet
  • 1,037
  • 4
  • 12
  • 33
  • 2
    best way is to convert the query to a PHP implementation. You could also use the `exec()` php function, but I think use of this code when the frontend isn't the shell is considered bad practice.. – RichardBernards Nov 19 '14 at 12:09
  • I don't understand the C# language enough to convert to PHP. That was my first thought lol. But the script is greek to me and it seems like it's calling c# functions. – proPhet Nov 19 '14 at 12:11
  • What do you mean by a C# script? C# is compiled into an exe or dll. – Ben Robinson Nov 19 '14 at 12:13
  • Somewhere, somehow the query should be deducted, but without sourcecode, we cannot give an accurate answer to this... – RichardBernards Nov 19 '14 at 12:13
  • the script is a .dll file – proPhet Nov 19 '14 at 12:14
  • Could I load the dll code using `COM`? How would this work without parsing any variables? – proPhet Nov 19 '14 at 12:25
  • So, the C# assembly has a method that you like to call with some parameters and at the end this method calls a SQL stored procedure, where these parameters are involved somehow? So if you don't understand the C# code, you maybe should trace the SQL server to see the sql statement and how the parameters are interweaved there. Then built the same in PHP. – Oliver Nov 19 '14 at 12:35
  • Maybe you should post your C# code here and we can give you a little insight, what happens there. – Oliver Nov 19 '14 at 12:37

2 Answers2

4

Option 1: Use the DLL

You can call the function using PHP's COM class.

You'll need to be running PHP on Windows, which I assume you are if you're using a C# DLL.

Steps:

  1. Register the DLL using the command regasm yourdllname.dll in Command Prompt or the Run dialog. You have one RegAsm.exe for each version of .NET installed on your computer, so make sure to execute the one for the version of .NET that the DLL targets by running it from %windir%\Microsoft.NET\AppropriateVersion.
  2. Create a COM object in PHP that references the class name of the DLL.
  3. Call the function, which will be available as a method of the COM object you've created.

Example:

$object = new COM('MyDllProjectName.MyDllClassName');
$object->myMethod();

Option 2: Rewrite in PHP

As has been mentioned, the cleaner, cross-platform option is to re-implement the SQL query in PHP directly, especially if your only reason for using the DLL is to run a query.

Eric Eskildsen
  • 3,137
  • 1
  • 28
  • 43
  • 4
    Option 3 Access the DLL through a webservice – DarkBee Nov 19 '14 at 12:45
  • I'm confused, where does the `myMethod()` come from? is it instantiated there? how then do I call it? – proPhet Nov 19 '14 at 13:27
  • You'd replace `myMethod` with the name of the method (function) that calls the SQL stored procedure in your DLL. – Eric Eskildsen Nov 19 '14 at 13:31
  • I tried registering the dll but I got an error saying that `..the-entry point DllRegisterServer was not found` – proPhet Nov 19 '14 at 14:13
  • 1
    Try using `regasm` (C:\Windows\Microsoft.NET\Framework\YourVersion\RegAsm.exe) instead of `regsvr32`. The syntax is the same: `regasm yourdllname.dll`. – Eric Eskildsen Nov 19 '14 at 14:28
  • I managed to register the assembly using `regasm.exe DllName.dll /tlb:DllName.tlb` but will this assembly still be able to be called using the `COM()` method? – proPhet Nov 20 '14 at 08:22
1

Using COM directly has many caveats and issues.

There is a library called NetPhp that abstracts on top of COM using reflection, so that you can call ANY .dll from within PHP without hassle:

http://www.drupalonwindows.com/en/blog/calling-net-framework-and-net-assemblies-php

david
  • 11
  • 1