---
title: "【转】Flex和外部应用程序进行通信"
author: "pgmjl71f"
date: "2012-07-01T10:39:30Z"
tags: []
categories: ["非技术"]
slug: "flex"
draft: false
lastmod: "2012-07-01T10:39:30Z"
id: 1573034
url: "https://pgmjl71f.iteye.com/blog/1573034"
---


　　Flash To EXE  
　　Flex端  Flex创建一个Flex Project命名为Demo1，类型选Web application(runs in Flash Player)  
　　在Demo1.mxml上添加一个按钮，并未按钮添加点击事件  
　　private function ButtonClick(event:MouseEvent):void  
　　{  
　　//调用外部程序SayHello方法，并传入方法参数"Hunk"，输出方法返回值result  
　　if (!ExternalInterface.available) return ;  
　　var result:String = ExternalInterface.call("SayHello", "Hunk");  
　　trace(result);  
　　}  
　　EXE端  
　　VS2008创建window应用程序项目  
　　打开默认的窗体，向窗体添加ShockwaveFlashObject控件  
　　向窗体添加下面代码，为ShockwaveFlashObject控件注册FlashCall事件接收Flash发送过来的请求  
　　protected override void OnLoad(EventArgs e)  
　　{  
　　if (m_Init) return;  
　　m_Init = true;  
　　string swfPath = Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["SwfPath"]);  
　　if (System.IO.File.Exists(swfPath))  
　　{  
　　axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_Fla shCallEventHandler(axShockwaveFlash1_FlashCall);  
　　//通过配置文件加载Demo1.swf  
　　axShockwaveFlash1.Movie = System.IO.Path.Combine(Application.StartupPath, swfPath);  
　　}  
　　}  
　　private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_Fla shCallEvent e)  
　　{  
　　try  
　　{  
　　//当点击Demo1.swf的按钮时，该方法就会被触发，这里需要解析swf发送过来的xml请求信息  
　　//e.request格式  
　　//Hunk  
　　//根据e.request内容执行相关操作  
　　//操作处理完成后需要返回值到swf，这时需要调用ShockwaveFlashObject控件的SetReturnValue方法  
　　//返回Hello!给Demo1.swf  
　　axShockwaveFlash1.SetReturnValue("Hello!");  
　　}  
　　catch (Exception ex)  
　　{  
　　}  
　　}  
　　总结  
　　Flex调用外部应用程序方法(或flash to html)时主要使用ExternalInterface.call方法，详细了解该方法请查阅帮助。该方法会有一个比较严重的问题，当外部应用程序方法执行时间过长(大于60秒)而没有值返回时会报1502错误，所以操作时间大于60秒的方法需要另外的方式来实现，下篇会介绍如何实现异步调用方式解决该问题。  
　　外部应用程序主要通过注册ShockwaveFlashObject控件的FlashCall事件来接收flash发送过来的请求，请求是xml格式的字符串，详细可查找flex builder的帮助了解。返回值时调用ShockwaveFlashObject控件的SetReturnValue方法。  
　　进阶  
　　理解了Flex To EXE的原理后就可以设计一些比较有用的方法让EXE来完成Flex所不能完成的操作(不用AIR)，下面实现一个本地IO的API  
　　package common  
　　{  
　　import flash.external.ExternalInterface;  
　　public class LocalAPI  
　　{  
　　//Singleton static obj  
　　private static var g_Instance:LocalAPI = null;  
　　//获取LocalAPI单件实例  
　　public static function get Instance():LocalAPI  
　　{  
　　if (g_Instance == null)  
　　{  
　　g_Instance = new LocalAPI();  
　　}  
　　return g_Instance;  
　　}  
　　public function LocalAPI()  
　　{  
　　if (g_Instance != null)  
　　throw new Error("Singleton class. Please use Instance static filed.");  
　　}  
　　//写日志          public function Log(message:String):Number          {              return ExternalInterface.call(Command.FLASH_TO_APP_LOG, message);          }          //复制文件  
　　public function CopyFile(srcFilePath:String, destFilePath:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_COPY_F ILE, srcFilePath, destFilePath);  
　　}  
　　//删除文件  
　　public function DeleteFile(filePath:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_DELETE _FILE, filePath);  
　　}  
　　//复制文件夹  
　　public function CopyFolder(srcFolderPath:String, destFolderPath:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_COPY_F OLDER, srcFolderPath, destFolderPath);  
　　}  
　　//删除文件夹  
　　public function DeleteFolder(FolderPath:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_DELETE _FOLDER, FolderPath);  
　　}  
　　//写文件  
　　public function WriteFile(filePath:String, content:String, append:Boolean):Number  
　　{  
　　if (append)  
　　return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_ FILE, filePath, content, "true");  
　　else  
　　return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_ FILE, filePath, content, "false");  
　　}  
　　//读取文件  
　　public function ReadFile(filePath:String):String  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_READ_F ILE, filePath);  
　　}  
　　//获取路径的子文件夹列表  
　　public function GetFolderList(folderPath:String):Array  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_GET_FO LDER_LIST, folderPath);  
　　}  
　　//获取路径的子文件列表  
　　public function GetFileList(folderPath:String):Array  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_GET_FI LE_LIST, folderPath);  
　　}  
　　//创建新的文件夹  
　　public function CreateDirectory(folderPath:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_CREATE _FOLDER, folderPath);  
　　}  
　　//判断文件是否存在  
　　public function IsFileExist(path:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_ FILE, path);  
　　}  
　　//判断文件夹是否存在          
　　public function IsFolderExist(path:String):Number  
　　{  
　　return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_ FOLDER, path);  
　　}  
　　}  
　　} 
　　转自 http://blog.csdn.net/hunkcai/archive/2009/12/15/50 11767.aspx  
