Flex +javascript +Activex FLex 通过Javascript 调用ActiveX 操作硬件问题

我现在遇到这样的问题,公司做一套miss产品也就是我们的收银,仓储管理之类的软件这个时候会和很多的硬件打交道,例如pos收银机,打票机,钱箱,扫描器等等。我们界面准备用flex来做,后台用java(Spring+hibernate)+Blazeds,现在就是遇到了flex和硬件打交道的问题。因为厂商提供的接口都是dll所以我是通过Activex封装厂商提供的Dll,然后javascript去调用ActiveX,然后flex调用javascript.但是在页面引用<objectid="test"codeBase="test.CAB#1,0,0,1"classid="CLSID:E1BAF4EF-D3AA-4526-9A75-3E52CCDE1D2A"></object>

,这个对象的时候,flex找不到任何信息.也不报错,这个activeX我用html代码测试是没有问题的。能正确得到dll返回的值。把代码写在flex的页面就不行了、下面我贴出我的代码

首先在flex里面,我在index.template.html里面加入的代码:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!--savedfromurl=(0014)about:internet-->

<htmlxmlns="http://www.w3.org/1999/xhtml"lang="en"xml:lang="en">

<!--

SmartdevelopersalwaysViewSource.

ThisapplicationwasbuiltusingAdobeFlex,anopensourceframework

forbuildingrichInternetapplicationsthatgetdeliveredviathe

FlashPlayerortodesktopsviaAdobeAIR.

LearnmoreaboutFlexathttp://flex.org

//-->

<head>

<title>${title}</title>

<metaname="google"value="notranslate"/>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>

<!--IncludeCSStoeliminateanydefaultmargins/paddingandsettheheightofthehtmlelementand

thebodyelementto100%,becauseFirefox,oranyGeckobasedbrowser,interpretspercentageas

thepercentageoftheheightofitsparentcontainer,whichhastobesetexplicitly.Fixfor

Firefox3.6focusborderissues.Initially,don'tdisplayflashContentdivsoitwon'tshow

ifJavaScriptdisabled.

-->

<styletype="text/css"media="screen">

html,body{height:100%;}

body{margin:0;padding:0;overflow:auto;text-align:center;

background-color:${bgcolor};}

object:focus{outline:none;}

#flashContent{display:none;}

</style>

<!--EnableBrowserHistorybyreplacinguseBrowserHistorytokenswithtwohyphens-->

<!--BEGINBrowserHistoryrequiredsection${useBrowserHistory}>

<linkrel="stylesheet"type="text/css"href="history/history.css"/>

<scripttype="text/javascript"src="history/history.js"></script>

<!${useBrowserHistory}ENDBrowserHistoryrequiredsection-->

<scripttype="text/javascript"src="swfobject.js"></script>

<scripttype="text/javascript">

//Forversiondetection,settomin.requiredFlashPlayerversion,or0(or0.0.0),fornoversiondetection.

varswfVersionStr="${version_major}.${version_minor}.${version_revision}";

//Touseexpressinstall,settoplayerProductInstall.swf,otherwisetheemptystring.

varxiSwfUrlStr="${expressInstallSwf}";

varflashvars={};

varparams={};

params.quality="high";

params.bgcolor="${bgcolor}";

params.allowscriptaccess="sameDomain";

params.allowfullscreen="true";

varattributes={};

attributes.id="${application}";

attributes.name="${application}";

attributes.align="middle";

swfobject.embedSWF(

"${swf}.swf",

"flashContent",

"${width}",

"${height}",

swfVersionStr,

xiSwfUrlStr,

flashvars,

params,

attributes

);

//JavaScriptenabledsodisplaytheflashContentdivincaseitisnotreplacedwithaswfobject.

swfobject.createCSS("#flashContent","display:block;text-align:left;");

</script>

<script>

functionsayHello(){

alert("你好伙计!");

}

</script>

<scriptlanguage="javascript">

functiontestdll()

{

//alert("bbb");

//varbtn=document.getElementById("btn").value;

//alert(btn);

//vardid=document.getElementById("test").value;

//alert(did);

//这里导入了我的Activex打包的cab文件叫test.CAB里面的Dll有个叫GetContent的方法,有两个参数,是int类型的,返回的是这两个参之和;我在html代码里面测试了是没问题的。能正确返回值2;

varsun=test.GetContent(1,1);

alert(sun);

}

</script>

</head>

<body>

<objectid="test"codeBase="test.CAB#1,0,0,1"classid="CLSID:E1BAF4EF-D3AA-4526-9A75-3E52CCDE1D2A"></object>

<!--SWFObject'sdynamicembedmethodreplacesthisalternativeHTMLcontentwithFlashcontentwhenenough

JavaScriptandFlashplug-insupportisavailable.Thedivisinitiallyhiddensothatitdoesn'tshow

whenJavaScriptisdisabled.

-->

<!---->

<inputid="btn"value="点击我"type="button"style="display:none"/>

<divid="flashContent">

<p>

ToviewthispageensurethatAdobeFlashPlayerversion

${version_major}.${version_minor}.${version_revision}orgreaterisinstalled.

</p>

<scripttype="text/javascript">

varpageHost=((document.location.protocol=="https:")?"https://":"http://");

document.write("<ahref='http://www.adobe.com/go/getflashplayer'><imgsrc='"

+pageHost+"www.adobe.com/images/shared/download_buttons/get_flash_player.gif'alt='GetAdobeFlashplayer'/></a>");

</script>

</div>

<noscript>

<objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"width="${width}"height="${height}"id="${application}"><paramname="movie"value="${swf}.swf"/>

<paramname="quality"value="high"/>

<paramname="bgcolor"value="${bgcolor}"/>

<paramname="allowScriptAccess"value="sameDomain"/>

<paramname="allowFullScreen"value="true"/>

<!--[if!IE]>-->

<objecttype="application/x-shockwave-flash"data="${swf}.swf"width="${width}"height="${height}">

<paramname="quality"value="high"/>

<paramname="bgcolor"value="${bgcolor}"/>

<paramname="allowScriptAccess"value="sameDomain"/>

<paramname="allowFullScreen"value="true"/>

<!--<![endif]-->

<!--[ifgteIE6]>-->

<p>

EitherscriptsandactivecontentarenotpermittedtorunorAdobeFlashPlayerversion

${version_major}.${version_minor}.${version_revision}orgreaterisnotinstalled.

</p>

<!--<![endif]-->

<ahref="http://www.adobe.com/go/getflashplayer">

<imgsrc="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="GetAdobeFlashPlayer"/>

</a>

<!--[if!IE]>-->

</object>

<!--<![endif]-->

</object>

</noscript>

</body>

</html>

这里是我的flex的MXML代码:

<?xmlversion="1.0"encoding="utf-8"?>

<s:Applicationxmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"minwidth="955"minheight="600">

<fx:Declarations>

<!--将非可视元素(例如服务、值对象)放在此处-->

</fx:Declarations>

<fx:Script>

<![CDATA[

publicfunctioncallJs():void{

ExternalInterface.call("sayHello");

}

publicfunctioncallJs1():void{

//这里的testdll是index.template.html页面加入的js函数

ExternalInterface.call("testdll");

}]]>

</fx:Script>

<s:Buttonid="btn"x="12"y="10"label="FLex调用js"click="callJs()">

</s:Button>

<s:Buttonid="btn1"x="10"y="77"label="FLex调用jsanddll"click="callJs1()"/>

</s:Application>

相关推荐