windows PHP5 COM()、DOTNET():如何启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3172756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:45:29  来源:igfitidea点击:

PHP5 COM(), DOTNET(): how to start?

.netwindowscomphp

提问by bob-the-destroyer

Where I'm stuck is the right parameter syntax to instantiate an object using PHP's com()and dotnet()classes to load .net assemblies, windows system applications and components beyond the usual MS Office apps. If successfully created, working with the objects isn't an issue here.

我遇到的问题是使用 PHPcom()dotnet()类来实例化对象以加载 .net 程序集、Windows 系统应用程序和通常的 MS Office 应用程序之外的组件的正确参数语法。如果成功创建,处理对象在这里不是问题。

Can anyone explain what each part of each parameter means for both classes, and how I can find out the proper calling syntax for any application, component or assembly? What exactly can or can't PHP load, and if it can, how? How do I go about finding out which system components and classes are visible and can be made available through com() and dotnet()? For calling assemblies, Im I incorrect in assuming anything listed in /windows/assembly/gacxx folders can be used here?

谁能解释每个参数的每个部分对这两个类的含义,以及我如何找出任何应用程序、组件或程序集的正确调用语法?PHP 究竟可以或不能加载什么,如果可以,如何加载?我如何去找出哪些系统组件和类是可见的,并且可以通过 com() 和 dotnet() 提供?对于调用程序集,我假设 /windows/assembly/gacxx 文件夹中列出的任何内容都可以在这里使用是错误的?

I'm trying to dig deeper into connecting to or using Windows system applications, components and assemblies. For example, using 'System.Windows.Forms' assembly. After reviewing PHP's manual on the topicincluding code examples, it's not getting any clearer for me. By PHP's errors, I'm failing to get the syntax right, failing to find the specified files, or otherwise just failing completely without explanation. No related events are showing in Event Viewer to hint at application crashing or denied access.

我正在尝试更深入地了解连接或使用 Windows 系统应用程序、组件和程序集。例如,使用“System.Windows.Forms”程序集。在查看了有关该主题的 PHP 手册(包括代码示例)后,对我来说并没有变得更清楚。由于 PHP 的错误,我无法正确使用语法,无法找到指定的文件,或者在没有解释的情况下完全失败。事件查看器中没有显示任何相关事件来暗示应用程序崩溃或拒绝访问。

I have .net 4 framework installed. These are my current PHP settings...

我安装了 .net 4 框架。这些是我当前的 PHP 设置...

compiled: --enable-com-dotnet

编译:--enable-com-dotnet

[com_dotnet]

[com_dotnet]

  • COM support => enabled
  • DCOM support => disabled
  • .Net support => enabled
  • COM 支持 => 启用
  • DCOM 支持 => 禁用
  • .Net 支持 => 启用

Directive => Local Value => Master Value

指令 => 本地价值 => 主价值

  • com.allow_dcom => 0 => 0
  • com.autoregister_casesensitive => no value => no value
  • com.autoregister_typelib => 1 => 1
  • com.autoregister_verbose => 1 => 1
  • com.code_page => no value => no value
  • com.typelib_file => no value => no value
  • com.allow_dcom => 0 => 0
  • com.autoregister_casesensitive => 无值 => 无值
  • com.autoregister_typelib => 1 => 1
  • com.autoregister_verbose => 1 => 1
  • com.code_page => 没有价值 => 没有价值
  • com.typelib_file => 无值 => 无值

I've managed to get just three cases working so far. All other cases fail, including attempting to use other mscorlib classes...

到目前为止,我只设法让三个案例起作用。所有其他情况都失败了,包括尝试使用其他 mscorlib 类...

<?php

$scripting = new COM("MSScriptControl.ScriptControl");
$scriping_host = new COM("WScript.Shell');
$stack = new DOTNET("mscorlib", "System.Collections.Stack");

?>

I see 'System.Windows.Forms' just sitting there in the /windows/assembly folder taunting me. $forms = new DOTNET('System.Windows.Forms', 'Form');fails, even if the full assembly string is used. Am I looking in the wrong place? Should I instead approach this from within .net and create my own assembly to allow me to dynamically call others from PHP?

我看到 'System.Windows.Forms' 就坐在 /windows/assembly 文件夹中嘲笑我。 $forms = new DOTNET('System.Windows.Forms', 'Form');失败,即使使用了完整的程序集字符串。我找错地方了吗?我应该从 .net 内部处理这个问题并创建我自己的程序集以允许我从 PHP 动态调用其他程序集吗?

Note: Using the win32api PHP extension instead is right out as it's a dead project, and I'm hoping to step away from being dependent on others' pet project extensions.

注意:改用 win32api PHP 扩展是正确的,因为它是一个死项目,我希望摆脱对其他人的宠物项目扩展的依赖。

Edit:COM issue mostly solved. Within the registry, I was able to find a list of com supported software and system components and create several com objects using their clsid's. Still unsure exactly how to create DOTNET objects though.

编辑:COM 问题大多已解决。在注册表中,我能够找到 com 支持的软件和系统组件的列表,并使用它们的 clsid 创建多个 com 对象。但是仍然不确定如何创建 DOTNET 对象。

采纳答案by John Saunders

Try this:

尝试这个:

$forms = new DOTNET('System.Windows.Forms', 'System.Windows.Forms.Form');

I notice that the examples from the manual use fully-qualified class names.

我注意到手册中的示例使用了完全限定的类名。

回答by David

Using the COM and DOTNET classes is a nightmare. There are so many caveats that do not appear in the manuals. Simply put, you cannot get a regular .Net binary and expect to consume it from PHP (without NetPHP).

使用 COM 和 DOTNET 类是一场噩梦。有很多警告没有出现在手册中。简而言之,您无法获得常规的 .Net 二进制文件并期望从 PHP(没有 NetPHP)使用它。

Using COM you can instantiate any COM component in the system.

使用 COM,您可以实例化系统中的任何 COM 组件。

Using DOTNET you can only target .NET assemblies that have been compiled for the .NET 3.5 or below.

使用 DOTNET,您只能针对已为 .NET 3.5 或更低版本编译的 .NET 程序集。

Class constructors cannot have parameters, and everything needs to be decorated as COM Visible.

类构造函数不能有参数,一切都需要装饰为 COM Visible。

This basically means that you need to make your custom libraries for everything... unusable.

这基本上意味着您需要为所有内容制作自定义库......无法使用。

There is a ver convenient wrapper, the [PhpNet][1] library, that makes it a breeze to consume .Net from PHP and solves all these problems:

有一个非常方便的包装器,[PhpNet][1] 库,它使从 PHP 使用 .Net 变得轻而易举并解决了所有这些问题:

  • Use any .Net binaries (even without COM Visibility)
  • Iterate over .Net collections directly from PHP
  • Access native enums and static methods
  • Use class constructors with parameters
  • Debug .Net and PHP code at the same time as if it was a single application.
  • Work with libraries compiled for any version of the .Net framework (including 4.0 and above)
  • 使用任何 .Net 二进制文件(即使没有 COM Visibility)
  • 直接从 PHP 迭代 .Net 集合
  • 访问本机枚举和静态方法
  • 使用带参数的类构造函数
  • 同时调试 .Net 和 PHP 代码,就好像它是单个应用程序一样。
  • 使用为任何版本的 .Net 框架(包括 4.0 及更高版本)编译的库

This is what I am talking about:

这就是我要说的:

$manager = new \NetPhp\Core\NetManager();
$manager->RegisterAssembly('mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'mscorlib');
$manager->RegisterClass('mscorlib', 'System.IO.File', 'File');
$manager->RegisterClass('mscorlib', 'System.IO.FileOptions', 'FileOptions');
$file = $manager->Create('mscorlib', 'File');
$fileoptions = $manager->Create('mscorlib', 'System.IO.FileOptions')->Enum('Encrypted');

$file->Create("C:\www\prueba.tres", 2048, $fileoptions);

回答by Jon Cram

The PHP manual states that COMand DOTNETare classes not functions.

PHP 手册指出COMDOTNET是类而不是函数。

You ask how to use these classes. The manual page for each contains detailed descriptions of the constructor arguments along with examples and a fair amount of associated user-contributed documentation.

你问如何使用这些类。每个手册页都包含构造函数参数的详细描述以及示例和大量相关的用户贡献文档。

Do the manual pages not provide what you need?

手册页没有提供您需要的内容吗?