简体   繁体   English

如何从PHP运行Windows Forms应用程序

[英]How to run a Windows Forms Application from PHP

I'm trying to run a custom Windows Forms application that accepts command line arguments from a PHP script, but I have not had success so far. 我正在尝试运行一个自定义Windows窗体应用程序,该应用程序从PHP脚本接受命令行参数,但是到目前为止我还没有成功。 This program loads its interactive GUI if no arguments are provided, otherwise it will only process the information and then exit with status 0. 如果未提供任何参数,则此程序将加载其交互式GUI,否则它将仅处理信息,然后以状态0退出。

First, I tried using the exec() function and the page would just hang for about two minutes and then the browser would throw a "No data received" error. 首先,我尝试使用exec()函数,该页面将仅挂起大约两分钟,然后浏览器将引发“未接收到数据”错误。 So I started reading on the exec, popen, proc_open, system, passthru and shell_exec functions. 因此,我开始阅读exec,popen,proc_open,system,passthru和shell_exec函数。 I tried using exec, system and shell_exec to make the program run but to no avail. 我尝试使用exec,system和shell_exec使程序运行,但无济于事。

Then I found that maybe there was a permissions issue, since the user actually trying to run the program was my web server (Apache/2.2.22 (Win32) PHP/5.3.14) so I accesed the Apache service's properties and had it log on as my own user, restarted the service and managed to get a return value using the exec function. 然后我发现可能存在权限问题,因为实际上尝试运行该程序的用户是我的Web服务器(Apache / 2.2.22(Win32)PHP / 5.3.14),因此我访问了Apache服务的属性并将其记录下来以我自己的用户身份重新启动服务,并使用exec函数设法获得返回值。

I also found that in order to prevent the page from hanging I was supposed to set the program to run in the background, however I need to wait for the program's response to let the user know it has finished its work. 我还发现,为了防止页面挂起,我应该将程序设置为在后台运行,但是我需要等待程序的响应才能让用户知道它已经完成了工作。

The problem right now is that I can only seem to run it without arguments. 现在的问题是,我似乎只能在没有参数的情况下运行它。 Which doesn't actually load its GUI, it only returns its status 0, so I'm not sure it actually ran. 它实际上不会加载其GUI,它只会返回其状态0,因此我不确定它是否实际运行。 If I set an argument, then the page hangs and I get a cmd.exe process that won't close itself in the task manager. 如果设置参数,则页面将挂起,并且我将得到一个cmd.exe进程,该进程不会在任务管理器中自行关闭。

PHP Code: PHP代码:

$path = 'start /b "D:/Path/to/program.exe" argument';
$output = array();
$returnVal;
exec($path, $output, $returnVal);

How can I get this program to run? 如何使该程序运行?

Update: If I run the program without parameters and appending a > output.txt to the command, I get the following in the output.txt file: 更新:如果我运行不带参数的程序并将> output.txt附加到命令中,则会在output.txt文件中得到以下内容:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\WPrograms32\Apache2.2\htdocs>

通过将项目转换为控制台应用程序来解决此问题,PHP能够以某种方式正确执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM