Posted by phase in SWF Studio V3 on Aug 18 2008, 05:19 pm

hi, i'm executig an command line tool with some arrguments and it works great but how can i capture and output the error massages when something gets wrong? i tried the this code but it doesn't work for me:


var listener_obj = new Object();
listener_obj.onShellExecuteFeedback = function (event_obj)
{
   ssDebug.trace(event_obj.result);
}
ssEvents.addEventListener({listener:listener_obj,event:'Shell.onShellExecuteFeedback,'});


thanks for help, phase


Posted by northcode in SWF Studio V3 on Aug 18 2008, 09:36 pm

For that event to generate anything you also have to call Shell.execute with the saveStdOut flag.

Here's a simple AS2 example. It assumes that there is a file called test.bat in the same folder as your EXE. The output of this command will be sent to the SWF Studio Trace Tab while the BAT file runs.


ssCore.init();
ssDefaults.synchronousCommands = true;

r = ssCore.Shell.setNotify({event:"onShellExecuteFeedback"}, {callback:onFeedback});

function onFeedback(r, c, e)
{
   ssDebug.trace(r.result);
}

r = ssCore.SysInfo.getEnv({variable:"COMSPEC"});

cmd = r.result;
   
quote = "\"";
args = "/C " + quote + ssGlobals.ssStartDir + "\\test.bat" + quote;
   
r = ssCore.Shell.execute({path:cmd, arguments:args, saveStdOut:true});

if (!r.success)
{   
   ssDebug.trace(r.result)
   ssDebug.trace(r.Error.description);
}


Posted by phase in SWF Studio V3 on Aug 19 2008, 04:54 am


QUOTE: from northcode;45144
For that event to generate anything you also have to call Shell.execute with the saveStdOut flag. ....

hi Tim, thanky for reply! I just copy&paste your code but nothing is traced.
I don't have a test.bat so there should be an error msg or not? somthing like file not found....

i just want to catch the error msg from the cmd window.

thanks,
phase


Posted by mbd in SWF Studio V3 on Aug 19 2008, 10:17 am

Did you enable the debug option in the Output tab? Make sure that's enabled and then rebuild your EXE. Make sure SWF Studio is open to the Trace tab and then launch your EXE.


Posted by northcode in SWF Studio V3 on Aug 19 2008, 11:39 am


QUOTE:
I don't have a test.bat so there should be an error msg or not? somthing like file not found

For that example to work you NEED a test.bat file. The Shell.execute won't return an error because the file it's trying to launch (cmd.exe) is found - it just won't return anything.

Create a text file with just one line in it (like below) and call it test.bat, this will generate some output when you run the example :)


dir c:\ /s


Posted by phase in SWF Studio V3 on Aug 19 2008, 02:37 pm


QUOTE: from northcode;45153
For that example to work you NEED a test.bat file. The Shell.execute won't return an error because the file it's trying to launch (cmd.exe) is found - it just won't return anything.

Create a text file with just one line in it (like below) and call it test.bat, this will generate some output when you run the example :)


dir c:\ /s


@mbd: yes, i enabled the debug option in the Output tab. other traces are shown up!

it doesn't matter if i had this bat-file. the executable that i want to execute requires some arrguments. if i execute it without any arrgument i get a help text displayed in the debug window, so it works to catch massages from the cmd. But when i pass only one arrgument i got an error msg in the dos box. i.e. "A format name is required". but i can't get this msg in the debug window.

i hope now its clear what the problem is.

thanks for support,
phase


Posted by northcode in SWF Studio V3 on Aug 19 2008, 11:33 pm

It sounds like the error is actually being written to the stderr (standard error) stream rather than the stdout (standard output) stream that the saveStdOut option is designed to capture. You can capture the stderr stream but your life is about to get a little more complicated.

What you have to do is redirect stderr to stdout so that SWF Studio can capture it. You can use the DOS shell (cmd.exe) to do it, but that means that you have to launch your application using cmd.exe instead of just launching it directly.

The redirection is not an issue. Passing arguments to your application while it is launched this way might be a problem. I'll have to test that. If it doesn't work, the fallback position is to create a BAT file on the fly that calls your application. That way the redirection will work and you can still pass arguments.


Posted by phase in SWF Studio V3 on Aug 20 2008, 03:30 am


QUOTE: from northcode;45155
...
The redirection is not an issue. Passing arguments to your application while it is launched this way might be a problem. I'll have to test that. If it doesn't work, the fallback position is to create a BAT file on the fly that calls your application. That way the redirection will work and you can still pass arguments.


good morning Tim,
thanks so far. can you give me a hint pls to create a .bat-File and redirect the stdErr to stdOut? I'm not firm on Windows and i'm not sure how to do that.

thanks,
phase


Posted by northcode in SWF Studio V3 on Aug 20 2008, 09:30 am

I'm going to create a simple console application that writes different messages to stdout and stderr for testing. Then I'll create an example to show you EXACTLY how to make it work so you can just replace my test application with yours. I'll post everything here when it's ready.


Posted by phase in SWF Studio V3 on Aug 20 2008, 01:49 pm


QUOTE: from northcode;45159
I'm going to create a simple console application that writes different messages to stdout and stderr for testing. Then I'll create an example to show you EXACTLY how to make it work so you can just replace my test application with yours. I'll post everything here when it's ready.
thanks Tim. Northcode gives the best support at all! swfStudio rules ;-)


Posted by northcode in SWF Studio V3 on Aug 20 2008, 05:56 pm

Okay, here's the example I promised.

Inside the zip file you'll find:

capture.fla - A Flash 8 example showing how to call a Windows console application and capture both the standard output and standard error streams in SWF Studio.

console.exe - A simple Windows console application that prints one message to stdout, one message to stderr and also displays it's command line arguments to stdout (just to show that it works).

capture.bat - An example BAT file that you would use to launch your application. You need the %1 %2 %3... stuff to pass the command line arguments from the BAT file to the EXE file.

If you DON'T want to capture stderr as part of the output, just find this code + " 2>&1" and remove it. That's what handles the redirection.

You'll notice a flash when you run the test app, that's just because I didn't hide the DOS window in the call to Shell.execute. If you want to do that, just add winState:"hidden" to the Shell.execute call.

attachments: capture.zip