tags:

views:

106

answers:

4

My boss has a windows application that he wrote. It is not a Windows console application, but a Windows GUI application. We, of course, have the source code, but he does not want it to be a console app. He wants it to remain a regular GUI application.

The thing is, he wants to be able to call it from PHP, passing it parameters, and have the application return information to the calling PHP script. I can easily call Windows console applications and read the output. I can even call VBS scripts and get the output from them as well.

But, we are stumped on how to get a regular Windows application to output data to a calling php script without resorting to writing the output to a text file and reading it from within php.

Has anybody been able to do this? If so, how?

Thanks in advance.

Amy


Editing to add: Apparently, the boss put this code in his application:

BOOL bConsole = AllocConsole();
 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 DWORD dwCharsWritten = 0;
 string sS3Path = sCommandLine.substr(sCommandLine.find("S3://") + 5);
 string sMessage = "S3 Path: " + sS3Path;

 BOOL bWritten = WriteConsole(hOutput, sMessage.c_str(), sMessage.size(), &dwCharsWritten, NULL);

But, php is not seeing the information that he is writing to this console. We've tried exec and shell_exec to no avail.

+3  A: 

The best approch here would be to add a command line switch which would make your application behave like a console application when specified, GUI otherwise.

Retriving information from a GUI application is impossible unless it is sending output through Standard Output or a file.

Andrew Moore
Apparently this is non-trivial, as the choice to run an app as a GUI application or a console application is made at your application's build time, not at run time -- you can't *really* make it choose based on a command line switch. See: http://stackoverflow.com/questions/54536/win32-gui-app-that-writes-usage-text-to-stdout-when-invoked-as-app-exe-help
Frank Farmer
Like I said, behave LIKE... You can still pipe information to standard output even if it is a GUI application. The console window will not appear, but other programs will still be able to read the information.
Andrew Moore
+3  A: 

The best approach is to properly re-architect the logic in the Windows GUI app, so that it is exposed as a COM component. The GUI app and the PHP script can both call the COM component, and utilize the same logic.

In this case the data exchange need not and probably should not be via text. You can do the data exchange by setting and reading typed properties on the COM component.

This is what COM is/was intended for. And PHP of course has a mature library for invoking COM components on Windows.

It is a really bad idea to invoke a GUI app from a web app. You are asking for problems in doing that. The COM approach is much preferred.

Update: PHP COM DOC

Cheeso
A: 

You can pass parameters to a win forms app much the way you would to a command line application. Using the arguments parameter in the main method.

This will require some modification of your winforms application, but it will allow you to call the exe from php, passing in some parameters.

I'm not sure what your business requirements are, but ideally, the application is written with the business domain and the GUI separated, since you most likely don't need this cmd line process to have every feature of the winforms app, but rather produce proper output from some inputs, it shouldn't be too big of a deal to implement a cmd version of the application without rewriting any of your business logic.

Matthew Vines
A: 

Our final solution was for my boss to modify his Windows application. He was able to write to the console from within the app, and we could then read the output to that console.

Amy

Amy