使用PHP调用C#.net .dll脚本 [英] Call a C# .net .dll script using PHP

查看:252
本文介绍了使用PHP调用C#.net .dll脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.net dll脚本,该脚本调用一个SELECT s数据的SQL存储过程以运行相关方法.

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

我需要使用PHP运行dll,因为我的整个应用程序都是使用PHP构建的.

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

做到这一点的最佳方法是什么?

What is the best way of doing this?

我对C#完全没有经验.

I'm not experienced at all with C#.

编辑

我成功使用以下方法注册了.net dll:

I successfully registered the .net dll using:

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

我现在应该能够使用如下所述的PHP的COM()方法来调用dll的函数/方法.

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

但是当.net dll被注册为程序集时,这些功能是否仍可以通过COM()方法访问?有区别吗?

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?

编辑

注册.net dll程序集后,当我尝试使用COM()调用该方法时,我收到一条错误消息:

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"

编辑

尝试使用:

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

遇到了internal server 500错误

这是因为PHP无法与.net 4程序集通信吗?

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

推荐答案

选项1:使用DLL

您可以使用PHP的COM类调用该函数.

Option 1: Use the DLL

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

您需要在Windows上运行PHP,如果您使用的是C#DLL,我认为您就是这样.

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

步骤:

  1. 在命令提示符或运行"对话框中使用命令regasm yourdllname.dll注册DLL. 对于计算机上安装的每个.NET版本,您都有一个 RegAsm.exe ,因此请确保通过从%windir运行DLL来针对DLL目标的.NET版本执行一个. %\ Microsoft.NET \ AppropriateVersion .
  2. 在PHP中创建一个引用DLL的类名称的COM对象.
  3. 调用该函数,该函数将作为您创建的COM对象的一种方法.
  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.

示例:

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

选项2:用PHP重写

如上所述,更简洁的跨平台选项是直接在PHP中重新实现SQL查询,尤其是在使用DLL的唯一原因是运行查询的情况下.

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.

这篇关于使用PHP调用C#.net .dll脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆