Simple programming example of interaction between ActionScript and C/C ++

Source: Internet
Author: User

If you don't want to talk about anything, go to the source code. For more information about alchemy, see the official documentation.

1.
Declare a header file myutil. h with the following content:

# Ifndef myutil_h

# Define myutil_h

# Include "as3.h"

Namespace myworld {

Class Io

{

Public:

Static as3_val _ bytearray_cls;

Static as3_val _ nsname;

Static as3_val _ clsname;

Static as3_val _ empty_array;

Public:

Static as3_val getbytearraycls ();

Static as3_val getemptyarrayobj ();

};

}

# Endif

2.
The implementation file of the header file myutil. H is as follows:

# Include "myutil. H"

Namespace myworld {

As3_val IO: _ nsname = NULL;

As3_val IO: _ clsname = NULL;

As3_val IO: _ bytearray_cls = NULL;

As3_val IO: _ empty_array = NULL;

 

As3_val IO: getbytearraycls ()

{

If (_ bytearray_cls = NULL)

{

/*

* Assign a value to _ bytearray_cls to obtain flash. utils. bytearray. skip this step. If you do not know it, contact us privately.

*/

}

Return _ bytearray_cls;

}

 

As3_val IO: getemptyarrayobj ()

{

If (_ empty_array = NULL ){


/*

* Assign a value to _ empty_array to obtain flash. utils. bytearray. skip this step. If you do not know it, contact us privately.

*/

}

Return _ empty_array;

}

}

3.
Declare a header file myarray. h with the following content:

# Ifndef myarray_h

# Define myarray_h

Int writeheader (char * Header, int length );

# Endif

4.
The implementation file of the header file myarray. h. The content is as follows:

# Include "myarray. H"

# Include <string. h>

Int writeheader (char * Header, int length ){

Char * STR = "helloworld ";

Int size = strlen (STR );

Int I = 0;

For (I = 0; I <size; I ++ ){

* (Header + I) = * (STR + I );

}

Return size;

}

5.
The main. cpp function is implemented as follows:

# Include "myarray. H"

# Include "myarray. cpp"

# Include "myutil. H"

# Include "myutil. cpp"

# Include "as3.h"

# Include <stdio. h>

As3_val geteaacheader (void * data, as3_val ARGs)

{

Char cppflvheader [128];

Int cppflvheaderlen = writeheader (cppflvheader, 128 );

As3_val asflvheader = as3_new (myworld: IO: getbytearraycls (), myworld: IO: getemptyarrayobj ());

Char * tmpcppflvheader = reinterpret_cast <char *> (cppflvheader );

As3_bytearray_writebytes (asflvheader, static_cast <void *> (tmpcppflvheader), cppflvheaderlen );

Return asflvheader;

}

Int main (){

As3_val geteaacheaderfun = as3_function (null, geteaacheader );

As3_val _ API = as3_object ("geteaacheader: as3valtype", geteaacheaderfun );

As3_release (geteaacheaderfun );

As3_libinit (_ API );

Return 0;

}

6.
Put these files in the same directory and compile them with alchemy. The command is as follows:

Alb-on; G ++ main. cpp-O3-wall-SWC-O main. SWC; Alb-off

7.
Call in flex Builder as follows:

ImportCmodule. awu. clibinit;

ImportFlash. utils. bytearray;

Public
Static const
_ Lib_init: cmodule. Main. clibinit =NewCmodule. Main. clibinit ();

Public static constCmoduleapi: * = _ lib_init.init ();

Public
Static Function
Geteaacheader (): bytearray {

ReturnCmoduleapi. geteaacheader ();

}

PS: I wrote an article at the beginning. Please forgive me. If you have any questions or communication, you can add your YY: 301558660

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/7466128

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.