开发者

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

开发者 https://www.devze.com 2023-01-05 20:29 出处:网络
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

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.

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?

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 topic including 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.

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

compiled: --enable-com-dotnet

[com_dotnet]

  • COM support => enabled
  • DCOM support => disabled
  • .Net support => enabled

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

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

<?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 sitti开发者_StackOverflowng 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?

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.

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.


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.


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).

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

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

Class constructors cannot have parameters, and everything needs to be decorated as 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:

  • 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)

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);


The PHP manual states that COM and DOTNET are classes not functions.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号