Reprint translation Introduction: About Flash and C + + Native Extension C + + extension ane--2

Source: Internet
Author: User

Life is the beginning of the beginning is not it? I'm still translating this.

Coding with C + +

With the C + + project finally set up, it's time to start adding some code. We'll start with the NativeAdd.h file so open up, that file and enter the code below.

After the C + + project is set up, add the following code to start with NativeAdd.h, as follows

 #include  flashruntimeextensions.h   extern   " c  "   void  Extinitializer (void  * * Extdatatoset, frecontextinitializer*  Ctxinitializertoset, Frecontextfinalizer* Ctxfinalizertoset); __declspec (dllexport)  void  Extfinalizer (  void  * Extdata); __declspec (dllexport) freobject doadd (Frecontext ctx,  void  * Funcdata, uint32_t argc, Freobject argv[]);}  

As mentioned, I ' m no authority on C + +, but I'll do my best to explain the code above. First, the #include is comparable to the import statement in ActionScript. This tells the file to also use the FlashRuntimeExtensions.h file. The extern "C" line tells the compiler, we want the following block of code to being compiled C-style and not C + + style.

I C + + is very slag, I try to explain the code, first, #include into the import statement to AS3, this tells the file, using the FlashRuntimeExtensions.h header file, extern "C" to tell the compiler, We want to compile the code enclosed below in C syntax.

The command I ' m least familiar with are the __declspec (dllexport) but I think it's safe to assume this essentially marks T He following functions as exports from our DLL; Meaning they is visible to other applications that might use the DLL.

The author says __declspec (dllexport) He is not familiar, he can only speculate about its role.

Following the __declspec (dllexport), we define our function signatures. Unlike AS3, the return type comes first. It is followed by the function name and then the parameter list. The first and functions is for setting up and shutting to our Native Extension. The third function is the one that would handle the bulk of the work for us, Doadd. Note the This header file, we is simply defining the function signatures, but not providing a function body. We'll do and next in the NativeAdd.cpp file.

Explain the difference between C syntax and AS3 syntax. The main point is that the first two methods are enabled, the second one is off Native Extension, and the third one is taken to handle the Doadd method. Detailed below

Open the NativeAdd.cpp file and enter the following functions. We'll go through each of the function one at a time, but first let's just make sure the file are all ready to go. At this point, make sure the file contains only the following code.

Open NativeAdd.cpp Add the following method, you need to ensure that only the following content.

" NativeAdd.h "  <stdlib.h>extern"C"{}

We ' re going to is adding all of our functions inside the extern "C" block and we'll start with our doadd function.

We'll put all the methods in the parentheses of the extern "C", first of all Doadd

 freobject Doadd (Frecontext ctx, void  *  Funcdata, uint32_t argc, Freobject argv[]) {int32_t One, both, sum;  Freobject result; FREGetObjectAsInt32 (argv[ 0 ], &one  ); FREGetObjectAsInt32 (argv[ 1 ], &two  );  Sum  = one +  FRENewObjectFromInt32 (sum,  &result);  return   result;}  

This is the function of actually going to does the work for we extension and return our sum. To begin with, we declare a few variables of type int32_t. The next variable is a freobject that would hold the result of our addition. The FRE types is what data is passed between Flash and C; I highly suggest checking out the Adobe documentation on working with the various types if you is going to being doing more Of this on your own.

This is the subject of the addition method, (the code itself), Freobject to save the results, Fretypes is the data from Flash to C transmission type. The author recommends looking at Adobe documentation for many types of usage, and then you can do more.

The next function calls grab the data passed in by Flash, which comes on via the argv array, and stores it in our Loca Lly defined variables one and both. These represent the numbers that'll be added together. Next We do the actual addition and store the result in the sum variable. The last thing we need to do before returning the sum was to convert it into a FRE object that Flash can understand. The function FRENewObjectFromInt32 takes care of the if US and now we can return the result.

FREGetObjectAsInt32 This method is used to obtain flash data from the argv, which is two addend, and then saved to the local two variables. And then do an addition. Finally, use the FRENewObjectFromInt32 method to return the results added in local C to flash.

You are wondering about the ampersands and asterisks if you aren ' t familiar with C coding. It has to does with the actual memory address of the variables and was quite beyond the scope of this tutorial, but if you ' re Interested in learning more, you can read up on the pointers on Wikipedia.

You would be curious to know how to do it in C, this is mainly memory address and so on, beyond the scope of this tutorial is no longer discussed. If you're interested, take a look at the Pointers section first.

The next couple of functions we define is not specified in our NativeAdd.h file, but they is necessary all the same.

The following two methods are not defined in the header file, but are used as well.

voidContextinitializer (void* Extdata,Constuint8_t* Ctxtype, Frecontext ctx, uint32_t* numfunctions,Constfrenamedfunction**functions) {  *numfunctions =1; Frenamedfunction* func = (frenamedfunction*) malloc (sizeof(frenamedfunction) * (*numfunctions)); func[0].name = (Constuint8_t*)"Doadd"; func[0].functiondata =NULL; func[0].function = &Doadd;*functions =func;}voidContextfinalizer (Frecontext ctx) {return;}

These methods deal with the ActionScript Extensioncontext object which we'll see if we get to the as part of the Tutori Al. The first function is the initializer. First we tell it how many functions we'll define, which in this case is just the 1, Doadd. Next We allocate memory for the function using malloc. After the We fill in some data regarding our function. We give it a name, set the data to NULL and then mark the address of our function. Lastly, we assign our frenamedfunction pointer, func, to the passed in Double-pointer functions. I don ' t has a good grasp on working with pointers so most of this I got from the Adobe docs.

These methods, which deal with As3 Extensioncontext object, will be discussed later when we relate to the AS3 section. The first method is to initialize, to tell that there are several methods defined, our case is only one, Doadd, and then allocate memory with malloc. After that, enter some data to the method, such as name.

Set data to NULL, mark the method low, and finally assign a frenamedfunction pointer to the pointer. The author says I can't do this stuff, completely copied adobe docs ... (pointer to pointer.) is to assign an address that points to the memory address of a pointer. Personal understanding)

The other function, the finalizer, doesn ' t really need to does anything so we keep it simple with nothing more than an empty Return statement.

The last of the functions we need to add is the ones we had declared in our. h file; The Extinitializer and Extfinalizer functions. The functions we covered above is responsible for dealing with the extension context object while the ones below is for The actual extension itself.

Another method, finalizer nothing special, the following two methods, Extinitializer and Extfinalizer functions.

void Extinitializer (void* * extdata, frecontextinitializer* ctxinitializer, frecontextfinalizer*  Ctxfinalizer) {  *ctxinitializer = &contextinitializer;   *ctxfinalizer   = &contextfinalizer;} void Extfinalizer (void* extdata) {  return;}

Again, the finalizer here are simple so we'll focus on the Extinitializer function. This function was actually pretty simple as well. All we ' re doing are telling the extension where to find the context initializer and finalizer functions-that ' s it.

Your NativeAdd.cpp file should look like this if it is completed.

The method is very simple, tell, extension where to find initializer, finalizer content. Finished.

Here is the full version

//NativeAdd.cpp:Defines The exported functions for the DLL application.#include"NativeAdd.h"#include<stdlib.h>extern "C"{freobject Doadd (frecontext ctx,void*Funcdata, uint32_t argc, Freobject argv[])    {int32_t One, both, sum;    Freobject result; FREGetObjectAsInt32 (argv[0], &One ); FREGetObjectAsInt32 (argv[1], &); Sum= one +both ; FRENewObjectFromInt32 (SUM,&result); returnresult; }  voidContextinitializer (void* Extdata,Constuint8_t* Ctxtype, Frecontext ctx, uint32_t* numfunctions,Constfrenamedfunction**functions) {    *numfunctions =1; Frenamedfunction* func = (frenamedfunction*) malloc (sizeof(frenamedfunction) * (*numfunctions)); func[0].name = (Constuint8_t*)"Doadd"; func[0].functiondata =NULL; func[0].function = &Doadd; *functions =func; }  voidContextfinalizer (Frecontext ctx) {return; }  voidExtinitializer (void* * Extdata, frecontextinitializer* Ctxinitializer, frecontextfinalizer*Ctxfinalizer) {    *ctxinitializer = &Contextinitializer; *ctxfinalizer = &Contextfinalizer; }  voidExtfinalizer (void*extdata) {    return; }}</stdlib.h>

You should now is able to successfully build the dll! You can find the NativeAdd.dll in either the Debug or Release folder in your project directory.

Good, compile it successfully, you can find the NativeAdd.dLL file under Debug or release file!

Reprint translation Introduction: About Flash and C + + Native Extension C + + extension ane--2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.