Last week I decided to look into building a native extension for AIR and was very surprised by the lack of quality resources and tutorials for doing so. I was able to find examples for Android using Java and for iOS using Objective-C, but pretty much nothing for a standard Windows C++ project. I was eventually able to get things up and running by piecing together bits of information from various sources, but I thought I'd take the time to write a step-by-step tutorial on how to do this so that perhaps others will have an easier time. In this tutorial you will create a native extension that simply adds two numbers together and returns the sum.
I'll start with a warning: this tutorial is not for the absolute beginner. You will need the AIR/Flex SDK, Microsoft Visual Studio, familiarity with C++ coding and be comfortable creating packages via the command line. Also, while I'm comfortable with Flash and ActionScript, I am definitely not an authority on C/C++ so there may be other (better) ways to do some things. Of course, if you happen to be an authority on that stuff, please feel free to suggest improvements! Ok, with the initial disclaimer out of the way, let's go ahead and get our hands dirty!
Setting up the C++ project
To begin, you'll need to have Microsoft Visual C++ 2010; and yes, the free Express edition is just fine. Once you've got that installed (and any necessary updates) go ahead and open it up and start a new project. We want to create a Win32 project so select that and give it the name NativeAdd.
Select Next and on the following screen, under Application Type, choose DLL (Dynamic-Link Library) and click Finish.
Great! The C++ project has been created! The next step is to make a few changes to the code; open up dllmain.cpp and the top of the file remove the following line:
#include "stdafx.h"
and replace it with this:
#include <Windows.h>
After that, let's go to the DllMain function and remove the entire switch statement so that the only code left in the function is: return TRUE;
The dllmain.cpp file should now look like this:
#include <Windows.h> BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; }
Now turn your attention to NativeAdd.cpp and remove the line: #include "stdafx.h"
The next thing we'll do is get rid of the extra files automatically added to the project by Visual Studio. In the solution explorer, open the Header Files folder and right click the stdafx.h file and select Remove. Go ahead and just click remove again if you get a popup box. Do the same to targetver.h and also stdafx.cpp in the Source Files folder.
The next step is to add a few files. First, we'll add NativeAdd.h to the Header Files folder. To do so, right click the Header Files folder and select Add > New Item. Select Header File from the list and name it NativeAdd.h.
Now we'll grab some necessary files from the Flex SDK and add them to our project. Navigate to the Flex SDK directory and enter the include directory. You'll want to copy the FlashRuntimeExtensions.h file from there. Go back into Visual Studio and right click the Header Files folder, select Add > Existing Item. When the file selection dialog comes up, paste the FlashRuntimeExtensions.h and then select it.
We've got one more file to grab from the Flex SDK so go back to the root folder of the Flex SDK. Navigate to lib/win and copy the FlashRuntimeExtensions.lib file. Right click the NativeAdd project in the solution explorer and select Add > Existing Item and again paste the file in the file selection dialog box and select it to add it to the project. If you've followed along your solution explorer should look like the image below.
We've got just one more thing to take care of before getting into the code. Because we removed the stdafx files, we need to tell the compiler not to look for them. To do this, right click the project file in the solution explorer. Select Properties to bring up the dialog box. Choose Configuration Properties > C/C++ > Precompiled Headers. At the top of the dialog box, change Configuration to All Configurations. Now, in the right-most pane, change the Precompiled Header option to Not Using Precompiled Headers. Use the image below as a guide if you need it.