ADM Blog No matter how you see things, reality changes when you reach understanding

18Mar/0913

Communicate betwen C# and an embeded Flash application


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 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!

C# to ActionScript Communication

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):

import flash.external.ExternalInterface;

Next, we have to register any function we want to make available externally:

ExternalInterface.addCallback("addText", addText);

Basically, the code above will allow us to call the addText function (which I will show in a minute) from the C# application.
The addText function is as below. Basically, it takes a string input and appends it to a text box

function addText(val:String):void
{
	inTxt.appendText(val + "\n"); // append text recieved from c#
}

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:

private AxShockwaveFlash player;
 
public DemoForm ()
{
     ...
     player.LoadMovie(0, Application.StartupPath + "\\EITest.swf");
     player.Play();
     ...
}

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:

private void sendBtn_Click(object sender, EventArgs e)
{
    player.CallFunction("" + outTxt.Text + "");
}

ActionScript to C# Communication

Again, you will need to use the ExternalInterface in the ActionScript:

function send(evt : Event):void
{
	ExternalInterface.call("sendText", outTxt.text); // function to call and it's parameters
	outTxt.text = ""; // reset text box
}

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.

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:

<invoke name="sendText" returntype="xml">
<arguments>
<string>some text message here</string>
</arguments>
</invoke>

So now all we have to do is parse the XML in the event handler and invoke the C# function locally:

private void player_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
    // message is in xml format so we need to parse it
    XmlDocument document = new XmlDocument();
    document.LoadXml(e.request);
    // get attributes to see which command flash is trying to call
    XmlAttributeCollection attributes = document.FirstChild.Attributes;
    String command = attributes.Item(0).InnerText;
    // get parameters
    XmlNodeList list = document.GetElementsByTagName("arguments");
    // Interpret command
    switch (command)
    {
        case "sendText" : resultTxt.Text = list[0].InnerText; break;
        case "Some_Other_Command" : break;
    }
}

Viola!

c# to flash activex demo

I have made the simple example discussed available here.

Related posts:

  1. Right click and custom context menu in Flash/Flex
  2. Use clipboard (copy/paste) in C# console application
  3. Open and Save files to Desktop without going to Server
  4. How to identify at runtime if the swf is in debug or release mode/build
  5. How to clone (duplicate) an object in ActionScript 3
Comments (13) Trackbacks (1)
  1. Thanks for the great resource

  2. It is nice work,
    I am new to Flex.
    It would be great if you can share the Action Script file as well, i.e the source code of SWF file as well.
    Thanks.
    Pankaj

  3. @Pankaj
    it’s there in the zip file under Flash Project folder

  4. Hi,
    In the Flash Project, there are two files .swf and .fla
    what I needed was some .as file or .msxml file which I can open on Flex Builder and see the code behind that flash file.

    Thanks
    Pankaj

  5. @Pankaj

    csharp.as
    ——————–
    package
    {
    import flash.external.ExternalInterface;

    public class Csharp
    {
    private var _text : TextArea;

    public function Csharp(someTextArea : TextArea)
    {
    this._text = someTextArea;
    ExternalInterface.addCallback(“addText”,addText);
    }

    // get message from c#
    private function addText(val:String):void
    {
    _text.appendText(val + “\n”);
    }

    // Send message to C#
    public function send(message : String):void
    {
    ExternalInterface.call(“sendText”, message);
    }
    }
    }

    the way to use it is

    var com : Csharp = new Csharp(responseBox); // where responseBox is a textarea where messages will be apended when recived from c#

    and if you want to send someting you do
    com.send(‘hello from flash’);

    that’s it. the code was in the article, rest of it was just…interface

  6. Hey man, this works perfect, but what if I want to send more than 1 data to C# with the same function. Do I have to put several ExternalInterface.call inside the button function o with just one I can send a lot of data ?

  7. @Lucas
    As you see, in c# there is a switch on the command function recived from flash. in the example i gave a comment ago, the function send(message:String) uses the command sendText, but you can use that as a parameter as well.

    public function send(command : String, message : String):void
    {
    ExternalInterface.call(command, message);
    }

    and in c#

    switch (command)
    {
    case “sendText” : doSometing(); break;
    case “Some_Other_Command” : doSometingElse(); break;
    case ….
    …..
    }

  8. Hi,

    This is my first interaction with flex.
    Can i use flex to carry out memory dump of flash application without using ShockwaveFlash COM object?

    Thanks.

  9. Hi,

    Thank you for this very useful article. Do you have idea, how can I extend this to give back a return value at AS->C# call ?
    Because now AS->C# is a simple procedure, not a function call and in a lot case return value is needed…

  10. Any way to do this with Flash Lite?

  11. @Scott
    Flash Lite has no equivalent api feature like the AS3 ExternalInterface api. One potential way to send data from Flash to another local application is with fscommand(). Another common approach is to build the native application as a local server and use xml sockets as a way to send data back and forth.

  12. great Work!Really Helpful.

  13. thank you !
    it was really helpfull !
    sample on adobe site is horror !
    this is awsome !

    KISS ! ( keep it simple & stupid )


Leave a comment


Uses wordpress plugins developed by www.wpdevelop.com