6

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?

8
  • 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.. Commented Nov 19, 2014 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. Commented Nov 19, 2014 at 12:11
  • What do you mean by a C# script? C# is compiled into an exe or dll. Commented Nov 19, 2014 at 12:13
  • Somewhere, somehow the query should be deducted, but without sourcecode, we cannot give an accurate answer to this... Commented Nov 19, 2014 at 12:13
  • the script is a .dll file Commented Nov 19, 2014 at 12:14

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Option 3 Access the DLL through a webservice
I'm confused, where does the myMethod() come from? is it instantiated there? how then do I call it?
You'd replace myMethod with the name of the method (function) that calls the SQL stored procedure in your DLL.
I tried registering the dll but I got an error saying that ..the-entry point DllRegisterServer was not found
Try using regasm (C:\Windows\Microsoft.NET\Framework\YourVersion\RegAsm.exe) instead of regsvr32. The syntax is the same: regasm yourdllname.dll.
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.