Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a C# .net .dll script using PHP

Tags:

c#

php

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?

like image 219
proPhet Avatar asked Nov 19 '14 12:11

proPhet


People also ask

What is call function in C?

A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.

What is call by value in C?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.


1 Answers

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.

like image 53
Eric Eskildsen Avatar answered Sep 30 '22 02:09

Eric Eskildsen