Tutorial: Flash and C++ Native Extension

Using the native extension with Flash Develop

Using a native extension with Flash Develop is actually a bit of a pain, but it is definitely my preferred IDE, so I'll show you how to do this. I got these steps from this video by the AIR Kinect guys, so if you get confused reading this, you can always watch that video instead.

Start by creating a new AIR AS3 Projector project in Flash Develop. Next copy the NativeAdd.ane file from the Flex SDK bin directory and paste it into the lib folder of your Flash Develop project. Go ahead and rename the ane file to an swc as well; this will enable code completion for the extension in Flash Develop. Then right click the file and select Add to Library.

Now open the application.xml file; we need to add a couple of things to this file, just above the closing tag. We'll need to add extended Desktop support and also our extension. To do this, paste the following xml just above the closing tag in the application.xml file.

<supportedProfiles>extendedDesktop</supportedProfiles>

<extensions>
  <extensionID>com.yourname.NativeAdd</extensionID>
</extensions>

In the extensionID tag you'll want to put the fully qualified name of your extension.

Next, add a new folder at the root level of your Flash Develop project and call it extension. In this folder, create two more folders: one named debug and another named release. Now copy (not cut) and paste the NativeAdd.swc from the lib folder into the release folder and change the name back to NativeAdd.ane.

Now copy the NativeAdd.swc from the lib folder again and paste it into the extension/debug folder. Rename it to NativeAdd.zip and extract it. Now rename the extracted to folder to NativeAdd.ane; you can delete the NativeAdd.zip file leaving only the NativeAdd.ane folder. We're almost done, I promise!

The next step is to open the run.bat file. On the line with: adl "%APP_XML%" "%APP_DIR%" add the following: -extdir extension/debug/

Here is a screen capture for reference.

With any luck you should now be able to build and run the project with no errors! That's great and all, but we haven't even used our extension yet; let's do that now!

Open Main.as and paste the following code into the constructor function.

var ane:NativeAdd = new NativeAdd();			
var sum:int = ane.doAdd(5, 5);
trace(sum);
ane.dispose();

If you run the project now you should see it trace out 10 to the output window! It's not exactly the most interesting program ever created, but you have just used native C code to perform addition from within your Flash project! Cool!

Wow! If you made it this far, thank you and congratulations! You have created your very own native extension and got it running within Flash! If you don't have things working at this point, don't get too upset about it; there were A TON of steps to follow here and even the simplest mistake could derail the whole thing. As usual, all the materials used here are available for download. I encourage you to check them out if you're having trouble.

Download Source

NOTE: If you get an error about not being able to find the Flex/AIR SDK from FlashDevelop with the downloaded source and are running on a 32 bit machine, go into the SetupSDK.bat file and change the Path to Flex SDK to C:\Program Files\FlashDevelop\Tools\flexsdk. This project was built on a 64 bit Win7 machine and your path may be different. Also ensure you have the latest FlashDevelop and/or Flex SDK installed.

Pages: 1 2 3 4 5

Bookmark and Share

35 Responses to “Tutorial: Flash and C++ Native Extension”

  1. Impressive publish! STICK WITH IT!

  2. Khan says:

    Excellent tips. Good tutorial for c++.

  3. Tomasz Dysinski says:

    Excellent and thorough!

    I have a comment about the following command line:

    adt -package -target ane NativeAdd.ane extfiles/extdesc.xml -swc extfiles/com.yourname.NativeAdd.swc
    -platform Windows-x86 -C extfiles NativeAdd.dll library.swf

    You left the namespace in extfiles/com.yourname.NativeAdd.swc. I couldn’t get it to work until I removed the namespace part, but then again, I wrote a BAT file to run this from the bin folder instead of the extfiles folder in the flex SDK bin folder (more typing, but less chance of misplacing files).

    Anyways, thanks again for taking the time to write this.

  4. Rhuno says:

    There is a lot going on in this tutorial. I do plan on eventually trying to make a video tutorial for this as it may be easier to follow.

  5. Erik Forsstrom says:

    Thank you so much for taking your time and writing this great tutorial!

    If you have plans to create a .bat file for the AS3 side, this acompc compiler option helps alot
    -directory=true -output=destination_directory
    This saves you the step of unpacking the library.swf from the .swc
    Run the acompc twice, one for the .swc and one for the .swf

  6. Mike says:

    Great tutorial.

    Thank you for your time to put this toghter. Bookmarked.

    Cheers Mike.

  7. BunbunManJu says:

    Hi, I have this error when I tried to package the ane file. It say ‘java’ is not recognised as an internal or external command. Do you know how I can resolve this?

  8. BunbunManJu says:

    Hi, I had solved the previous error. But now, I have another one.
    _ctx = ExtensionContext.createExtensionContext(); keep returning null. Please help. Q.Q

  9. BunbunManJu says:

    Hi again, I have gotten pass the above error too and now when I build the flash develop project, it did trace out 10 in the output window, the thing is I got a error while loading initial content in the AIR debug launcher. I have totally no idea what is wrong as I followed your tutorial step by step. Please help me. O ya, one thing, I can’t seem to put at the end of my NativeAdd.cpp. it gives me a syntax error. Is it because of this?

  10. BunbunManJu says:

    I tried the extension in flash pro and it keep spitting the error that _ctx is null even though I had call the constructor. O dear… Please help!

  11. Rhuno says:

    I haven’t tried this with Flash Pro, but I’m guessing there is some way you need to tell it you’re using a native extension.

  12. neelam ojha says:

    great article. keep it up.thanks

  13. chenze says:

    It ‘s very technical! It ‘s helpful for me, thanks a lot.

  14. Luis Furtado says:

    Just amazing! Thank very much!

  15. Nemis says:

    Thanks!

  16. John says:

    Great tutorial! Everything’s going real well so far, except I have one question. On this page, there’s the following phrase:

    Now rename the extracted to folder to NativeAdd.ane; you can delete the NativeAdd.zip file leaving
    only the NativeAdd.ane folder. We’re almost done, I promise!

    I’m using WinRAR, but I don’t think it should make that much of a difference. When I try to extract that zip file, I just get four files out of it, not another folder. When you say to rename the extracted folder to NativeAdd.ane, what do you mean? It sounds like you mean to put these four files into a folder, then rename that folder. But on Windows, this just changes the name of the folder; it doesn’t give you a real ane file back.

  17. Rhuno says:

    John, that’s correct. If WinRAR gives you the four files, you just need to put them in a folder and name that folder NativeAdd.ane. I realize this just changes the name of the folder and does not give you a real ane file, but that’s just the way the debugging works. Wonky, eh?

  18. Sam says:

    Thanks, but the info provided is incomplete. There’s no info about how to *adapt* the example for an *existing* AIR project as this is what all we want, NOT just run this simplistic “example”.
    There is missing information, like:
    1. My AIR project is on another folder. Should I just copy the airapp.as file to c:\com\username\airapp ?
    2. My AIR package doesn’t have any folders (like package com.username), should I move and rebuild the whole project somewhere else and add this, or not?
    3. Should I built the whole project in c:\com\username\airapp instead?

  19. Rhuno says:

    Sam, the last page here describes how to use the native extension within a project. It doesn’t matter where your AIR project is because the ANE is self-contained. To use a native extension with any project, new or existing, all you need to do is copy the ANE over and make sure it is properly added to the project.

  20. Sam says:

    Also no matter what I’ve tried, I get the error: Could not find source for class com.username.

  21. Sam says:

    Just saw your post, the problem is I cannot get passed the acompc on page 3. I get the above error.

  22. Rhuno says:

    The error means the compiler can’t find your classes. Make sure the source path is set correctly and also the include-classes. Note that you need to include each file name, it won’t work if you just specify a folder.

  23. Hassan says:

    Hi Rhuno,
    I just want to say many thanks for taking time to explain all steps required to build windows ANE.

  24. Zain says:

    i got error when run the adt command: “output file is not writable”. it’s might because something about win 8 security. Then i try change the output location to drive D, and it work well.

    the other error i got is about jre installation for Flash Develop, but it could be solved after uninstalling the older version of jre..

    It’s very good tutorial, easy to understand and follow. Very deep and technical. i tried and succeed.
    i love it. thanks, for sharing :)

  25. Dave Dyer says:

    Kudos. Your ANE tutorial got me over the hump.

  26. parmar says:

    Hello Sir,

    Thanks Sir, No word for this tutorial.it was great.
    Sir I am Stuck with how to create a dll using String data type . I am also new in C++ .
    what you have used that is int data type.
    can u help me in this.
    Your help is solicited.

    Regards,
    Parmar

  27. parmar says:

    Hi,

    Please help me to create ANE using string data type.

    Regards,
    Parmar

  28. parmar says:

    Hi,
    Still waiting , please help me to create ANE with string Manipulation …

    Regards,
    Parmar

  29. Rhuno says:

    Parmar, strings are handled in a similar manner to other data types. You need to use the FREGetObjectAsUTF8 and FRENewObjectFromUTF8 functions to get/set them. In the flex sdk, go to the include directory and look at the FlashRuntimeExtensions.h. That has all the native functions available to you and is heavily commented.

  30. parmar says:

    Thanks Rhuno…

    Actually for me const uint8_t* … these kind of data types are new.
    So I am feeling tough to manipulate these kind of data type in simple int , string data types,thatswhy I was asking for any string manipulated dummy app of any example to do that.

    I will try to understand FlashRuntimeExtensions.h. hope i will be able to do string manipulations.

    Once again thanks a lot for your support.
    Thanks

    Regards,
    Vishal Parmar

  31. Zain says:

    is it possible to hold instance of a variable in the C dll with ANE?
    So several FREObject function could use it.

  32. kleone says:

    Perfect tutorial. Thank you!

  33. mehdadoo says:

    Man your article was great! Thank you so much.

  34. SourceSkyBoxer says:

    I have problem with error include :( I have added h and lib why does it not work :(

  35. Boris says:

    Your article was beautiful

Leave a Reply

Subscribe to RSS feed FGS5 Badge