2013-05-14 41 views
0

我需要使用NativeProcess進行一些非常簡單的操作,我需要通過cmd行啓動一個.exe文件並將其傳遞給一個參數。我發現了NativeProcess示例,但它們都是用於更復雜的事情,並沒有顯示完整的實現。我在flash as3方面有很多經驗,但在這個特定領域沒有......如果有人能告訴我這是從開始到結束如何實現的,我將不勝感激。如何在Air應用程序中實現NativeProcess

+0

http://gotoandlearn.com/play.php?id=125 - 這是一個很好的起點。 – Tox 2015-05-18 16:45:37

回答

2

下面是代碼從Adobe網站對究竟在做什麼你問:

package 
{ 
    public class Main extends Sprite 
    { 
     public var process:NativeProcess; 

     public function Main() 
     { 
      if(NativeProcess.isSupported) 
       setupAndLaunch(); 
      else 
       trace("NativeProcess not supported."); 
     } 

     public function setupAndLaunch():void 
     {  
      var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 
      var file:File = File.applicationDirectory.resolvePath("yourapp.exe"); 
      nativeProcessStartupInfo.executable = file; 

      var processArgs:Vector.<String> = new Vector.<String>(); 
      processArgs[0] = "the parameter you are passing"; 
      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); 
     } 

     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 
     { 
      trace("Process exited with ", event.exitCode); 
     } 

     public function onIOError(event:IOErrorEvent):void 
     { 
      trace(event.toString()); 
     } 
    } 
} 

和重要信息The NativeProcess class and its capabilities are only available to AIR applications installed with a native installer (extended desktop profile applications). When debugging, you can pass the -profile extendedDesktop argument to ADL to enable the NativeProcess functionality. At runtime, you can check the NativeProcess.isSupported property to to determine whether native process communication is supported.

我測試在Flash上​​面通過設置應用程序配置文件制定到Extended Desktop和它的工作原理。

更多信息here