Simple C# and Flash interop

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

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:

To set a variable in the Flash code named “myFlashVariable” to the value “Hello Flash”, just call:

To invoke the Flash function “myFlashFunction” (which takes a string and a number as input arguments) assemble the following XML string:

Pass this string into the following method on the Flash player control:

That is it. You can now invoke Flash functions.

To receive method invocations from Flash code, implement a method with the following signature:

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.

19 thoughts on “Simple C# and Flash interop”

  1. Hi, this post is just what I was looking for to integrate Flash and C#! Unfortunately I realized that very few documentation is available about the FlashObject ActiveX for .NET. I looked at your example and the one from Gabe Wishnie but I’m not able to get the “FlashControl.CallFunction()” working, it always throw an exception “Error HRESULT E_FAIL has been returned from a call to a COM component”. I get this exception also when I run the code written by Gabe Wishnie. Is there something wrong in my environment? What should I check? I noticed also the you are using in the XML string the tag while Gabe Wishnie is using the one: is it the same?

    Thanks, Sandro

  2. Hi Sandro! Glad this has been of some help.

    I believe your problem is that you have not made the Flash function you are invoking externally available within your ActionScript. I intended to go into greater depth from the Flash side in a later post, but haven’t gotten to it.

    If you have a function “MyActionScriptFunction” in your ActionScript, simply include the following line at the top of your ActionScript:

    ExternalInterface.addCallback("MyExposedFunction", null, MyActionScriptFunction");

    Then, you can invoke “MyExposedFunction” with the FlashControl.CallFunction().

  3. I have do eveyrthing fine on debug mode. But when come to release mode i having error “Error HRESULT E_FAIL has been returned from a call to a COM component” how to solve this.. problem..

  4. Hi, i have one flash, this Flash i have been embed in to file text.html, after i embed file text.html into C#, why all event of flash is interactive.??

  5. Moy:

    I’m not sure why this would vary between debug and release modes, unless it has something to do with how the supporting libraries are published to your release directory. Perhaps the proper Flash movie is not published to your release directory?


    With regard to registering the Shockwave Flash Object, you need to put the interop DLL generated by VS 2003 into your objDebug directory for the designer to find it. You mention an HTML file, and I have to admit I don’t know much about using ASP.NET with Visual Studio, but for winforms you need to right-click on the Toolbox and select “Choose items” to get the Flash control available in the designer.

  6. Hi evryone,

    I’m having a trouble in listing out all Flex controls inside FlashPlayer or get information about a control such as control type, the text inside control, and the location of control.

    Anybody, please help me. Greate to got a example application.


  7. Thuc:

    Sorry, I’m not aware of how to directly query information about objects inside of Flash from C#. I haven’t used Flex, however, so I don’t know if that differs.

    The only way I’ve done something similar to what you seem to want is to create custom accessor functions in the Flash, which allow me to get the information I need from the internals of the movie. It’s a little inconvenient, and not an option if you didn’t author the Flash code, but it’s all I’ve found in my limited experience.

    Good luck.


  8. i am getting the flashmovie played in the form and i need some of the sentences to be displayed on the movie which is in the xml file so how shud i link that xml file to the movie ?? kindly help me
    regards : shiva prasad .

  9. Shiva:

    I don’t quite understand what you are trying to do. The way I get text sentences from my C# code into my Flash code is by passing them in as a string argument, using the invoke interface.

    Good luck,

  10. Hi Bo,
    I have a .SWF file which has external interfaces implemented. I am creating a Windows Form with a flashElement in it.
    I try to pass parameters using Invoke interface with no effect. I tried the alternative to the invoke(string):
    axShockwaveFlash1.Loop = false;
    axShockwaveFlash1.Movie = myFilePath;
    axShockwaveFlash1.Playing = true;

    with no effect.

    can you help?

  11. Bo,
    I am running into some of the same problems that previous posters came across. I have my ExternalInterface.addCallback(“dostuff”, function_name); in ActionScript and I actually get a Flash Security Error #2060 when attempting this call. I also get the E_FAIL errors when trying to call into flash from the ActiveX control, but I assume those are because my callback registration failed. Are there are any settings I am missing? Thanks for any guidance.

  12. Hey Bo,

    Neat article. I was wondering if you have ever run into an issue I am having with our Media Player programmed with C# and Flash Interop.

    We have a form with an ActiveX ShockwaveFlashObject control on it. The program loops through a schedule of content and displays it over and over on the control, fullscreen, like this:

    axFlash.Move = scheduleItem.FilePath;

    This works great, but after a couple of days running, the form on which the Flash ActiveX control resides will throw an exception like this:

    System.Runtime.InteropServices.SEHException: External component has thrown an exception.
    at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
    at System.Windows.Forms.Control.DefWndProc(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.AxHost.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Looking at the taskmanager, I see that our program has allocated virtually all of the available memory on the machine. (~500MB)

    Have you ever seen anything like this before? Any ideas how I can get an external component (Flash ActiveX in this case) to release resources back without restarting the program? Maybe restarting the program periodically is the only good option? Would re-creating the form or the control fix things?

    1. Adam:

      Sorry for the long delay in reply.

      To answer your question: Actually, yes. In long runs, the last version of Flash I used (at least when used as an embedded control) horrendously leaked memory. The only solution I had for the issue was a fairly error-prone process of monitoring the running app, then killing and restarting it at regular intervals. The monitoring process even snapped a screenshot to mask the restart procedure. This is one of a number of reasons why I actually haven’t used Flash for a number of years. I wish I could tell you different, but I discovered no solution for avoiding the leaking.

  13. Thanks a lot ….i really mean…i was stuck on this issue for long and wasn’t getting any help………

    i was getting the same error as Sandro Comuzzi was facing…..and as you told i was missing the “addcallback” from my code…..

    now it’s working perfectly……

    thanks again…..

  14. i want to use shockwave flash object in windows application
    using visual studio 2008 through c# language.
    i have added shockwave flash object from toolbox by simply drag n drop on the WinForm.but i don’t know what i do next to display the swf file in shockwave flash object on execution.
    plzzz tell me how i do this.

  15. Even now I still find the implementation of flash on a windows form very useful. It’d be very convenient if transparency were permitted but it doesn’t seem to be the case. Anyone find a work around for this?

  16. Hey i have some issues with Flash and C# interop. Could you please be king enough to view my issue here


    Also as an additional note i THINK, not sure that the error goes when VS is installed on that particular machine.
    I have been having this issue since a long time and am unable to solve it any help would be highly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">