You are viewing bojolais

Simple C# and Flash interop

« previous entry | next entry »
Feb. 11th, 2006 | 03:14 am

I've recently promised Larry I'd share new insights about interoperability between C# code and Flash.

Flash is often assumed to be only a toy for web designers, but it's actually a very powerfully programmable visualization tool in the hands of a technically capable artist. In the not-quite-immediate future for Windows programmers, the Windows Presentation Foundation promises to obsolete Flash for most Windows-native applications, but WPF currently exists only as technology previews. You have plenty of time to get dangerous with a little Flash in your .NET before WPF is a viable alternative.

There are two primary strategies for interoperability with Flash, and the new External API is what I'll show here (the alternative involving some simple socket communication between a .NET app and an out-of-process Flash player). Essentially, you can embed a ActiveX Flash player into a Windows form, load Flash code ("movies") into the player, then set variables or invoke functions with single method calls in the flash code from C#. Hook up a delegate to one of the Flash player's events, and you can receive communcation back from the Flash code. It's that simple, as you can see in this post by Gabe Wishnie.

Most complicated was simply getting the Flash control to work in Visual Studio 2005. Basically, it doesn't automatically create the interop DLL properly, but placing the interop DLL generated by VS.NET 2003 into your project's obj directory is a quick work-around. Read more about this work-around on Mark Dryden's site.

In the Forms designer in VS 2005, right-click in the Toolbox and select "Choose items" to add the "Shockwave Flash Object" COM component (assuming you've installed Flash and have the control registered). Next, drag the player control to your form. To begin interacting with Flash, first load your Flash code into the control:
    this.yourFlashControl.LoadMovie(0,
        Application.StartupPath + "\\yourFlashMovie.swf");


To set a variable in the Flash code named "myFlashVariable" to the value "Hello Flash", just call:
    this.yourFlashControl.SetVariable("myFlashVariable", "Hello Flash!");


To invoke the Flash function "myFlashFunction" (which takes a string and a number as input arguments) assemble the following XML string:
    <invoke name="myFlashFunction" returntype="xml">
        <arguments>
            <string>Hello Flash!</string>
            <number>42</number>
        </arguments>
    </invoke>    


Pass this string into the following method on the Flash player control:
    this.yourFlashControl.CallFunction(yourXmlInvokeString);


That is it. You can now invoke Flash functions.

To receive method invocations from Flash code, implement a method with the following signature:
    void FlashCallEventHandler(object,
        AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent)


Register a delegate to this method with the FlashCall event to hook this up. Whenever the External API is used within Flash to call an external method, your delegate will be invoked. The AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent's request property will consist of an XML string in the same format we passed to Flash earlier, so simply parse out the intended method name and input arguments and act upon them as you wish.
    private yourInitMethodOfChoice()
    {
        this.yourFlashControl.LoadMovie(0, Application.StartupPath +
            "\\yourFlashMovie.swf");
        this.yourFlashControl.FlashCall +=
            new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(
                this.FlashCallEventHandler);
    }

    private void FlashCallEventHandler(object sender,
        AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
    {
        // inside here, just parse e.request.  Some validation is recommended, but
        // using reflection to directly invoke methods requested via Flash is very
        // simple to implement.
    }

Link | Leave a comment | Share

Comments {9}

Petrorabbit

(no subject)

from: petrorabbit
date: Apr. 13th, 2006 03:50 am (UTC)
Link

Dude your C# is lame compared to my ActionScript. You don't even have a variable named something like "tempVarForThisFunction." I'll keep hope alive that your junk is sufficiently sloppy when you're not posting it on LJ for Larry. Otherwise, it's your ass,

Reply | Thread

bojolais

(no subject)

from: bojolais
date: Apr. 13th, 2006 01:45 pm (UTC)
Link

Does "internal void InitializeBecauseICantDoWhatIWantInTheConstructor()" count? I don't think any of my "public class AssClass" or assInstance1 or molestInt have actually made it into any (shipped) code...

Reply | Parent | Thread

bad xml

from: anonymous
date: Jan. 28th, 2009 11:43 pm (UTC)
Link

warning: if you copy the xml from this page, as i did, make sure you remove the trailing slash from the first . i didn't notice it and spent quite a while trying to figure out why it wasn't working.

Reply | Thread

bojolais

Re: bad xml

from: bojolais
date: Jan. 28th, 2009 11:51 pm (UTC)
Link

Indeed. Updating post now.

Reply | Parent | Thread

Any ideas on how to make SetVariable work with AS3?

from: anonymous
date: May. 7th, 2009 08:35 pm (UTC)
Link

Any ideas on how to access 'this.yourFlashControl.SetVariable("myFlashVariable", "Hello Flash!");
' with AS3?

We have a legacy app that uses this and I have no way of updating it. I'm writing new modules and can't find any way to access variables set in this manner using AS3. I'm wanting to use AS3 for a number of reasons.

I've tried using Flash's ExternalInterface and also LoaderInfo. The only thing I can think of doing right now is to use LocalConnection to send the vars from the old AS1 stuff over to the new AS3 modules we're creating.... and that just sounds aweful.

Any ideas?

Reply | Thread

bojolais

Re: Any ideas on how to make SetVariable work with AS3?

from: bojolais
date: May. 7th, 2009 09:00 pm (UTC)
Link

Unfortunately I haven't used any of this in a few years, and nothing past the versions I referenced in the article (Flash 8?).

Sorry, and good luck!

Reply | Parent | Thread

Re: Any ideas on how to make SetVariable work with AS3?

from: anonymous
date: May. 19th, 2009 04:55 pm (UTC)
Link

No worries. Thx for the reply.

Reply | Parent | Thread

Too late.

from: anonymous
date: Aug. 21st, 2009 12:34 pm (UTC)
Link

Hello, i know these comments were posted like 3 years ago, and that i might not get a reply to this, but how would i make a function in C# that would find a variable which the user wants to find, and display it in a textbox?

Please reply, thanks!

Reply | Thread

Coole übersichtliche Seite

from: anonymous
date: Jan. 26th, 2011 12:15 am (UTC)
Link

Tolle Blogs die auf jedenfall auch uns gefallen duerfte. Aber auch meine Blogseite dürfte gefallen auch wenn sie sich um [url=http://www.sexakt.org][/b]Sex Cam[/b][/url] dreht, aber voellig private Girls zeigt.

Reply | Thread