<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>ADM Blog &#187; communication</title> <atom:link href="http://blog.another-d-mention.ro/tag/communication/feed/" rel="self" type="application/rss+xml" /><link>http://blog.another-d-mention.ro</link> <description>No matter how you see things, reality changes when you reach understanding</description> <lastBuildDate>Fri, 22 Jul 2011 06:56:49 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1</generator> <item><title>Communicate betwen C# and an embeded Flash application</title><link>http://blog.another-d-mention.ro/programming/communicate-betwen-c-and-an-embeded-flash-application/</link> <comments>http://blog.another-d-mention.ro/programming/communicate-betwen-c-and-an-embeded-flash-application/#comments</comments> <pubDate>Wed, 18 Mar 2009 09:13:01 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[C#]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[activex]]></category> <category><![CDATA[communication]]></category> <category><![CDATA[external interface]]></category> <guid
isPermaLink="false">http://blog.another-d-mention.ro/?p=230</guid> <description><![CDATA[The External API allows an ActionScript developer to easily interact with the container program that is hosting Flash Player 8 and vice versa. The majority of the time, this will most likely be a Web browser, but this does not always have to be the case. As many C# developers know, it is easy to [...]
Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/right-click-and-custom-context-menu-in-flash-flex/' rel='bookmark' title='Permanent Link: Right click and custom context menu in Flash/Flex'>Right click and custom context menu in Flash/Flex</a></li><li><a
href='http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/' rel='bookmark' title='Permanent Link: Use clipboard (copy/paste) in C# console application'>Use clipboard (copy/paste) in C# console application</a></li><li><a
href='http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/' rel='bookmark' title='Permanent Link: How to clone (duplicate) an object in ActionScript 3'>How to clone (duplicate) an object in ActionScript 3</a></li></ol>]]></description> <content:encoded><![CDATA[<p><script type="text/javascript">google_ad_client = "ca-pub-3771432957882119";
/* InsidePost */
google_ad_slot = "9112434755";
google_ad_width = 468;
google_ad_height = 60;</script><br
/> <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></p><p>The External API allows an ActionScript developer to easily interact with the container program that is hosting Flash Player 8 and vice versa. The majority of the time, this will most likely be a Web browser, but this does not always have to be the case.</p><p>As many C# developers know, it is easy to house an ActiveX control (the IE version of Flash Player) in a .NET Windows application. This means we can now load an SWF in our Windows application and easily send data back and forth. Keep in mind that the keyword in this statement is "easily;" although possible before, it was not nearly as simple as the External API makes it now!</p><h3>C# to ActionScript Communication</h3><p>As I said before, communication between Flash Player and its container has been made extremely easy. The new class that makes this process so easy is the ExternalInterface. We will begin in the ActionScript. First, we need to import this new class so we can use it (as2 only, in as3 it will work without the import):</p><div
class="wp_syntax"><div
class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">external</span>.<span style="color: #006600;">ExternalInterface</span>;</pre></div></div><p>Next, we have to register any function we want to make available externally:</p><div
class="wp_syntax"><div
class="code"><pre class="actionscript" style="font-family:monospace;">ExternalInterface.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;addText&quot;</span>, addText<span style="color: #66cc66;">&#41;</span>;</pre></div></div><p>Basically, the code above will allow us to call the addText function (which I will show in a minute) from the C# application.<br
/> The addText function is as below. Basically, it takes a string input and appends it to a text box</p><div
class="wp_syntax"><div
class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> addText<span style="color: #66cc66;">&#40;</span>val:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	inTxt.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>val + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// append text recieved from c#</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div><p>That's it from the ActionScript side. Now all we need to do is call the function from C#. First, I add an instance of the Flash Player ActiveX control to my form and load the SWF we created in the form's constructor:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> AxShockwaveFlash player<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> DemoForm <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #008000;">...</span>
     <span style="color: #0000FF;">player</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LoadMovie</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, Application<span style="color: #008000;">.</span><span style="color: #0000FF;">StartupPath</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>EITest.swf&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     player<span style="color: #008000;">.</span><span style="color: #0000FF;">Play</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Next, all we have to do is call the externalized method when desired. In my case, it is in response to the user clicking the send button:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> sendBtn_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    player<span style="color: #008000;">.</span><span style="color: #0000FF;">CallFunction</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">+</span> outTxt<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><h3>ActionScript to C# Communication</h3><p>Again, you will need to use the ExternalInterface in the ActionScript:</p><div
class="wp_syntax"><div
class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span>evt : Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sendText&quot;</span>, outTxt.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// function to call and it's parameters</span>
	outTxt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;&quot;</span>; <span style="color: #808080; font-style: italic;">// reset text box</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div><p>As you can see, I am calling a method sendText and passing the input string as a parameter. Now to receive the message in C#, we first have to subscribe to the FlashCall event. You can do this in the constructor or from the activex properties panel on events tab.</p><p>Now the call made in ActionScript will be received in the request property of the event argument. For my particular call, the XML will look like this:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;invoke</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sendText&quot;</span> <span style="color: #000066;">returntype</span>=<span style="color: #ff0000;">&quot;xml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>some text message here<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/invoke<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>So now all we have to do is parse the XML in the event handler and invoke the C# function locally:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> player_FlashCall<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, _IShockwaveFlashEvents_FlashCallEvent e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// message is in xml format so we need to parse it</span>
    XmlDocument document <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    document<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadXml</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">request</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// get attributes to see which command flash is trying to call</span>
    XmlAttributeCollection attributes <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstChild</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Attributes</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">String</span> command <span style="color: #008000;">=</span> attributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// get parameters</span>
    XmlNodeList list <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;arguments&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// Interpret command</span>
    <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;sendText&quot;</span> <span style="color: #008000;">:</span> resultTxt<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> list<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;Some_Other_Command&quot;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Viola!</p><p><a
href="http://cache.another-d-mention.ro/images/wp-content/uploads/2009/03/untitled.png"><img
class="alignnone size-full wp-image-233" title="c# to flash activex demo" src="http://cache.another-d-mention.ro/images/wp-content/uploads/2009/03/untitled.png" alt="untitled Communicate betwen C# and an embeded Flash application" width="565" height="285" /></a></p><p>I have made the simple example discussed available <a
href="http://cache.another-d-mention.ro/stuff/CFlashProject.zip">here</a>.</p><p><script type="text/javascript">google_ad_client = "ca-pub-3771432957882119";
/* InsidePost */
google_ad_slot = "9112434755";
google_ad_width = 468;
google_ad_height = 60;</script><br
/> <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></p><p>Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/right-click-and-custom-context-menu-in-flash-flex/' rel='bookmark' title='Permanent Link: Right click and custom context menu in Flash/Flex'>Right click and custom context menu in Flash/Flex</a></li><li><a
href='http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/' rel='bookmark' title='Permanent Link: Use clipboard (copy/paste) in C# console application'>Use clipboard (copy/paste) in C# console application</a></li><li><a
href='http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/' rel='bookmark' title='Permanent Link: How to clone (duplicate) an object in ActionScript 3'>How to clone (duplicate) an object in ActionScript 3</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://blog.another-d-mention.ro/programming/communicate-betwen-c-and-an-embeded-flash-application/feed/</wfw:commentRss> <slash:comments>39</slash:comments> </item> </channel> </rss>
