ari使用NativeProcess访问本地exe文件

package
{
    import flash.display.Sprite;
    import flash.desktop.NativeProcess;
    import flash.desktop.NativeProcessStartupInfo;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    import flash.events.NativeProcessExitEvent;
    import flash.filesystem.File;
   
    public class NativeProcessExample extends Sprite
    {
        public var process:NativeProcess;

        public function NativeProcessExample()
        {
            if(NativeProcess.isSupported)
            {
                setupAndLaunch();
            }
            else
            {
                trace("NativeProcess not supported.");
            }
        }
       
        public function setupAndLaunch():void
        {    
            var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
            var file:File = File.applicationDirectory.resolvePath("DogForAir.exe");
            nativeProcessStartupInfo.executable = file;

            var processArgs:Vector.<String> = new Vector.<String>();
            processArgs[0] = "foo";
            nativeProcessStartupInfo.arguments = processArgs;

            process = new NativeProcess();
            process.start(nativeProcessStartupInfo);
            process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
            process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
            process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
            process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
            process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
            process.closeInput();
        }

        public function onOutputData(event:ProgressEvent):void
        {
            trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
        }
       
        public function onErrorData(event:ProgressEvent):void
        {
            trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable));
        }
       
        public function onExit(event:NativeProcessExitEvent):void
        {
         if(event.exitCode.toString()=="0")
         {
         GameModel.getInstance().state=false;
         }
         else if(event.exitCode.toString()=="1")
         {
         GameModel.getInstance().state=true;
         }
         this.dispatchEvent(new Event("refresh"));
        }
        public function onIOError(event:IOErrorEvent):void
        {
             trace(event.toString());
        }
    }
}

posted @ 2010-10-21 11:37  只身走天下  阅读(1685)  评论(1)    收藏  举报