This post will show you how to create an AIR Native Extension for Windows. Specifically, we will create an extension which uses .NET to transcode BitmapData objects into .bmp, .jpg, and .png formats much faster than existing ActionScript or Alchemy methods.
Requirements:
Checkout the project on GitHub for all the code.
- Source files
- Project files for Visual C++ and Flash Builder
- Batch files so you don’t have to use the command line
- All intermediate files
The project contains some placeholder files that I cannot include in the repository because they are copyrighted by Adobe. After checking out the project, you need to:
- Replace
FREImageProcessor/FREImageProcessor/FlashRuntimeExtensions.hwith AIR_SDK/include/FlashRuntimeExtensions.h - Replace
FREImageProcessor/FREImageProcessor/FlashRuntimeExtensions.libwith AIR_SDK/lib/win/FlashRuntimeExtensions.lib
Now that everything is setup, you can try compiling everything yourself.
- Open FREImageProcessor/FREImageProcessor.sln in Visual C++ 2010
- Build the .dll (F7)
- Import ImageProcessor into Flash Builder
- Build the .swc (Ctrl + B)
- Open the .swc with a .zip editor, and extract library.swf to the same folder as the .swc
- Build the .ane (run ImageProcessor/bin/package.bat)
- Import ImageProcessorTest into Flash Builder
- Build the .swf (Ctrl + B)
- Build the .exe (run ImageProcessorTest/bin-debug/package.bat)
If everything was successful, you can run the .exe and click the stage to encode a JPEG. It will be saved to “C:\temp.jpg”.
#1 by Joggl89 on September 14, 2011 - 7:58 am
Hey,
Is this also your prefered way to set up? So should I start by bulding the .as File which later calls the native code?
Please don’t mind asking, but what would be the difference in building this for android. I guess programming the .jar ist not the problem.
Would be cool if you could help me on this
#2 by Sean Fujiwara on September 14, 2011 - 3:41 pm
Yes, I think it’s easier to work ‘backwards’ from the interface to the implementation.
For Android, Step 2 is completely different, and in Step 3 you replace “Windows-x86″ with “Android-ARM”. I will try to post a sample for Android later this week.
#3 by Johannes Boppre on September 14, 2011 - 11:30 pm
Nice I’m looking forward to read this
Cheers,
Joggl89
#4 by Sean Fujiwara on September 21, 2011 - 12:27 pm
Hi Johannes, I didn’t have time to write an example for android, but Adobe has posted their official examples: http://www.adobe.com/devnet/air/native-extensions-for-air.html
Good luck!
#5 by Ray Kutro on September 21, 2011 - 11:47 am
Nice work. I want to do it with C# rather than C++, do you have any example of doing it with C#? it would be really helpful.
Thanks
#6 by Sean Fujiwara on September 21, 2011 - 12:43 pm
If you know how to combine C# and C++, you’re already better off than me. I looked into it, and it seems pretty complicated. But, I don’t think there’s anything specific to AIR native extensions.
#7 by Daniel Frank on September 26, 2011 - 8:18 pm
Anyway to get AIR Main Window HWND?
#8 by Sean Fujiwara on September 26, 2011 - 8:50 pm
There’s no API for it, but I’m sure you could get it using GetForegroundWindow or something.
http://msdn.microsoft.com/en-us/library/ms633505%28v=VS.85%29.aspx
What do you need the HWND for?
#9 by Daniel Frank on September 27, 2011 - 4:24 am
I need HWND to use Windows 7 Tasklist API like JumpLists, ProgressBar and Icon Overlay.
#10 by Daniel Frank on September 27, 2011 - 10:28 pm
Here (http://dl.dropbox.com/u/473693/ane/TaskBarProgressTest.exe) a simple test app using GetForegroundWindow to provide HWND.
I will put the sources in github soon.
How can i address this extension to apear in ( http://www.adobe.com/devnet/air/native-extensions-for-air.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+developer_center_tutorials+%28Adobe+Developer+Center+RSS+Feed%29)?
thx,
#11 by Sean Fujiwara on October 1, 2011 - 5:12 pm
Very cool. I’m not sure what the official way to get a post on that page is, but I’ll let you know when I find out.
#12 by Sean Fujiwara on October 6, 2011 - 1:35 am
I’ve replied via e-mail.
#13 by Daniel Frank on October 1, 2011 - 6:13 pm
A new version for the taskbar extension can be found at https://github.com/daniel-frank/fre-taskbar-progress
Now using EnumWindows to acquire HWND.
#14 by Sean Fujiwara on October 1, 2011 - 6:32 pm
That seems like the best way to get the main HWND, thanks for sharing.
#15 by Dheeraj on October 24, 2011 - 4:18 am
Hi,
I tried to launch the application with Flex 4.5.1+Air 3.0 SDK but I faced the following error. Even My Flash Builder updated to 4.5.1
Process terminated unexpectedly.
The content cannot be loaded because there was a problem loading an extension: Error: Requested extension com.magicalhobo.utils.ImageProcessor could not be found.
Launch command details: “C:\Program Files\Adobe\Adobe Flash Builder 4.5\sdks\Air3.0_flex4.5.1\bin\adl.exe” -runtime “C:\Program Files\Adobe\Adobe Flash Builder 4.5\sdks\Air3.0_flex4.5.1\runtimes\air\win” D:\ProjectsR&D\FREImageProcessor\ImageProcessorTest\bin-debug\ImageProcessorTest-app.xml D:\ProjectsR&D\FREImageProcessor\ImageProcessorTest\bin-debug
#16 by Sean Fujiwara on October 24, 2011 - 10:16 am
That sounds right. You can’t use native extensions when debugging with Flash Builder 4.5, because it doesn’t support them. We have to wait until Flash Builder 4.6 for that.
#17 by 七夜 on October 25, 2011 - 7:26 pm
Thanks
#18 by Shinji on October 26, 2011 - 4:11 am
Good article. Perhaps you can help us out. My collegae and I have been struggling with the call from the flashbuilder project. We created a very simple one , nothing too fancy. Just want to return a string. We’ve been getting the same error over and over
ArgumentError: Error #3500: The extension context does not have a method with the name test.
at flash.external::ExtensionContext/_call()
at flash.external::ExtensionContext/call()
at nl.techtribe.test.nativeExtension::TestExtension/test()[C:\Users\Sander Riels\Adobe Flash Builder 4.5\TEST_NATIVE_EXTENTION\src\nl\techtribe\test\nativeExtension\TestExtension.as:30]
at HelloWorldExtensionWorkflow/clickHandler()[C:\Users\Sander Riels\Adobe Flash Builder 4.5\TEST_NATIVE_EXTENTION\src\HelloWorldExtensionWorkflow.mxml:27]
at HelloWorldExtensionWorkflow/___HelloWorldExtensionWorkflow_Button1_click()[C:\Users\Sander Riels\Adobe Flash Builder 4.5\TEST_NATIVE_EXTENTION\src\HelloWorldExtensionWorkflow.mxml:37]
I’ve read that more people had the same problem for instance in this article
http://blog.aboutme.be/2011/09/12/air-native-extensions-hello-world-example-ant-script/
However those solutions do not seem to work. Did you encounter this problem as well and if so how did you solve?
#19 by Sean Fujiwara on October 26, 2011 - 2:27 pm
There are a lot of reasons you could be getting that error. I would put some logging in your native code to see if the context initializer is ever called.
You can copy the logging from my pen tablet extension: https://github.com/magicalhobo/FREPenTablet/blob/master/FREPenTablet/FREPenTablet/DLLMain.cpp
If it’s not being called, something is going wrong during packaging. If it is, something is wrong with your context initializer.
#20 by Shinji on October 27, 2011 - 6:55 am
Thanks for the quick reply. I´ll try out the logger.
If I have a positive result , I will let you know.
#21 by 七夜 on November 7, 2011 - 2:44 am
Error encoding: TypeError: Error #1009: Cannot access a property or method of a null object reference.
I don’t know how to deal with it.
#22 by Aaron Winters on November 10, 2011 - 11:36 am
I am also having this error (Error #1009: Cannot access a property or method of a null object reference.)
I know that when I execute the:
context = ExtensionContext.createExtensionContext
line, that the context variable is null afterwards, thus giving the error when I call it’s .call function.
#23 by Brad Wiederholt on April 21, 2012 - 3:05 pm
I have the same issue, and like Aaron, it’s because context = ExtensionContext.createExtensionContext is returning null. I’m trying to work backward, wondering if my extension.xml is ok (it appears fine) or maybe the .dll I built is not behaving properly (like contextInitializer is misbehaving). I’m just not finding much in the way of troubleshooting this. Any ideas?
#24 by 七夜 on November 7, 2011 - 4:50 am
I just kow that fb 4.5 cannot output directly。My god. Can fb 4.6?
#25 by Sean Fujiwara on November 7, 2011 - 4:53 am
I think Flash Builder 4.6 will be able to. For now it’s not too hard if you use the batch files.
#26 by 七夜 on November 8, 2011 - 4:50 am
Thanks.However,l have a question what .ane files used to do.
#27 by 七夜 on November 8, 2011 - 10:35 pm
I success. Thanks very much for your title.
#28 by Derek on November 8, 2011 - 2:29 pm
I’m getting
unexpected failure: Platform conversion failed. Process exited with error 5. Output was “[ErrorEvent type="error" bubbles=false cancelable=false eventPhase=2 text="Unhandled exception Error: open[110]: MSI Error” errorID=0]” then a java.io.IOException that is identical.
When I am running adt. Any Idea why?
(just as a note I do have the air 3.0 sdk installed over flex 4.5.1 and I am using flashbuilder 4.5 not 4.6)
#29 by Sean Fujiwara on November 8, 2011 - 3:18 pm
I haven’t come across that error, but try this blog post: http://blog.tiltdigital.com/tag/platform-conversion-failed/
#30 by Derek on November 8, 2011 - 5:25 pm
Thanks, I tried that and indeed was missing some a couple steps (flashplayerglobal in particular)
However, after fixing it I still get the same error. The problem is not in creating the ane but in packaging the final air exe.
#31 by Derek on November 8, 2011 - 6:25 pm
Nevermind. That was indeed it. I deleted the whole sdk and re-downloaded/copied/installed/edited it and it works now.
Many thanks!
#32 by Sean Fujiwara on November 8, 2011 - 8:23 pm
Cool, glad you got it working.
#33 by Ken Patel on March 21, 2013 - 11:39 am
Make sure the following file is writeable (cannot be read-only):
sdk\runtimes\air\win\Adobe AIR\Versions\1.0\Resources\template.msi
#34 by Felix Turner (@felixturner) on February 15, 2012 - 2:49 pm
Windows only? Boo!
#35 by Sean Fujiwara on May 10, 2012 - 11:48 pm
Feel free to create a Mac/Linux version and post a link here
#36 by meenakshi on March 29, 2012 - 10:51 pm
Hi,
If I use a simple class in c# and make a dll.Then try to communicate with AIR native extension.I am getting the context but unable to call dll method.I am getting an Error #3500: The extension context does not have a method with the name GetVersion.What should i do.Do I need to write some extra lines code in dll in order to communicate with AIR.Please Do post ur comments.
Thanks
#37 by Meenakshi on April 12, 2012 - 10:55 pm
hi ,
I just downloaded your dll and package into ane.Then build a test ane project but calling native function is giving error 3500 each time.I don’t have visual studio thats why just used the dll downloaded.I am using AIR 3.2.So make necessary changes on extension.xml.
What should I need to get rid off this error.
#38 by Sean Fujiwara on May 10, 2012 - 11:52 pm
If you don’t have visual studio, the problem is probably that you don’t have the debug version of .NET. You have to install Visual Studio to use the DLL as I posted it.
#39 by Victor on May 7, 2012 - 9:07 am
Hello n_n
Thanks for the wonderful tutorial! Everything goes off without a hitch, but for some reason the “C:\temp.jpg” output is never produced. The test application (ImageProcessorTest.exe) outputs the following:
Click to capture stage contents
Extension created successfully
Encoded to JPEG of 5905 bytes in 113ms
Extension created successfully
Encoded to JPEG of 11172 bytes in 8ms
Extension created successfully
Encoded to JPEG of 17110 bytes in 7ms
Extension created successfully
Encoded to JPEG of 21983 bytes in 7ms
Extension created successfully
Encoded to JPEG of 26636 bytes in 8ms
So it is doing -something- successfully. I’m just having a difficult time discovering what that something is! XD
#40 by Sean Fujiwara on May 10, 2012 - 11:50 pm
That’s strange, sounds like it should be working. I would suggesting adding logging in the ANE. You can see an easy example of that here: https://github.com/magicalhobo/FREPenTablet/blob/master/FREPenTablet/FREPenTablet/DLLMain.cpp
#41 by Kuro on June 16, 2012 - 11:33 am
Thank You Sample Code. but it is not call function save()
#42 by David Mouton on June 1, 2012 - 3:29 am
Hi,
I’ve got some trouble with transparent PNG. Sometimes, i’ve got a gray border around png transparent element, that is not in my BitmapData ! I think this issue is in relation with smooth edge. Any Idea ?
#43 by kicksaintnick on August 19, 2012 - 4:22 am
I can’t seem to get this project to work in FlashDevelop. FlashDevelop has trouble recognising the ANE file. I haven’t had issues with other ANE files only this one. A bit unusual :/